def get_vpc_peer_ports(self, switch): ports = [] logger.debug("Get VPC ports for switch %s" % switch.name) try: link = Link.objects.get(topology_id=switch.topology.id, src_switch=switch.id, dst_switch__tier=switch.tier, link_type=VPC_PEER, dummy=False) logger.debug("VPC ports %s" % str(link.src_ports)) return string_to_ports(link.src_ports) except Link.DoesNotExist: try: link = Link.objects.get(topology_id=switch.topology.id, dst_switch=switch.id, src_switch__tier=switch.tier, link_type=VPC_PEER, dummy=False) logger.debug("VPC ports %s" % str(link.dst_ports)) return string_to_ports(link.dst_ports) except Link.DoesNotExist: logger.debug("%s- %s" % (ERR_NO_VPC_PEER_PORTS, switch.name)) raise IgniteException("%s- %s" % (ERR_NO_VPC_PEER_PORTS, switch.name))
def get_vpc_peer_ports(self, switch): ports = [] logger.debug("Get VPC ports for switch %s" % switch.name) try: link = Link.objects.get( topology_id=switch.topology.id, src_switch=switch.id, dst_switch__tier=switch.tier, link_type=VPC_PEER, dummy=False, ) logger.debug("VPC ports %s" % str(link.src_ports)) return string_to_ports(link.src_ports) except Link.DoesNotExist: try: link = Link.objects.get( topology_id=switch.topology.id, dst_switch=switch.id, src_switch__tier=switch.tier, link_type=VPC_PEER, dummy=False, ) logger.debug("VPC ports %s" % str(link.dst_ports)) return string_to_ports(link.dst_ports) except Link.DoesNotExist: logger.debug("%s- %s" % (ERR_NO_VPC_PEER_PORTS, switch.name)) raise IgniteException("%s- %s" % (ERR_NO_VPC_PEER_PORTS, switch.name))
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_link(self, link_id, data): src_ports = string_to_ports(data[SRC_PORTS]) dst_ports = string_to_ports(data[DST_PORTS]) # check that correct # of ports were given if len(src_ports) != data[NUM_LINKS] or len(dst_ports) != data[NUM_LINKS]: raise IgniteException(ERR_INV_PORT_COUNT) # delete current link link = Link.objects.get(pk=link_id) src_switch = link.src_switch dst_switch = link.dst_switch self.delete_link(link_id, True) # add new link with new values self._add_link(src_switch, dst_switch, data[LINK_TYPE], data[NUM_LINKS], src_ports, dst_ports)
def delete_link(self, link_id): link = Link.objects.get(pk=link_id) # delete physical links created for a logical link if link.num_links > 1 or link.link_type in [PORT_CHANNEL, VPC_PEER]: src_ports = string_to_ports(link.src_ports) dst_ports = string_to_ports(link.dst_ports) for index in range(link.num_links): Link.objects.filter(src_switch_id=link.src_switch.id, dst_switch_id=link.dst_switch.id, src_ports=src_ports[index], dst_ports=dst_ports[index]).delete() # delete the logical link link.delete()
def delete_link(self, link_id): link = Link.objects.get(pk=link_id) # delete physical links created for a logical link if link.num_links > 1 or link.link_type in [PORT_CHANNEL, VPC_PEER]: src_ports = string_to_ports(link.src_ports) dst_ports = string_to_ports(link.dst_ports) for index in range(link.num_links): Link.objects.filter( src_switch_id=link.src_switch.id, dst_switch_id=link.dst_switch.id, src_ports=src_ports[index], dst_ports=dst_ports[index], ).delete() # delete the logical link link.delete()
def update_link(self, link_id, data): src_ports = string_to_ports(data[SRC_PORTS]) dst_ports = string_to_ports(data[DST_PORTS]) # check that correct # of ports were given if (len(src_ports) != data[NUM_LINKS] or len(dst_ports) != data[NUM_LINKS]): raise IgniteException(ERR_INV_PORT_COUNT) # delete current link link = Link.objects.get(pk=link_id) src_switch = link.src_switch dst_switch = link.dst_switch self.delete_link(link_id, True) # add new link with new values self._add_link(src_switch, dst_switch, data[LINK_TYPE], data[NUM_LINKS], src_ports, dst_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 _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 createAnkJsonData(data, cfg): #Prepare ank json format from poap json ank_data = {"directed":"false", "graph":[], "profiles":[], "links":[], "multigraph":"false", "nodes":[] } ank_data['profiles'] = [] switchid_to_name = {} if data.has_key('switches'): for switch in data['switches']: node = {} ports = [] node['asn'] = 1 node['device_type'] = "router" #ANK picks it as router. Not sure why. if switch['tier'] == 'Core': node_type = 'core' #this might be needed later to identify type of node elif switch['tier'] == 'Spine': node_type = 'spine' elif switch['tier'] == 'Leaf': node_type = 'leaf' elif switch['tier'] == 'Border': node_type = 'border' if switch.has_key('feature_profile'): node['profile'] = switch['feature_profile'] else: log.debug("feature_profile not specified for switch") return None #we are using 'name' as 'id' since that is used by ank in all the descriptions #while generating config. node['devsubtype'] = node_type if switch.has_key('name'): node['id']= switch['name'] else: log.debug("Name not specified for switch") return None #we are using 'id' as name since we need to create config files with 'id'. if switch.has_key('id'): node['name']= switch['id'] switchid_to_name[switch['id']] = switch['name'] else: log.debug("id not specified for switch") return None points = (newpoint() for x in xrange(100)) for x,y in points: node['x'] = x node['y'] = y break ank_data["nodes"].append(node) if ank_data['nodes'] is None: return ank_data for node in ank_data["nodes"]: node["ports"] = [] #generate_pc_cfg = False #if (cfg['device_profile'].has_key('pc_enabled') and # cfg['device_profile']['pc_enabled'] == True): # generate_pc_cfg = True #iterate thorugh the link_list and add them to links if data.has_key('links'): for links in data["links"]: links['src_ports'] = string_to_ports(links['src_ports']) links['dst_ports'] = string_to_ports(links['dst_ports']) #process PC seperately #if ((generate_pc_config == True) and if ((re.search(VPC_LINK_TYPE , links['link_type'])) or (re.search(PC_LINK_TYPE , links['link_type'])) or (re.search(VPC_MEMBERLINK_TYPE , links['link_type']))): configure_PC_VPC(links, ank_data, switchid_to_name) continue src_node_type = dest_node_type = None for ank_nodes in ank_data["nodes"]: if ank_nodes['id'] == switchid_to_name[links['src_switch']]: src_node = ank_nodes src_node_type = ank_nodes['devsubtype'] if ank_nodes['id'] == switchid_to_name[links['dst_switch']]: dest_node = ank_nodes dest_node_type = ank_nodes['devsubtype'] if src_node_type is not None and dest_node_type is not None: break for port_list1, port_list2 in zip(links["src_ports"], links["dst_ports"]): link = {} link["src"] = switchid_to_name[links['src_switch']] link["dst"] = switchid_to_name[links['dst_switch']] link["src_port"] = port_list1 link["dst_port"] = port_list2 link["link_type"] = links['link_type'] #SHARAD: for not generating ip address #link["link_type"] = 'is_not_l3' ank_data["links"].append(link) #In POAP json file src and dst ports are missing in node port lists. If its not available, update them in node[ports] list _p = {} _p['id'] = port_list1 _p['category'] = "physical" _p['subcategory'] = "physical" _p['description'] = "" _p['connected_to'] = dest_node_type #_p['ipv4_address'] = "11.0.0.8" #_p['ipv4_prefixlen'] = 30 src_node['ports'].append(_p) _p = {} _p['id'] = port_list2 _p['category'] = "physical" _p['subcategory'] = "physical" _p['description'] = "" _p['connected_to'] = src_node_type #_p['ipv4_address'] = "11.0.0.9" #_p['ipv4_prefixlen'] = 30 dest_node['ports'].append(_p) #print "completed all configs" return ank_data
def createAnkJsonData(data, cfg): #Prepare ank json format from poap json ank_data = { "directed": "false", "graph": [], "profiles": [], "links": [], "multigraph": "false", "nodes": [] } ank_data['profiles'] = [] switchid_to_name = {} if data.has_key('switches'): for switch in data['switches']: node = {} ports = [] node['asn'] = 1 node[ 'device_type'] = "router" #ANK picks it as router. Not sure why. if switch['tier'] == 'Core': node_type = 'core' #this might be needed later to identify type of node elif switch['tier'] == 'Spine': node_type = 'spine' elif switch['tier'] == 'Leaf': node_type = 'leaf' elif switch['tier'] == 'Border': node_type = 'border' if switch.has_key('feature_profile'): node['profile'] = switch['feature_profile'] else: log.debug("feature_profile not specified for switch") return None #we are using 'name' as 'id' since that is used by ank in all the descriptions #while generating config. node['devsubtype'] = node_type if switch.has_key('name'): node['id'] = switch['name'] else: log.debug("Name not specified for switch") return None #we are using 'id' as name since we need to create config files with 'id'. if switch.has_key('id'): node['name'] = switch['id'] switchid_to_name[switch['id']] = switch['name'] else: log.debug("id not specified for switch") return None points = (newpoint() for x in xrange(100)) for x, y in points: node['x'] = x node['y'] = y break ank_data["nodes"].append(node) if ank_data['nodes'] is None: return ank_data for node in ank_data["nodes"]: node["ports"] = [] #generate_pc_cfg = False #if (cfg['device_profile'].has_key('pc_enabled') and # cfg['device_profile']['pc_enabled'] == True): # generate_pc_cfg = True #iterate thorugh the link_list and add them to links if data.has_key('links'): for links in data["links"]: links['src_ports'] = string_to_ports(links['src_ports']) links['dst_ports'] = string_to_ports(links['dst_ports']) #process PC seperately #if ((generate_pc_config == True) and if ((re.search(VPC_LINK_TYPE, links['link_type'])) or (re.search(PC_LINK_TYPE, links['link_type'])) or (re.search(VPC_MEMBERLINK_TYPE, links['link_type']))): configure_PC_VPC(links, ank_data, switchid_to_name) continue src_node_type = dest_node_type = None for ank_nodes in ank_data["nodes"]: if ank_nodes['id'] == switchid_to_name[links['src_switch']]: src_node = ank_nodes src_node_type = ank_nodes['devsubtype'] if ank_nodes['id'] == switchid_to_name[links['dst_switch']]: dest_node = ank_nodes dest_node_type = ank_nodes['devsubtype'] if src_node_type is not None and dest_node_type is not None: break for port_list1, port_list2 in zip(links["src_ports"], links["dst_ports"]): link = {} link["src"] = switchid_to_name[links['src_switch']] link["dst"] = switchid_to_name[links['dst_switch']] link["src_port"] = port_list1 link["dst_port"] = port_list2 link["link_type"] = links['link_type'] #SHARAD: for not generating ip address #link["link_type"] = 'is_not_l3' ank_data["links"].append(link) #In POAP json file src and dst ports are missing in node port lists. If its not available, update them in node[ports] list _p = {} _p['id'] = port_list1 _p['category'] = "physical" _p['subcategory'] = "physical" _p['description'] = "" _p['connected_to'] = dest_node_type #_p['ipv4_address'] = "11.0.0.8" #_p['ipv4_prefixlen'] = 30 src_node['ports'].append(_p) _p = {} _p['id'] = port_list2 _p['category'] = "physical" _p['subcategory'] = "physical" _p['description'] = "" _p['connected_to'] = src_node_type #_p['ipv4_address'] = "11.0.0.9" #_p['ipv4_prefixlen'] = 30 dest_node['ports'].append(_p) #print "completed all configs" return ank_data