Skip to content
This repository was archived by the owner on Nov 9, 2023. It is now read-only.

Commit 5d998f4

Browse files
authored
Merge pull request #39 from jenniexie/route_and_addr_api
add API to get node's route and ip
2 parents ce8b798 + a2f8ea9 commit 5d998f4

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

happy/HappyNodeRoute.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,15 +490,30 @@ def __update_state(self):
490490
else:
491491
self.removeNodeRoute(self.node_id, self.to)
492492

493+
def getNodeRoute(self, node_id, route_type):
494+
"""This function will get node route base on node_id and route_type.
495+
496+
Args:
497+
node_id (str): A string containing the node_id, example: "cloud", "BorderRouter"
498+
route_type (str): A string containing ip type, example: "v4", "v6"
499+
500+
Returns:
501+
node route ip
502+
"""
503+
node_route_prefix = self.getNodeRoutePrefix(route_type, node_id)
504+
node_route_via_id = self.getNodeRouteVia(route_type, node_id)
505+
node_route_addrs = self.getNodeAddressesOnPrefix(node_route_prefix, node_route_via_id)
506+
if len(node_route_addrs) != 1:
507+
self.logger.error("HappyNodeRoute: there should be only one route for {}, given {}".format(node_id, len(node_route_addrs)))
508+
return node_route_addrs[0]
509+
510+
493511
def run(self):
494512
# query node's route ip in v4 or v6 format.
495513
if not self.add and not self.delete:
496-
node_route_prefix = self.getNodeRoutePrefix(self.route_type, self.node_id)
497-
node_route_via_id = self.getNodeRouteVia(self.route_type, self.node_id)
498-
node_route_addrs = self.getNodeAddressesOnPrefix(node_route_prefix, node_route_via_id)
499-
514+
route_ip = self.getNodeRoute(self.node_id, self.route_type)
500515
emsg = "virtual node: {}, route_type: {}, route ip: {}".format(
501-
self.node_id, self.route_type, node_route_addrs)
516+
self.node_id, self.route_type, route_ip)
502517
print emsg
503518
else:
504519
self.__pre_check()

happy/State.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ def getNodeInterfaceAddresses(self, interface_id, node_id=None, state=None):
181181
return []
182182
return node_interface["ip"].keys()
183183

184+
def getNodeAddrMatchingPrefix(self, node_id, interface, prefix):
185+
"""Each interface may have multiple addresses, this function will get addresses based on interface and prefix.
186+
187+
Args:
188+
node_id (str): A string containing the node_id, example: "cloud", "BorderRouter"
189+
interface (str): A string containing interface id, example: "wpan0", "wlan0"
190+
prefix (str): A string containing an IP prefix, example: "10.0.1"
191+
192+
Returns:
193+
addresses_matchPrefix: A string list containing IP addresses
194+
"""
195+
addresses = self.getNodeInterfaceAddresses(interface, node_id)
196+
addresses_matchPrefix = []
197+
for address in addresses:
198+
if IP.prefixMatchAddress(prefix, address):
199+
addresses_matchPrefix.append(address)
200+
return addresses_matchPrefix
201+
184202
def getNodeInterfaceAddressInfo(self, interface_id, addr, node_id=None, state=None):
185203
node_interface = self.getNodeInterface(interface_id, node_id, state)
186204
if node_interface == {}:

0 commit comments

Comments
 (0)