Skip to content

Commit 40d64fd

Browse files
SIMPLE-6813 added deployed_mac_address property to Interface (#116)
1 parent a9d4f50 commit 40d64fd

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

virl2_client/models/interface.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def __init__(
8383
"ipv4": None,
8484
"ipv6": None,
8585
}
86+
self._deployed_mac_address = None
8687

8788
def __eq__(self, other):
8889
if not isinstance(other, Interface):
@@ -252,6 +253,12 @@ def discovered_ipv6(self) -> str | None:
252253
self.node.sync_l3_addresses_if_outdated()
253254
return self._ip_snooped_info["ipv6"]
254255

256+
@property
257+
def deployed_mac_address(self) -> str | None:
258+
"""Return the deployed MAC address of the interface."""
259+
self.node.sync_interface_operational()
260+
return self._deployed_mac_address
261+
255262
@property
256263
def is_physical(self) -> bool:
257264
"""

virl2_client/models/node.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Node:
5656
"vnc_key": "{lab}/nodes/{id}/keys/vnc",
5757
"layer3_addresses": "{lab}/nodes/{id}/layer3_addresses",
5858
"operational": "{lab}/nodes/{id}?operational=true&exclude_configurations=true",
59+
"inteface_operational": "{lab}/nodes/{id}/interfaces?data=true&operational=true",
5960
}
6061

6162
def __init__(
@@ -130,6 +131,7 @@ def __init__(
130131
self._pinned_compute_id = pinned_compute_id
131132
self._stale = False
132133
self._last_sync_l3_address_time = 0.0
134+
self._last_sync_interface_operational_time = 0.0
133135
self._parameters = parameters
134136

135137
self.statistics: dict[str, int | float] = {
@@ -195,6 +197,17 @@ def sync_l3_addresses_if_outdated(self) -> None:
195197
):
196198
self.sync_layer3_addresses()
197199

200+
@check_stale
201+
@locked
202+
def sync_interface_operational_if_outdated(self) -> None:
203+
timestamp = time.time()
204+
if (
205+
self.lab.auto_sync
206+
and timestamp - self._last_sync_interface_operational_time
207+
> self.lab.auto_sync_interval
208+
):
209+
self.sync_interface_operational()
210+
198211
@property
199212
@locked
200213
def state(self) -> str | None:
@@ -835,24 +848,6 @@ def sync_layer3_addresses(self) -> None:
835848
interfaces = result.get("interfaces", {})
836849
self.map_l3_addresses_to_interfaces(interfaces)
837850

838-
@check_stale
839-
@locked
840-
def sync_operational(self, response: dict[str, Any] = None):
841-
"""
842-
Synchronize the operational state of the node.
843-
844-
:param response: If the operational data was fetched from the server elsewhere,
845-
it can be passed here to save an API call. Will be fetched automatically
846-
otherwise.
847-
"""
848-
if response is None:
849-
url = self._url_for("operational")
850-
response = self._session.get(url).json()
851-
self._pinned_compute_id = response.get("pinned_compute_id")
852-
operational = response.get("operational", {})
853-
self._compute_id = operational.get("compute_id")
854-
self._resource_pool = operational.get("resource_pool")
855-
856851
@check_stale
857852
@locked
858853
def map_l3_addresses_to_interfaces(
@@ -879,6 +874,37 @@ def map_l3_addresses_to_interfaces(
879874
}
880875
self._last_sync_l3_address_time = time.time()
881876

877+
@check_stale
878+
@locked
879+
def sync_operational(self, response: dict[str, Any] = None):
880+
"""
881+
Synchronize the operational state of the node.
882+
883+
:param response: If the operational data was fetched from the server elsewhere,
884+
it can be passed here to save an API call. Will be fetched automatically
885+
otherwise.
886+
"""
887+
if response is None:
888+
url = self._url_for("operational")
889+
response = self._session.get(url).json()
890+
self._pinned_compute_id = response.get("pinned_compute_id")
891+
operational = response.get("operational", {})
892+
self._compute_id = operational.get("compute_id")
893+
self._resource_pool = operational.get("resource_pool")
894+
895+
@check_stale
896+
@locked
897+
def sync_interface_operational(self):
898+
"""Synchronize the operational state of the node's interfaces."""
899+
url = self._url_for("inteface_operational")
900+
response = self._session.get(url).json()
901+
self.lab.sync_topology_if_outdated()
902+
for interface_data in response:
903+
interface = self.lab._interfaces[interface_data["id"]]
904+
operational = interface_data.get("operational", {})
905+
interface._deployed_mac_address = operational.get("mac_address")
906+
self._last_sync_interface_operational_time = time.time()
907+
882908
def update(
883909
self,
884910
node_data: dict[str, Any],

0 commit comments

Comments
 (0)