def update_defaults(self, data): self._validate_defaults(data) # update default switch attributes for item in data[SWITCHES]: try: switch = Switch.objects.get(topology_id=self._top.id, dummy=True, tier=item[TIER]) except Switch.DoesNotExist: # if tier default does not exist, create one 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() # update default link attributes for item in data[LINKS]: link = Link.objects.get( topology_id=self._top.id, dummy=True, src_ports=item[SRC_TIER], dst_ports=item[DST_TIER] ) link.link_type = item[LINK_TYPE] link.num_links = item[NUM_LINKS] link.save()
def update_defaults(self, data): self._validate_defaults(data) # update default switch attributes for item in data[SWITCHES]: try: switch = Switch.objects.get(topology_id=self._top.id, dummy=True, tier=item[TIER]) except Switch.DoesNotExist: # if tier default does not exist, create one 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() # update default link attributes for item in data[LINKS]: link = Link.objects.get(topology_id=self._top.id, dummy=True, src_ports=item[SRC_TIER], dst_ports=item[DST_TIER]) link.link_type = item[LINK_TYPE] link.num_links = item[NUM_LINKS] link.save()
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 get_default_discovery_switch(fab_id, tier): logger.debug("creating default switch") switch_default = Switch() switch_default.topology_id = fab_id switch_default.dummy = True switch_default.name = DEFAULT switch_default.tier = tier switch_default.model = get_switch(1) switch_default.image_profile = image.get_profile(1) switch_default.save() logger.debug("default switch created") return switch_default
def post(self, request, format=None): serializer = SwitchSerializer(data=request.data) if serializer.is_valid(): swi_object = Switch() swi_object.model = serializer.data['model'] swi_object.name = serializer.data['name'] swi_object.image = serializer.data['image'] swi_object.slots = serializer.data['slots'] swi_object.tier = serializer.data['tier'] swi_object.line_cards = str(serializer.data['line_cards']) swi_object.save() serializer = SwitchGetSerializer(swi_object) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def _add_switch(self, tier, index=0): if not index: index = self._get_next_index(tier) # create new switch switch = Switch() switch.topology = self._top switch.dummy = False switch.tier = tier # get switch model from tier default default = Switch.objects.get(topology_id=self._top.id, tier=tier, dummy=True) switch.model = default.model # new switch name = fabric prefix (if any) + tier + index if self._top.is_fabric: switch.name = self._top.name + "_" + tier + str(index) else: switch.name = tier + str(index) switch.save() self._add_auto_links(switch)
def SaveSwitch(lst): try: logok = '' logero = '' first_sheet = lst.sheet_by_index(0) for r in range(1, first_sheet.nrows): m = ServerInfo.objects.get(IP=first_sheet.cell(r, 4).value).id chk = Switch.objects.filter(Server_id=m).exists() if chk == False: sv = Switch( SwitchIP=first_sheet.cell(r, 0).value, SwitchType=first_sheet.cell(r, 1).value, SwitchPort=first_sheet.cell(r, 2).value, SwitchLocation=first_sheet.cell(r, 3).value, Server_id=m, ) sv.save() logok += first_sheet.cell(r, 4).value.encode("utf-8") + '/' elif chk == True: logero += first_sheet.cell(r, 4).value.encode("utf-8") + '/' return {'y0': logok, 'y1': logero} except: return False
def add_discovered_switches(fab_id, tiers, switch_with_ip): from bootstrap import bootstrap logger.debug("Adding discovered switches in fabric") switch_id_details = dict() switch_id_details[SPINE] = dict() switch_id_details[LEAF] = dict() switch_id_details[CORE] = dict() switch_id_details[BORDER] = dict() for spine_switch in tiers[SPINE]: logger.debug("Adding spine switch:%s in fabric", spine_switch[0]) switch = Switch() switch.tier = SPINE switch.name = spine_switch[0] switch.dummy = False switch.serial_num = spine_switch[1] find_duplicate([spine_switch[1]]) switch.topology_id = fab_id switch.mgmt_ip = switch_with_ip[spine_switch[0]] switch.model = SwitchModel.objects.get(id=1) switch.save() bootstrap.update_boot_detail(switch, match_type=DISCOVERY, discovery_rule=None, boot_time=timezone.now(), boot_status=BOOT_SUCCESS, model_type='') logger.debug("Spine added") switch_id_details[SPINE][spine_switch[0]] = switch.id for leaf_switch in tiers[LEAF]: logger.debug("Adding leaf switch:%s to fabric", leaf_switch[0]) switch = Switch() switch.tier = LEAF switch.name = leaf_switch[0] switch.serial_num = leaf_switch[1] find_duplicate([leaf_switch[1]]) switch.topology_id = fab_id switch.mgmt_ip = switch_with_ip[leaf_switch[0]] switch.model = SwitchModel.objects.get(id=1) switch.save() bootstrap.update_boot_detail(switch, match_type=DISCOVERY, discovery_rule=None, boot_time=timezone.now(), boot_status=BOOT_SUCCESS, model_type='') logger.debug("Leaf added") switch_id_details[LEAF][leaf_switch[0]] = switch.id for core_switch in tiers[CORE]: logger.debug("Adding core switch:%s in fabric", core_switch) switch = Switch() switch.tier = CORE switch.name = core_switch switch.dummy = False switch.topology_id = fab_id switch.model = SwitchModel.objects.get(id=1) switch.save() bootstrap.update_boot_detail(switch, match_type=DISCOVERY, discovery_rule=None, boot_time=timezone.now(), boot_status=BOOT_SUCCESS, model_type='') logger.debug("core added") switch_id_details[CORE][core_switch] = switch.id for border_switch in tiers[BORDER]: logger.debug("Adding border switch:%s in fabric", border_switch) switch = Switch() switch.tier = BORDER switch.name = border_switch switch.dummy = False switch.topology_id = fab_id switch.model = SwitchModel.objects.get(id=1) switch.save() bootstrap.update_boot_detail(switch, match_type=DISCOVERY, discovery_rule=None, boot_time=timezone.now(), boot_status=BOOT_SUCCESS, model_type='') logger.debug("Border added") switch_id_details[BORDER][border_switch] = switch.id return switch_id_details