Example #1
0
    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()
Example #2
0
    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
Example #3
0
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
Example #4
0
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)
Example #5
0
def tasks_validation(data, options, job):
    from celery_tasks import get_all_switches

    tsk = []
    if len(data) == 0:
        raise IgniteException("Job cannot have empty task sequence")
    if options == "update":
        ref_count_delete(job)
    for task in data:
        try:
            grp = group.get_group(task[GROUP_ID])
            img = image_profile.get_profile(task[IMAGE_ID])
            if options != "get" and img.system_image == None:
                raise IgniteException("No system image is found in the image profile: " + img.profile_name)
            if options != "get" and task["type"] == "epld_upgrade" and img.epld_image == None:
                raise IgniteException("No epld image is found in the image profile: " + img.profile_name)
            if task["type"] in ["epld_upgrade", "switch_upgrade"]:
                if options != "get" and img.access_protocol != "scp":
                    raise IgniteException("Only scp protocol is supported for image profile: " + img.profile_name)
            task["switch_count"] = len(grp.switch_list)
            # False is to say not to decrypt passwords
            switches = get_all_switches(task, False)
            task["group"]["switches"] = switches
            task[IMAGE_NAME] = img.profile_name

            if task["type"] != "custom":
                task["task_params"] = {}
                image = get_image_details(img)
                if task["type"] == "epld_upgrade":
                    image["epld_image"] = img.epld_image
                task["task_params"]["image"] = image

            if task["type"] == "custom":
                if task["file_name"] is None and task["function"] is None:
                    raise IgniteException("Please Provide file/function name Custom task")
                try:
                    task["parameters"] = fill_param_values(task["params"])
                except:
                    raise IgniteException(ERR_IN_PARAMS)

            tsk.append(task)
            if options != "get":
                ref_count_add(grp)
        except Group.DoesNotExist as e:
            raise IgniteException("Group id " + str(task[GROUP_ID]) + " not found")
        except ImageProfile.DoesNotExist as e:
            raise IgniteException("Image id " + str(task[IMAGE_ID]) + " not found")
    return tsk
Example #6
0
def fill_param_values(params):
    parameters = {}
    if len(params):

        for param in params:
            if param[PARAM_TYPE] == FIXED:
                parameters[param[PARAM_NAME]] = param[PARAM_VAL]

            if param[PARAM_TYPE] == IMAGE_PROFILE:
                img = image_profile.get_profile(param[PARAM_VAL])
                parameters[param[PARAM_NAME]] = get_image_details(img)

            if param[PARAM_TYPE] == EVAL:
                try:
                    parameters[param[PARAM_NAME]] = eval(param[PARAM_VALUE])
                except SyntaxError:
                    raise IgniteException("%s = %s" % (ERR_EVAL_SYNTAX, param[PARAM_VALUE]))
    return parameters
Example #7
0
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
Example #8
0
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
Example #9
0
def fill_param_values(params):
    parameters = {}
    if len(params):

        for param in params:
            if param[PARAM_TYPE] == FIXED:
                parameters[param[PARAM_NAME]] = param[PARAM_VAL]

            if param[PARAM_TYPE] == IMAGE_PROFILE:
                img = image_profile.get_profile(param[PARAM_VAL])
                parameters[param[PARAM_NAME]] = get_image_details(img)

            if param[PARAM_TYPE] == EVAL:
                try:
                    parameters[param[PARAM_NAME]] = eval(param[PARAM_VALUE])
                except SyntaxError:
                    raise IgniteException(
                        "%s = %s" % (ERR_EVAL_SYNTAX, param[PARAM_VALUE]))
    return parameters
Example #10
0
def tasks_validation(data, options, job):
    from celery_tasks import get_all_switches
    tsk = []
    if len(data) == 0:
        raise IgniteException("Job cannot have empty task sequence")
    if options == "update":
        ref_count_delete(job)
    for task in data:
        try:
            grp = group.get_group(task["group_id"])
            img = image_profile.get_profile(task["image_id"])
            if options!= 'get' and img.system_image == None:
                raise IgniteException("No system image is found in the image profile: "+ img.profile_name)
            if options!= 'get' and task['type'] == 'epld_upgrade' and img.epld_image == None:
                raise IgniteException("No epld image is found in the image profile: "+ img.profile_name)
            if options!='get' and img.access_protocol != 'scp':
                raise IgniteException("Only scp protocol is supported for image profile: "+ img.profile_name)
            task["switch_count"] = len(grp.switch_list)
            switches = get_all_switches(task)
            task['group']['switches'] = switches
            task['params']={} 
            task['params']['image'] = {}
            image = {}
            image['profile_name'] = img.profile_name
            image['system_image'] = img.system_image
            image['id'] = img.id
            image['image_server_ip'] = img.image_server_ip
            image['image_server_username'] = img.image_server_username
            image['image_server_password'] = img.image_server_password
            image['access_protocol'] = img.access_protocol
            if task['type'] == 'epld_upgrade':
                image['epld_image'] = img.epld_image
            task['params']['image'] = image
            task["image_name"] = img.profile_name
            tsk.append(task)
            if options != "get":
                ref_count_add(grp)
        except Group.DoesNotExist as e:
            raise IgniteException("Group id "+str(task["group_id"])+" not found")
        except ImageProfile.DoesNotExist as e:
            raise IgniteException("Image id "+str(task["image_id"])+" not found")
    return tsk
Example #11
0
    def update_switch(self, switch_id, data):
        switch = Switch.objects.get(pk=switch_id)

        if data[MODEL] == 1:
            raise IgniteException(ERR_CAN_NOT_ASSIGN_UNKOWN_MODEL)

        # TODO: check if model is valid, currently UI perform this check

        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 = get_profile(data[IMAGE_PROFILE]) if data[IMAGE_PROFILE] else None
        switch.save()

        if change:
            self.update_model(switch)
Example #12
0
    def update_switch(self, switch_id, data):
        switch = Switch.objects.get(pk=switch_id)

        if data[MODEL] == 1:
            raise IgniteException(ERR_CAN_NOT_ASSIGN_UNKOWN_MODEL)

        # TODO: check if model is valid, currently UI perform this check

        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 = (get_profile(data[IMAGE_PROFILE])
                                if data[IMAGE_PROFILE] else None)
        switch.save()

        if change:
            self.update_model(switch)
Example #13
0
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)