def add_topology(self, data, user): logger.debug("topology name = %s, model = %s", data[NAME], data[MODEL_NAME]) self._validate_defaults(data[DEFAULTS]) self._top = Topology() self._top.name = data[NAME] self._top.model_name = data[MODEL_NAME] self._top.is_fabric = False self._top.updated_by = user self._top.save() # defaults object self._top.defaults = dict() self._top.defaults[SWITCHES] = list() self._top.defaults[LINKS] = list() # add topology default switches with dummy=True for item in data[DEFAULTS][SWITCHES]: if item[MODEL] == 1: raise IgniteException(ERR_CAN_NOT_ASSIGN_UNKOWN_MODEL) switch = Switch() switch.topology = self._top switch.dummy = True switch.name = DEFAULT switch.tier = item[TIER] switch.model = get_switch(item[MODEL]) switch.image_profile = get_profile(item[IMAGE_PROFILE]) switch.save() # add switch to topology defaults self._top.defaults[SWITCHES].append(switch) # add topology default links with dummy=True for item in data[DEFAULTS][LINKS]: link = Link() link.topology = self._top link.dummy = True link.src_ports = item[SRC_TIER] # store tier name in ports link.dst_ports = item[DST_TIER] # store tier name in ports link.link_type = item[LINK_TYPE] link.num_links = item[NUM_LINKS] link.save() # need tier names in link object while returning link.src_tier = link.src_ports link.dst_tier = link.dst_ports # add link to topology defaults self._top.defaults[LINKS].append(link) # new topology has no switches or links self._top.switches = list() self._top.links = list() return self._top
def post(self, request, format=None): serializer = TopologySerializer(data=request.data) me = RequestValidator(request.META) if serializer.is_valid(): topology_obj = Topology() topology_obj.user_id = me.user_is_exist().user_id topology_obj.name = request.data['name'] topology_obj.submit = request.data['submit'] topology_obj.topology_json = json.dumps( request.data['topology_json']) try: topology_obj.config_json = json.dumps( request.data['config_json']) except: topology_obj.config_json = json.dumps([]) try: topology_obj.defaults = json.dumps(request.data['defaults']) except: topology_obj.defaults = json.dumps({}) topology_obj.save() serializer = TopologyGetSerializer(topology_obj) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def clone_topology(topo_id, data, user=""): old_topology = Topology.objects.get(pk=topo_id) # create new fabric new_topology = Topology() new_topology.name = data[NAME] new_topology.model_name = old_topology.model_name new_topology.updated_by = user new_topology.is_fabric = False new_topology.save() # fabric defaults, switches & links objects new_topology.defaults = dict() new_topology.defaults[SWITCHES] = list() new_topology.defaults[LINKS] = list() new_topology.switches = list() new_topology.links = list() # map of topology switch id to fabric switch object # needed when creating fabric links switch_dict = dict() # duplicate all topology switches into fabric for switch in Switch.objects.filter( topology_id=old_topology.id).order_by(ID): # save id for later switch_id = switch.id switch.id = None switch.topology = new_topology switch.save() # store topology switch id to switch mapping switch_dict[switch_id] = switch if switch.dummy: new_topology.defaults[SWITCHES].append(switch) else: new_topology.switches.append(switch) # duplicate all topology links into fabric for link in Link.objects.filter(topology_id=old_topology.id): link.id = None link.topology = new_topology if link.src_switch: link.src_switch = switch_dict[link.src_switch.id] link.dst_switch = switch_dict[link.dst_switch.id] else: link.src_switch = None link.dst_switch = None link.save() if not link.dummy: new_topology.links.append(link) elif link.dummy and not link.src_switch: link.src_tier = link.src_ports link.dst_tier = link.dst_ports new_topology.defaults[LINKS].append(link) new_topology.save() return new_topology
def create(self, the_parsed_topology_xml): """ Create a topology model here. """ # Instance a list of model.Component classes that are all the instanced items from parsed xml. x = the_parsed_topology_xml componentXMLNameToComponent = { } #Dictionary mapss XML names to processes component objects so redundant processing is avoided components = [] for comp_xml_path in x.get_comp_type_file_header_dict(): file_path = os.environ['BUILD_ROOT'] + '/' + comp_xml_path print file_path processedXML = XmlComponentParser.XmlComponentParser(file_path) comp_name = processedXML.get_component().get_name() componentXMLNameToComponent[comp_name] = processedXML for instance in x.get_instances(): if instance.get_type() not in componentXMLNameToComponent.keys(): PRINT.info( "Component XML file type {} was not specified in the topology XML. Please specify the path using <import_component_type> tags." .format(instance.get_type())) else: instance.set_component_object( componentXMLNameToComponent[instance.get_type()]) components.append( Component.Component(instance.get_namespace(), instance.get_name(), instance.get_type(), xml_filename=x.get_xml_filename(), kind2=instance.get_kind())) #print "Assembly name: " + x.get_name(), x.get_base_id(), x.get_base_id_window() #for component in components: # print component.get_name(), component.get_base_id(), component.get_base_id_window() if self.__generate_new_IDS: # Iterate over all the model.Component classes and... instance_name_base_id_list = self.__compute_base_ids( x.get_base_id(), x.get_base_id_window(), x.get_instances(), x.get_xml_filename()) else: instance_name_base_id_list = [] # Iterate over all the model.Component classes and then... # Iterate over all the connection sources and assigne output ports to each Component.. # For each output port you want to assign the connect comment, target component namem, target port and type... # (Requires adding to the model.Port class ether members or a memeber called of type TargetConnection) for component in components: port_obj_list = [] for connection in x.get_connections(): if component.get_name() == connection.get_source()[0]: port = Port.Port(connection.get_source()[1], connection.get_source()[2], None, None, comment=connection.get_comment(), xml_filename=x.get_xml_filename) port.set_source_num(connection.get_source()[3]) port.set_target_comp(connection.get_target()[0]) port.set_target_port(connection.get_target()[1]) port.set_target_type(connection.get_target()[2]) port.set_target_num(connection.get_target()[3]) if connection.get_source()[1].startswith("i"): port.set_direction("input") else: port.set_direction("output") if connection.get_target()[1].startswith("i"): port.set_target_direction("input") else: port.set_target_direction("output") port_obj_list.append(port) component.set_ports(port_obj_list) # Instance a Topology class and give it namespace, comment and list of components. the_topology = Topology.Topology(x.get_namespace(), x.get_comment(), components, x.get_name(), instance_name_base_id_list, x.get_prepend_instance_name()) the_topology.set_instance_header_dict( x.get_comp_type_file_header_dict()) return the_topology