def update_profiles(fab_id, data, user): # setting global level config and feture profiles fabric = Fabric.objects.get(pk=fab_id) if data[CONFIG_PROFILE]: fabric.config_profile = config.get_profile(data[CONFIG_PROFILE]) else: fabric.config_profile = None if data[FEATURE_PROFILE]: fabric.feature_profile = feature.get_profile(data[FEATURE_PROFILE]) else: fabric.feature_profile = None fabric.save() cfg = dict() feat = dict() wf = dict() # store tier default config & feature profile for item in data[PROFILES]: # if profile id is zero, then store null # it means - do not apply config/feature/workflow for this tier if item[CONFIG_PROFILE]: cfg[item[TIER]] = config.get_profile(item[CONFIG_PROFILE]) else: cfg[item[TIER]] = None if item[FEATURE_PROFILE]: feat[item[TIER]] = feature.get_profile(item[FEATURE_PROFILE]) else: feat[item[TIER]] = None if item[WORKFLOW]: wf[item[TIER]] = workflow.get_workflow(item[WORKFLOW]) else: wf[item[TIER]] = None # update profiles in fabric default switches for switch in Switch.objects.filter(topology_id=fab_id, dummy=True): switch.config_profile = cfg[switch.tier] switch.feature_profile = feat[switch.tier] switch.workflow = wf[switch.tier] switch.save() Fabric.objects.filter(pk=fab_id).update(updated_by=user)
def save_discovered_fabric(fab_id, data, user): logger.debug("Start saving discovered Fabric") fabric = Fabric.objects.get(pk=fab_id) fabric.name = data[NAME] fabric.config_profile = config.get_profile(1) fabric.updated_by = user fabric.submit = True fabric.save() logger.debug("fabric name = %s", data[NAME]) cfg = dict() feat = dict() wf = dict() switch_dict = dict() for switch in Switch.objects.filter(topology_id=fab_id).order_by(ID): logger.debug("Looping through all switches of Discovered fabric- %s", data[NAME]) if switch.dummy: # set default config profile for fabric default switch logger.debug( "set default config profile for fabric default switch") switch.config_profile = config.get_profile(1) else: # append fabric name to switch name logger.debug( "Defining switch name by appending it to fabric name for switch-%s", switch.name) switch.name = fabric.name + "_" + switch.name # set Unknown model to switch in fabric logger.debug("set Unknown model to switch in fabric") switch.model = get_switch(1) logger.debug("Added Unknown model to switch in fabric") switch.save() fabric.is_saved = True fabric.save() logger.debug("Fabric saved") return fabric
def save_discovered_fabric(fab_id, data, user): logger.debug("Start saving discovered Fabric") fabric = Fabric.objects.get(pk=fab_id) fabric.name = data[NAME] fabric.config_profile = config.get_profile(1) fabric.updated_by = user fabric.submit = True fabric.save() logger.debug("fabric name = %s", data[NAME]) cfg = dict() feat = dict() wf = dict() switch_dict = dict() for switch in Switch.objects.filter(topology_id=fab_id).order_by(ID): logger.debug("Looping through all switches of Discovered fabric- %s", data[NAME]) if switch.dummy: # set default config profile for fabric default switch logger.debug("set default config profile for fabric default switch") switch.config_profile = config.get_profile(1) else: # append fabric name to switch name logger.debug("Defining switch name by appending it to fabric name for switch-%s", switch.name) switch.name = fabric.name + "_" + switch.name # set Unknown model to switch in fabric logger.debug("set Unknown model to switch in fabric") switch.model = get_switch(1) logger.debug("Added Unknown model to switch in fabric") switch.save() fabric.is_saved = True fabric.save() logger.debug("Fabric saved") return fabric
def update_switch(fab_id, switch_id, data, user): switch = Switch.objects.get(pk=switch_id) # check if switch is already booted if switch.boot_detail: if any([switch.name != data[NAME], switch.model.id != data[MODEL], switch.serial_num != data[SERIAL_NUM]]): raise IgniteException(ERR_NO_NAME_CHANGE) # check if switch already exists with new name # switch name is searched across all fabrics if (switch.name != data[NAME] and Switch.objects.filter(topology__is_fabric=True, dummy=False, name=data[NAME])): raise IgniteException(ERR_SW_NAME_IN_USE) switch.name = data[NAME] # check if switch already exists with new serial num # note: serial numbers are unique across fabrics if (data[SERIAL_NUM] and switch.serial_num != data[SERIAL_NUM] and Switch.objects.filter(dummy=False, serial_num=data[SERIAL_NUM])): raise IgniteException(ERR_SERIAL_NUM_IN_USE) # check if serial_num exists in discovery rules find_dup_serial_discovery([data[SERIAL_NUM]]) switch.serial_num = data[SERIAL_NUM] new_model = get_switch(data[MODEL]) # is there a change in model? change = True if new_model != switch.model else False logger.debug("%schange in switch model", "no " if not change else "") # save new values switch.model = new_model switch.image_profile = (image.get_profile(data[IMAGE_PROFILE]) if data[IMAGE_PROFILE] else None) switch.config_profile = (config.get_profile(data[CONFIG_PROFILE]) if data[CONFIG_PROFILE] else None) switch.feature_profile = (feature.get_profile(data[FEATURE_PROFILE]) if data[FEATURE_PROFILE] else None) switch.workflow = (workflow.get_workflow(data[WORKFLOW]) if data[WORKFLOW] else None) switch.save() if change: BaseTopology.get_object(fab_id, user).update_model(switch) else: Fabric.objects.filter(pk=fab_id).update(updated_by=user)
def add_discoveryrule(data, username): config_obj = get_profile(data[CONFIG]) image = image_profile.get_profile(data[IMAGE]) rule_object = DiscoveryRule() if data[WORKFLOW]: wf = get_workflow(data[WORKFLOW]) rule_object.workflow = wf rule_object.name = data[NAME] rule_object.match = data[MATCH].lower() if str(data[MATCH]) == SERIAL_NUM: find_repeat_serial_num(data[SUBRULES]) find_duplicate(data[SUBRULES]) rule_object.priority = data[PRIORITY] rule_object.config = config_obj rule_object.image = image rule_object.subrules = data[SUBRULES] rule_object.updated_by = username rule_object.save() return rule_object
def add_fabric(data, user): # get topology top = Topology.objects.get(pk=data[TOPOLOGY]) logger.debug("fabric name = %s, topology name = %s, model = %s", data[NAME], top.name, top.model_name) # create new fabric fabric = Fabric() fabric.name = data[NAME] fabric.model_name = top.model_name fabric.is_fabric = True if data[CONFIG_PROFILE]: fabric.config_profile = config.get_profile(data[CONFIG_PROFILE]) if data[FEATURE_PROFILE]: fabric.feature_profile = feature.get_profile(data[FEATURE_PROFILE]) fabric.updated_by = user fabric.save() cfg = dict() feat = dict() wf = dict() # store tier default config & feature profile for item in data[SWITCHES]: # if profile id is zero, then store null # it means - do not apply config/feature/workflow for this tier if item[CONFIG_PROFILE]: cfg[item[TIER]] = config.get_profile(item[CONFIG_PROFILE]) else: cfg[item[TIER]] = None if item[FEATURE_PROFILE]: feat[item[TIER]] = feature.get_profile(item[FEATURE_PROFILE]) else: feat[item[TIER]] = None if item[WORKFLOW]: wf[item[TIER]] = workflow.get_workflow(item[WORKFLOW]) else: wf[item[TIER]] = None # fabric defaults, switches & links objects fabric.defaults = dict() fabric.defaults[SWITCHES] = list() fabric.defaults[LINKS] = list() fabric.switches = list() fabric.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=top.id).order_by(ID): # save id for later switch_id = switch.id switch.id = None switch.topology = fabric if switch.dummy: # set config & feature profile for fabric default switch switch.config_profile = cfg[switch.tier] switch.feature_profile = feat[switch.tier] switch.workflow = wf[switch.tier] else: # append fabric name to switch name switch.name = fabric.name + "_" + switch.name switch.save() # store topology switch id to switch mapping switch_dict[switch_id] = switch if switch.dummy: fabric.defaults[SWITCHES].append(switch) else: fabric.switches.append(switch) # duplicate all topology links into fabric for link in Link.objects.filter(topology_id=top.id): link.id = None link.topology = fabric 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: fabric.links.append(link) elif link.dummy and not link.src_switch: link.src_tier = link.src_ports link.dst_tier = link.dst_ports fabric.defaults[LINKS].append(link) return fabric
def update_switch(fab_id, switch_id, data, user): switch = Switch.objects.get(pk=switch_id) # check if switch is already booted if switch.boot_detail: if data[CONFIG_TYPE] == RUNNING_CONFIG: logger.debug("input config type- " + data[CONFIG_TYPE]) if SwitchConfig.objects.filter(switch_id=switch_id): switch.config_type = data[CONFIG_TYPE] else: logger.error(ERR_RUN_CONFIG_NOT_AVAL) raise IgniteException(ERR_RUN_CONFIG_NOT_AVAL) else: switch.config_type = data[CONFIG_TYPE] if any([ switch.name != data[NAME], switch.model.id != data[MODEL], switch.serial_num != data[SERIAL_NUM] ]): raise IgniteException(ERR_NO_NAME_CHANGE) # check if switch already exists with new name # switch name is searched across all fabrics if (switch.name != data[NAME] and Switch.objects.filter( topology__is_fabric=True, dummy=False, name=data[NAME])): raise IgniteException(ERR_SW_NAME_IN_USE) switch.name = data[NAME] # check if switch already exists with new serial num # note: serial numbers are unique across fabrics if (data[SERIAL_NUM] and switch.serial_num != data[SERIAL_NUM] and Switch.objects.filter(dummy=False, serial_num=data[SERIAL_NUM])): raise IgniteException(ERR_SERIAL_NUM_IN_USE) # check if serial_num exists in discovery rules find_dup_serial_discovery([data[SERIAL_NUM]]) switch.serial_num = data[SERIAL_NUM] new_model = get_switch(data[MODEL]) # is there a change in model? change = True if new_model != switch.model else False logger.debug("%schange in switch model", "no " if not change else "") if change: if data[MODEL] == 1: logger.debug("Unknown switch model can not be assigned") raise IgniteException(ERR_CAN_NOT_ASSIGN_UNKOWN_MODEL) # save new values switch.model = new_model switch.image_profile = (image.get_profile(data[IMAGE_PROFILE]) if data[IMAGE_PROFILE] else None) switch.config_profile = (config.get_profile(data[CONFIG_PROFILE]) if data[CONFIG_PROFILE] else None) switch.feature_profile = (feature.get_profile(data[FEATURE_PROFILE]) if data[FEATURE_PROFILE] else None) switch.workflow = (workflow.get_workflow(data[WORKFLOW]) if data[WORKFLOW] else None) switch.save() if change: BaseTopology.get_object(fab_id, user).update_model(switch) else: Fabric.objects.filter(pk=fab_id).update(updated_by=user)