From 72b7b6c157c643549006fea1eb4f7348581cb9a4 Mon Sep 17 00:00:00 2001 From: Yaron Yogev Date: Thu, 10 Aug 2017 15:42:41 +0300 Subject: US2853 add switch-switch links Change-Id: I5e27d33f32dc1dfe930cb692cd98604cf437855b Signed-off-by: Yaron Yogev --- app/discover/find_links.py | 11 +++-- app/discover/find_links_for_instance_vnics.py | 3 +- app/discover/find_links_for_oteps.py | 15 +++--- app/discover/find_links_for_pnics.py | 70 ++++++++++++++++++--------- app/discover/find_links_for_vconnectors.py | 8 +-- app/discover/find_links_for_vedges.py | 20 +++++--- app/discover/find_links_for_vservice_vnics.py | 3 +- app/utils/inventory_mgr.py | 11 +++-- 8 files changed, 94 insertions(+), 47 deletions(-) (limited to 'app') diff --git a/app/discover/find_links.py b/app/discover/find_links.py index 0967a60..d234479 100644 --- a/app/discover/find_links.py +++ b/app/discover/find_links.py @@ -16,15 +16,20 @@ class FindLinks(Fetcher): super().__init__() self.inv = InventoryMgr() - def create_link(self, env, host, source, source_id, target, target_id, + def create_link(self, env, source, source_id, target, target_id, link_type, link_name, state, link_weight, - source_label="", target_label="", + host=None, switch=None, extra_attributes=None): if extra_attributes is None: extra_attributes = {} - link = self.inv.create_link(env, host, + source_label = extra_attributes.get('source_label', '') + target_label = extra_attributes.get('target_label', '') + link = self.inv.create_link(env, source, source_id, target, target_id, link_type, link_name, state, link_weight, + source_label=source_label, + target_label=target_label, + host=host, switch=switch, extra_attributes=extra_attributes) if self.inv.monitoring_setup_manager: self.inv.monitoring_setup_manager.create_setup(link) diff --git a/app/discover/find_links_for_instance_vnics.py b/app/discover/find_links_for_instance_vnics.py index 7e081fc..55f7bfc 100644 --- a/app/discover/find_links_for_instance_vnics.py +++ b/app/discover/find_links_for_instance_vnics.py @@ -53,7 +53,8 @@ class FindLinksForInstanceVnics(FindLinks): state = "up" # TBD link_weight = 0 # TBD attributes = {} if not network_id else {'network': network_id} - self.create_link(self.get_env(), host["name"], + self.create_link(self.get_env(), source, source_id, target, target_id, link_type, network_name, state, link_weight, + host=host["name"], extra_attributes=attributes) diff --git a/app/discover/find_links_for_oteps.py b/app/discover/find_links_for_oteps.py index 84373a4..a9cc149 100644 --- a/app/discover/find_links_for_oteps.py +++ b/app/discover/find_links_for_oteps.py @@ -36,9 +36,10 @@ class FindLinksForOteps(FindLinks): link_name = vedge["name"] + "-otep" state = "up" # TBD link_weight = 0 # TBD - self.create_link(self.get_env(), vedge["host"], + self.create_link(self.get_env(), source, source_id, target, target_id, - link_type, link_name, state, link_weight) + link_type, link_name, state, link_weight, + host=vedge["host"]) def add_otep_vconnector_link(self, otep): if "vconnector" not in otep: @@ -59,9 +60,10 @@ class FindLinksForOteps(FindLinks): link_name = otep["name"] + "-" + otep["vconnector"] state = "up" # TBD link_weight = 0 # TBD - self.create_link(self.get_env(), otep["host"], + self.create_link(self.get_env(), source, source_id, target, target_id, - link_type, link_name, state, link_weight) + link_type, link_name, state, link_weight, + host=otep["host"]) def add_otep_pnic_link(self, otep): pnic = self.inv.find_items({ @@ -80,6 +82,7 @@ class FindLinksForOteps(FindLinks): link_name = otep["host"] + "pnic" + pnic["name"] state = "up" # TBD link_weight = 0 # TBD - self.create_link(self.get_env(), otep["host"], + self.create_link(self.get_env(), source, source_id, target, target_id, - link_type, link_name, state, link_weight) + link_type, link_name, state, link_weight, + host=otep["host"]) diff --git a/app/discover/find_links_for_pnics.py b/app/discover/find_links_for_pnics.py index 717c1c1..268aa55 100644 --- a/app/discover/find_links_for_pnics.py +++ b/app/discover/find_links_for_pnics.py @@ -7,6 +7,8 @@ # which accompanies this distribution, and is available at # # http://www.apache.org/licenses/LICENSE-2.0 # ############################################################################### +import re + from discover.find_links import FindLinks @@ -20,12 +22,21 @@ class FindLinksForPnics(FindLinks): "type": "pnic", "pnic_type": "host" }) + self.log.info("adding links of type: pnic-network, host-switch") for pnic in pnics: self.add_pnic_network_links(pnic) self.add_host_pnic_to_switch_pnic_link(pnic) + pnics = self.inv.find_items({ + "environment": self.get_env(), + "type": "pnic", + "pnic_type": "switch", + "role": "uplink" + }) + self.log.info("adding links of type: switch-switch") + for pnic in pnics: + self.add_switch_to_switch_link(pnic) def add_pnic_network_links(self, pnic): - self.log.info("adding links of type: pnic-network") host = pnic["host"] # find ports for that host, and fetch just the network ID ports = self.inv.find_items({ @@ -38,7 +49,7 @@ class FindLinksForPnics(FindLinks): networks[port["network_id"]] = 1 for network_id in networks.keys(): network = self.inv.get_by_id(self.get_env(), network_id) - if network == []: + if not network: return source = pnic["_id"] source_id = pnic["id"] @@ -50,38 +61,53 @@ class FindLinksForPnics(FindLinks): else "Segment-None" state = "up" if pnic["Link detected"] == "yes" else "down" link_weight = 0 # TBD - source_label = "port-" + pnic["port_id"] if "port_id" in pnic \ - else "" - self.create_link(self.get_env(), host, + attributes={"network": target_id} + if "port_id" in pnic: + attributes['source_label'] = "port-" + pnic["port_id"] + self.create_link(self.get_env(), source, source_id, target, target_id, link_type, link_name, state, link_weight, - source_label, - extra_attributes={"network": target_id}) + host=host, + extra_attributes=attributes) def add_host_pnic_to_switch_pnic_link(self, host_pnic): - link_type = "host_pnic-switch_pnic" - self.log.info("adding links of type: {}".format(link_type)) - # find ports for that host, and fetch just the network ID - switch_pnics = self.inv.find_items({ + switch_pnic = self.inv.find_items({ "environment": self.get_env(), "type": "pnic", "pnic_type": "switch", - "mac_address": host_pnic["mac_address"] - }, {"id": 1}) - if not switch_pnics: + "mac_address": host_pnic["mac_address"]}, + get_single=True) + if not switch_pnic: return - if len(switch_pnics) > 1: - self.log.warn("multiple matching switch pNICs found " - "for host pNIC: mac_address={}" - .format(host_pnic["mac_address"])) - switch_pnic = switch_pnics[0] source = host_pnic["_id"] source_id = host_pnic["id"] target = switch_pnic["_id"] target_id = switch_pnic["id"] - link_name = "{}-{}".format(target_id, source_id) + link_type = "host-switch" + link_name = "{}-{}".format(host_pnic['host'], + switch_pnic['parent_id']) state = "up" if host_pnic["Link detected"] == "yes" else "down" link_weight = 0 # TBD - self.create_link(self.get_env(), host_pnic['host'], + self.create_link(self.get_env(), + source, source_id, target, target_id, + link_type, link_name, state, link_weight, + host=host_pnic['host']) + + def add_switch_to_switch_link(self, leaf_pnic): + spine_pnic = self.inv.get_by_id(self.get_env(), + leaf_pnic['connected_to']) + if not spine_pnic: + return + source = leaf_pnic["_id"] + source_id = leaf_pnic["id"] + target = spine_pnic["_id"] + target_id = spine_pnic["id"] + link_type = "switch-switch" + if_id_matches = re.search("(eth.*)$", source_id) + link_name = if_id_matches.group(1).replace("__", "/") + state = "up" # TBD + link_weight = 0 # TBD + self.create_link(self.get_env(), source, source_id, target, target_id, - link_type, link_name, state, link_weight) \ No newline at end of file + link_type, link_name, state, link_weight, + switch=leaf_pnic['switch']) \ No newline at end of file diff --git a/app/discover/find_links_for_vconnectors.py b/app/discover/find_links_for_vconnectors.py index 3d5cdb0..855a491 100644 --- a/app/discover/find_links_for_vconnectors.py +++ b/app/discover/find_links_for_vconnectors.py @@ -56,9 +56,10 @@ class FindLinksForVconnectors(FindLinks): attributes = {'network': vnic['network']} vconnector['network'] = vnic['network'] self.inv.set(vconnector) - self.create_link(self.get_env(), host, + self.create_link(self.get_env(), source, source_id, target, target_id, link_type, link_name, state, link_weight, + host=host, extra_attributes=attributes) def add_vconnector_pnic_link(self, vconnector, interface): @@ -82,7 +83,8 @@ class FindLinksForVconnectors(FindLinks): link_name = pnic["name"] state = "up" # TBD link_weight = 0 # TBD - self.create_link(self.get_env(), host, + self.create_link(self.get_env(), source, source_id, target, target_id, - link_type, link_name, state, link_weight) + link_type, link_name, state, link_weight, + host=host) diff --git a/app/discover/find_links_for_vedges.py b/app/discover/find_links_for_vedges.py index 1235074..f452d97 100644 --- a/app/discover/find_links_for_vedges.py +++ b/app/discover/find_links_for_vedges.py @@ -45,10 +45,12 @@ class FindLinksForVedges(FindLinks): link_weight = 0 # TBD source_label = vnic["mac_address"] target_label = port["id"] - self.create_link(self.get_env(), vedge["host"], + self.create_link(self.get_env(), source, source_id, target, target_id, link_type, link_name, state, link_weight, - source_label, target_label) + host=vedge["host"], + extra_attributes={"source_label": source_label, + "target_label": target_label}) def find_matching_vconnector(self, vedge, port): if self.configuration.has_network_plugin('VPP'): @@ -79,7 +81,8 @@ class FindLinksForVedges(FindLinks): source_label = vconnector_interface_name target_label = port["name"] mac_address = "Unknown" - attributes = {'mac_address': mac_address} + attributes = {'mac_address': mac_address, 'source_label': source_label, + 'target_label': target_label} for interface in vconnector['interfaces'].values(): if vconnector_interface_name != interface['name']: continue @@ -90,11 +93,11 @@ class FindLinksForVedges(FindLinks): break if 'network' in vconnector: attributes['network'] = vconnector['network'] - self.create_link(self.get_env(), vedge["host"], + self.create_link(self.get_env(), source, source_id, target, target_id, link_type, link_name, state, link_weight, - source_label, target_label, - attributes) + host=vedge["host"], + extra_attributes=attributes) def find_matching_pnic(self, vedge, port): pname = port["name"] @@ -119,6 +122,7 @@ class FindLinksForVedges(FindLinks): link_name = "Port-" + port["id"] state = "up" if pnic["Link detected"] == "yes" else "down" link_weight = 0 # TBD - self.create_link(self.get_env(), vedge["host"], + self.create_link(self.get_env(), source, source_id, target, target_id, - link_type, link_name, state, link_weight) + link_type, link_name, state, link_weight, + host=vedge["host"]) diff --git a/app/discover/find_links_for_vservice_vnics.py b/app/discover/find_links_for_vservice_vnics.py index e8a91c8..3b8f32d 100644 --- a/app/discover/find_links_for_vservice_vnics.py +++ b/app/discover/find_links_for_vservice_vnics.py @@ -49,8 +49,9 @@ class FindLinksForVserviceVnics(FindLinks): link_name = network["name"] state = "up" # TBD link_weight = 0 # TBD - self.create_link(self.get_env(), v["host"], + self.create_link(self.get_env(), source, source_id, target, target_id, link_type, link_name, state, link_weight, + host=v["host"], extra_attributes={'network': v['network']}) diff --git a/app/utils/inventory_mgr.py b/app/utils/inventory_mgr.py index 2fe2894..257b0e3 100644 --- a/app/utils/inventory_mgr.py +++ b/app/utils/inventory_mgr.py @@ -259,15 +259,15 @@ class InventoryMgr(MongoAccess, metaclass=Singleton): # state: up/down # link_weight: integer, position/priority for graph placement # source_label, target_label: labels for the ends of the link (optional) - def create_link(self, env, host, src, source_id, target, target_id, + def create_link(self, env, src, source_id, target, target_id, link_type, link_name, state, link_weight, source_label="", target_label="", + host=None, switch=None, extra_attributes=None): s = bson.ObjectId(src) t = bson.ObjectId(target) link = { "environment": env, - "host": host, "source": s, "source_id": source_id, "target": t, @@ -280,6 +280,10 @@ class InventoryMgr(MongoAccess, metaclass=Singleton): "target_label": target_label, "attributes": extra_attributes if extra_attributes else {} } + if host: + link['host'] = host + if switch: + link['switch'] = switch return self.write_link(link) def write_link(self, link): @@ -359,7 +363,8 @@ class InventoryMgr(MongoAccess, metaclass=Singleton): return features_in_env.get(feature.value) is True def save_inventory_object(self, o: dict, parent: dict, - environment: str, type_to_fetch: dict = None) -> bool: + environment: str, type_to_fetch: dict = None) \ + -> bool: if not type_to_fetch: type_to_fetch = {} -- cgit 1.2.3-korg