def _add_link_to_db(self, link_type, num_links, src_switch, dst_switch, src_ports, dst_ports): # create new link link = Link() link.topology = self._top link.dummy = False link.link_type = link_type link.num_links = num_links link.src_switch = src_switch link.dst_switch = dst_switch link.src_ports = ports_to_string(src_ports) link.dst_ports = ports_to_string(dst_ports) link.save() # create invidual physical links for matching with CDP data if num_links > 1 or link_type in [PORT_CHANNEL, VPC_PEER]: # set dummy = True for such links for index in range(num_links): link = Link() link.topology = self._top link.dummy = True link.link_type = PHYSICAL link.num_links = 1 link.src_switch = src_switch link.dst_switch = dst_switch link.src_ports = src_ports[index] link.dst_ports = dst_ports[index] link.save()
def _update_link_in_db(self, link, new_ports, is_src): old_src_ports = string_to_ports(link.src_ports) old_dst_ports = string_to_ports(link.dst_ports) # update ports in link if is_src: link.src_ports = ports_to_string(new_ports) else: link.dst_ports = ports_to_string(new_ports) link.save() if link.num_links > 1 or link.link_type in [PORT_CHANNEL, VPC_PEER]: # get member physical links for index in range(link.num_links): member = Link.objects.get( src_switch_id=link.src_switch.id, dst_switch_id=link.dst_switch.id, src_ports=old_src_ports[index], dst_ports=old_dst_ports[index], dummy=True, ) # update port if is_src: member.src_ports = new_ports[index] else: member.dst_ports = new_ports[index] member.save() index += 1
def _update_link_in_db(self, link, new_ports, is_src): old_src_ports = string_to_ports(link.src_ports) old_dst_ports = string_to_ports(link.dst_ports) # update ports in link if is_src: link.src_ports = ports_to_string(new_ports) else: link.dst_ports = ports_to_string(new_ports) link.save() if link.num_links > 1 or link.link_type in [PORT_CHANNEL, VPC_PEER]: # get member physical links for index in range(link.num_links): member = Link.objects.get(src_switch_id=link.src_switch.id, dst_switch_id=link.dst_switch.id, src_ports=old_src_ports[index], dst_ports=old_dst_ports[index], dummy=True) # update port if is_src: member.src_ports = new_ports[index] else: member.dst_ports = new_ports[index] member.save() index += 1
def _update_new_link_ports(self, links, ports, is_src): used_count = 0 for link in links: # ran out of ports? if used_count + link.num_links > len(ports): raise IgniteException(ERR_NOT_ENOUGH_PORTS) # actual ports to be used in link new_ports = ports[used_count : used_count + link.num_links] logger.debug( "link id = %d, %s, old_ports = %s, new ports = %s", link.id, "src" if is_src else "dst", link.src_ports if is_src else link.dst_ports, ports_to_string(new_ports), ) # modify link with new ports self._update_link_in_db(link, new_ports, is_src) used_count += link.num_links return used_count
def _get_ports(self, switch, port_role, num_ports, ports=[]): if switch.model_id == 1 or switch.model_id == None: raise IgniteException( "Cant not add switch as no switch model assigned") logger.debug("switch id = %d, port_role = %s", switch.id, port_role) # get all links for switch links = Link.objects.filter(Q(src_switch_id=switch.id) | Q(dst_switch_id=switch.id), topology_id=self._top.id, dummy=False) used_ports = list() # make list of ports currently in use for link in links: if switch.id == link.src_switch.id: used_ports += string_to_ports(link.src_ports) else: used_ports += string_to_ports(link.dst_ports) # make list of ports available for port_role in switch model # first ports with exact port_role and then role=BOTH # available ports = model ports - used ports model_ports = switch.model.meta[port_role] + switch.model.meta[BOTH] avail_ports = [port for port in model_ports if port not in used_ports] logger.debug("model_ports = %s", ports_to_string(model_ports)) logger.debug("used_ports = %s", ports_to_string(used_ports)) logger.debug("avail_ports = %s", ports_to_string(avail_ports)) # if ports are given, check that they are usable if ports: for port in ports: if port not in avail_ports: raise IgniteException(ERR_INV_PORTS) return ports if num_ports > len(avail_ports): raise IgniteException(ERR_NOT_ENOUGH_PORTS) return avail_ports[:num_ports]
def _get_ports(self, switch, port_role, num_ports, ports=[]): if switch.model_id == 1 or switch.model_id == None: raise IgniteException("Cant not add switch as no switch model assigned") logger.debug("switch id = %d, port_role = %s", switch.id, port_role) # get all links for switch links = Link.objects.filter( Q(src_switch_id=switch.id) | Q(dst_switch_id=switch.id), topology_id=self._top.id, dummy=False ) used_ports = list() # make list of ports currently in use for link in links: if switch.id == link.src_switch.id: used_ports += string_to_ports(link.src_ports) else: used_ports += string_to_ports(link.dst_ports) # make list of ports available for port_role in switch model # first ports with exact port_role and then role=BOTH # available ports = model ports - used ports model_ports = switch.model.meta[port_role] + switch.model.meta[BOTH] avail_ports = [port for port in model_ports if port not in used_ports] logger.debug("model_ports = %s", ports_to_string(model_ports)) logger.debug("used_ports = %s", ports_to_string(used_ports)) logger.debug("avail_ports = %s", ports_to_string(avail_ports)) # if ports are given, check that they are usable if ports: for port in ports: if port not in avail_ports: raise IgniteException(ERR_INV_PORTS) return ports if num_ports > len(avail_ports): raise IgniteException(ERR_NOT_ENOUGH_PORTS) return avail_ports[:num_ports]
def _update_new_link_ports(self, links, ports, is_src): used_count = 0 for link in links: # ran out of ports? if used_count + link.num_links > len(ports): raise IgniteException(ERR_NOT_ENOUGH_PORTS) # actual ports to be used in link new_ports = ports[used_count:used_count + link.num_links] logger.debug("link id = %d, %s, old_ports = %s, new ports = %s", link.id, "src" if is_src else "dst", link.src_ports if is_src else link.dst_ports, ports_to_string(new_ports)) # modify link with new ports self._update_link_in_db(link, new_ports, is_src) used_count += link.num_links return used_count
def add_discovered_links(fab_id, switch_id_details, connectivities, tiers): logger.debug( "Adding discovered links between switches in discovered fabric") ports_info = get_ports_info(connectivities, tiers[BORDER], tiers[CORE]) used_switch = list() for i in range(0, len(connectivities)): for j in range(0, len(connectivities[i])): ssw = connectivities[i][j].split(';')[0] dsw = connectivities[i][j].split(';')[2] if (ssw != dsw): if (ssw in switch_id_details[SPINE].keys() and \ dsw in switch_id_details[LEAF].keys()): logger.debug( "Source switch %s is Spine and Destination switch %s is Leaf", ssw, dsw) link_type = PHYSICAL src_switch = switch_id_details[SPINE][ssw] dst_switch = switch_id_details[LEAF][dsw] elif (ssw in switch_id_details[LEAF].keys() and \ dsw in switch_id_details[LEAF].keys()): logger.debug( "Source switch %s is Leaf and Destination switch %s is Leaf", ssw, dsw) link_type = VPC_PEER src_switch = switch_id_details[LEAF][ssw] dst_switch = switch_id_details[LEAF][dsw] elif (ssw in switch_id_details[SPINE].keys() and \ dsw in switch_id_details[CORE].keys()): logger.debug( "Source switch %s is Spine and Destination switch %s is CORE", ssw, dsw) link_type = PHYSICAL src_switch = switch_id_details[SPINE][ssw] dst_switch = switch_id_details[CORE][dsw] elif (ssw in switch_id_details[LEAF].keys() and \ dsw in switch_id_details[BORDER].keys()): logger.debug( "Source switch %s is LEAF and Destination switch %s is Border", ssw, dsw) link_type = PHYSICAL src_switch = switch_id_details[LEAF][ssw] dst_switch = switch_id_details[BORDER][dsw] else: logger.debug("No ssw/dsw found") continue temp_list = list() temp_list.append(src_switch) temp_list.append(dst_switch) if temp_list not in used_switch: used_switch.append(temp_list) for switches in ports_info: for neigh in ports_info[switches]: if switches == ssw and neigh == dsw: logger.debug( "Finding source ports and destination ports for link between switches %s and %s", ssw, dsw) src_ports_list = ports_info[switches][neigh][0] dst_ports_list = ports_info[switches][neigh][1] logger.debug("Ports are %s and %s", src_ports_list, dst_ports_list) link = Link() link.topology_id = fab_id link.dummy = False link.link_type = link_type link.num_links = 1 link.src_switch_id = src_switch link.dst_switch_id = dst_switch link.src_ports = ports_to_string(src_ports_list) link.dst_ports = ports_to_string(dst_ports_list) link.save() logger.debug("Link created")
def add_discovered_links(fab_id, switch_id_details, connectivities, tiers): logger.debug("Adding discovered links between switches in discovered fabric") ports_info = get_ports_info(connectivities, tiers[BORDER], tiers[CORE]) used_switch = list() for i in range(0, len(connectivities)): for j in range(0, len(connectivities[i])): ssw = connectivities[i][j].split(';')[0] dsw = connectivities[i][j].split(';')[2] if (ssw != dsw): if (ssw in switch_id_details[SPINE].keys() and \ dsw in switch_id_details[LEAF].keys()): logger.debug("Source switch %s is Spine and Destination switch %s is Leaf", ssw, dsw) link_type = PHYSICAL src_switch = switch_id_details[SPINE][ssw] dst_switch = switch_id_details[LEAF][dsw] elif (ssw in switch_id_details[LEAF].keys() and \ dsw in switch_id_details[LEAF].keys()): logger.debug("Source switch %s is Leaf and Destination switch %s is Leaf", ssw, dsw) link_type = VPC_PEER src_switch = switch_id_details[LEAF][ssw] dst_switch = switch_id_details[LEAF][dsw] elif (ssw in switch_id_details[SPINE].keys() and \ dsw in switch_id_details[CORE].keys()): logger.debug("Source switch %s is Spine and Destination switch %s is CORE", ssw, dsw) link_type = PHYSICAL src_switch = switch_id_details[SPINE][ssw] dst_switch = switch_id_details[CORE][dsw] elif (ssw in switch_id_details[LEAF].keys() and \ dsw in switch_id_details[BORDER].keys()): logger.debug("Source switch %s is LEAF and Destination switch %s is Border", ssw, dsw) link_type = PHYSICAL src_switch = switch_id_details[LEAF][ssw] dst_switch = switch_id_details[BORDER][dsw] else: logger.debug("No ssw/dsw found") continue temp_list = list() temp_list.append(src_switch) temp_list.append(dst_switch) if temp_list not in used_switch: used_switch.append(temp_list) for switches in ports_info: for neigh in ports_info[switches]: if switches == ssw and neigh == dsw: logger.debug("Finding source ports and destination ports for link between switches %s and %s", ssw, dsw) src_ports_list = ports_info[switches][neigh][0] dst_ports_list = ports_info[switches][neigh][1] logger.debug("Ports are %s and %s", src_ports_list, dst_ports_list) link = Link() link.topology_id = fab_id link.dummy = False link.link_type = link_type link.num_links = 1 link.src_switch_id = src_switch link.dst_switch_id = dst_switch link.src_ports = ports_to_string(src_ports_list) link.dst_ports = ports_to_string(dst_ports_list) link.save() logger.debug("Link created")