コード例 #1
0
    def collect_create_config(self, network_id, host_id, vlan_id):
        device_config_dict = {}
        vlan_list = db.get_vlanlist_byhost(host_id)
        if vlan_id not in vlan_list:
            vlan_list.append(vlan_id)

        host_list = db.get_host_list(network_id)
        # Find which leaf device connects to the host_id.
        leaf_need_configure = []
        leaf_generator = tools.topology_generator(self.leaf_topology)
        leaf_ip_ref = {}
        for leaf_ip, topology in leaf_generator:
            leaf_host = topology['host']
            if leaf_host in host_list:
                leaf_ip_ref.setdefault(leaf_ip, set([]))
                leaf_ip_ref[leaf_ip] |= set(db.get_vlanlist_byhost(leaf_host))
            if leaf_host == host_id:
                leaf_ip_ref[leaf_ip] |= set([vlan_id])
                device_config_dict.setdefault(leaf_ip, {})
                device_config_dict[leaf_ip].setdefault('port_vlan', [])
                device_config_dict[leaf_ip]['vlan_create'] = vlan_list
                device_config_dict[leaf_ip]['port_vlan'].\
                    append((topology['ports'], vlan_list))
                leaf_need_configure.append(leaf_ip)

        LOG.info(_("Starting collecting spine's configs with leaf %s."),
                 str(leaf_need_configure))
        # Find which spine device connects to the leaf device
        # which is configured above.
        spine_generator = tools.topology_generator(self.spine_topology)
        for spine_ip, topology in spine_generator:
            leaf_ip = topology['leaf_ip']
            if leaf_ip in leaf_need_configure:
                spine_vlan_list = list(leaf_ip_ref[leaf_ip])
                if spine_ip not in device_config_dict:
                    device_config_dict.setdefault(spine_ip, {})
                    device_config_dict[spine_ip].setdefault('port_vlan', [])
                    device_config_dict[spine_ip]['vlan_create'] = vlan_list
                    device_config_dict[spine_ip]['port_vlan'].\
                        append((topology['spine_ports'], spine_vlan_list))
                if leaf_ip in device_config_dict:
                    device_config_dict[leaf_ip]['port_vlan'].\
                        append((topology['leaf_ports'], spine_vlan_list))

        LOG.info(_("Collect device configuration: %s"), device_config_dict)

        return device_config_dict
コード例 #2
0
    def collect_delete_config(self, network_id, host_id, vlan_id):
        vlan_list = db.get_vlanlist_byhost(host_id)
        if vlan_id in vlan_list:
            vlan_list.remove(vlan_id)
        leaf_generator = tools.topology_generator(self.leaf_topology)
        host_list = db.get_host_list(network_id)
        LOG.info(_("Delete vlan host list %s"), host_list)
        # It is the counter of host that connects to the same
        # device specified by ip address.
        leaf_ref_vlans = {}
        leaf_ref_host = {}
        delete_config = {}
        for leaf_ip, topology in leaf_generator:
            leaf_ref_vlans.setdefault(leaf_ip, set([]))
            leaf_ref_host.setdefault(leaf_ip, False)
            host = topology['host']
            host_vlan = db.get_vlanlist_byhost(host)
            if host in host_list:
                leaf_ref_vlans[leaf_ip] |= set(host_vlan)
            if host == host_id:
                delete_config.setdefault(leaf_ip, {})
                delete_config[leaf_ip].setdefault('port_vlan', [])
                delete_config[leaf_ip]['port_vlan'].\
                    append((topology['ports'], vlan_list))
                delete_config[leaf_ip]['vlan_del'] = []
                if host in host_list:
                    host_list.remove(host)
            else:
                if len(set([vlan_id]) & set(host_vlan)) > 0:
                    leaf_ref_host[leaf_ip] = True

        # If there is no host connects to leaf in the same network,
        # we will remove the configuration in the spine device.
        # And remove the vlan configuration in the leaf device.
        for leaf_ip in leaf_ref_vlans:
            if leaf_ref_host[leaf_ip] is False and leaf_ip in delete_config:
                leaf_ref_vlans[leaf_ip] -= set([vlan_id])
                delete_config[leaf_ip]['vlan_del'] = [vlan_id]

        # Check which spine device connects to above leafs.
        # We need remove this spine's configuration.
        spine_generator = tools.topology_generator(self.spine_topology)
        # This dict is used to count the host number in same network
        # with leafs connected to spine.
        spine_delete_score = {}
        for spine_ip, topology in spine_generator:
            leaf_ip = topology['leaf_ip']
            if leaf_ip in leaf_ref_vlans:
                spine_delete_score.setdefault(spine_ip, 0)
                if leaf_ref_host[leaf_ip] is True:
                    spine_delete_score[spine_ip] += 1
            if leaf_ip in delete_config:
                vlan_list = list(leaf_ref_vlans[leaf_ip])
                delete_config[spine_ip] = {}
                delete_config[spine_ip].setdefault('port_vlan', [])
                delete_config[spine_ip]['port_vlan'].\
                    append((topology['spine_ports'], vlan_list))
                delete_config[spine_ip]['vlan_del'] = []
                if len(delete_config[leaf_ip]['vlan_del']) != 0:
                    delete_config[leaf_ip]['port_vlan'].\
                        append((topology['leaf_ports'], vlan_list))
        # Check does spine need to delete vlan.
        for spine_ip in spine_delete_score:
            if spine_delete_score[spine_ip] == 0 \
                    and spine_ip in delete_config:
                delete_config[spine_ip]['vlan_del'] = [vlan_id]
        LOG.info(_("Delete configuration : %s"), delete_config)
        return delete_config