Пример #1
0
def nsx_recreate_dhcp_edge(resource, event, trigger, **kwargs):
    """Recreate a dhcp edge with all the networks n a new NSXv edge"""
    if not kwargs.get('property'):
        LOG.error(_LE("Need to specify edge-id parameter"))
        return

    # input validation
    properties = admin_utils.parse_multi_keyval_opt(kwargs['property'])
    old_edge_id = properties.get('edge-id')
    if not old_edge_id:
        LOG.error(_LE("Need to specify edge-id parameter"))
        return
    LOG.info(_LI("ReCreating NSXv Edge: %s"), old_edge_id)

    # init the plugin and edge manager
    cfg.CONF.set_override('core_plugin',
                          'vmware_nsx.shell.admin.plugins.nsxv.resources'
                          '.utils.NsxVPluginWrapper')
    plugin = utils.NsxVPluginWrapper()
    nsxv_manager = vcns_driver.VcnsDriver(edge_utils.NsxVCallbacks(plugin))
    edge_manager = edge_utils.EdgeManager(nsxv_manager, plugin)
    context = n_context.get_admin_context()

    # verify that this is a DHCP edge
    bindings = nsxv_db.get_nsxv_router_bindings_by_edge(
        context.session, old_edge_id)
    if (not bindings or
        not bindings[0]['router_id'].startswith(
            nsxv_constants.DHCP_EDGE_PREFIX)):
        LOG.error(_LE("Edge %(edge_id)s is not a DHCP edge"),
                 {'edge_id': old_edge_id})
        return

    # find the networks bound to this DHCP edge
    networks_binding = nsxv_db.get_edge_vnic_bindings_by_edge(
        context.session, old_edge_id)
    network_ids = [binding['network_id'] for binding in networks_binding]

    # Find out the vdr router, if this is a vdr DHCP edge
    vdr_binding = nsxv_db.get_vdr_dhcp_binding_by_edge(
        context.session, old_edge_id)
    vdr_router_id = vdr_binding['vdr_router_id'] if vdr_binding else None

    # Delete the old edge
    delete_old_dhcp_edge(context, old_edge_id, bindings)

    if vdr_router_id:
        # recreate the edge as a VDR DHCP edge
        recreate_vdr_dhcp_edge(context, plugin, edge_manager,
                               old_edge_id, vdr_router_id)
    else:
        # This is a regular DHCP edge:
        # Move all the networks to other (new or existing) edge
        for net_id in network_ids:
            recreate_network_dhcp(context, plugin, edge_manager,
                                  old_edge_id, net_id)
Пример #2
0
def get_nsxv_backup_edges():
    edges = utils.get_nsxv_backend_edges()
    backup_edges = []
    edgeapi = utils.NeutronDbClient()
    for edge in edges:
        if edge['name'].startswith("backup-"):
            edge_vnic_binds = nsxv_db.get_edge_vnic_bindings_by_edge(
                edgeapi.context.session, edge['id'])
            if not edge_vnic_binds:
                backup_edges.append(edge)
    return backup_edges
Пример #3
0
def get_nsxv_backup_edges():
    edges = nsxv.get_edges()[1]
    edges = edges['edgePage'].get('data', [])
    backup_edges = []
    edgeapi = utils.NeutronDbClient()
    for edge in edges:
        if edge['name'].startswith("backup-"):
            edge_vnic_binds = nsxv_db.get_edge_vnic_bindings_by_edge(
                edgeapi.context.session, edge['id'])
            if not edge_vnic_binds:
                backup_edges.append(edge)
    return backup_edges
Пример #4
0
def nsx_recreate_dhcp_edge(resource, event, trigger, **kwargs):
    """Recreate a dhcp edge with all the networks on a new NSXv edge"""
    usage_msg = ("Need to specify edge-id or net-id parameter")
    if not kwargs.get('property'):
        LOG.error(usage_msg)
        return

    # input validation
    properties = admin_utils.parse_multi_keyval_opt(kwargs['property'])
    old_edge_id = properties.get('edge-id')
    if not old_edge_id:
        # if the net-id property exist - recreate the edge for this network
        net_id = properties.get('net-id')
        if net_id:
            nsx_recreate_dhcp_edge_by_net_id(net_id)
            return
        LOG.error(usage_msg)
        return
    LOG.info("ReCreating NSXv Edge: %s", old_edge_id)

    context = n_context.get_admin_context()

    # verify that this is a DHCP edge
    bindings = nsxv_db.get_nsxv_router_bindings_by_edge(
        context.session, old_edge_id)
    if (not bindings or
        not bindings[0]['router_id'].startswith(
            nsxv_constants.DHCP_EDGE_PREFIX)):
        LOG.error("Edge %(edge_id)s is not a DHCP edge",
                 {'edge_id': old_edge_id})
        return

    # init the plugin and edge manager
    cfg.CONF.set_override('core_plugin',
                          'vmware_nsx.shell.admin.plugins.nsxv.resources'
                          '.utils.NsxVPluginWrapper')
    with utils.NsxVPluginWrapper() as plugin:
        nsxv_manager = vcns_driver.VcnsDriver(
            edge_utils.NsxVCallbacks(plugin))
        edge_manager = edge_utils.EdgeManager(nsxv_manager, plugin)

        # find the networks bound to this DHCP edge
        networks_binding = nsxv_db.get_edge_vnic_bindings_by_edge(
            context.session, old_edge_id)
        network_ids = [binding['network_id'] for binding in networks_binding]

        # Delete the old edge
        delete_old_dhcp_edge(context, old_edge_id, bindings)

        # Move all the networks to other (new or existing) edge
        for net_id in network_ids:
            recreate_network_dhcp(context, plugin, edge_manager,
                                  old_edge_id, net_id)
Пример #5
0
def nsx_recreate_dhcp_edge(resource, event, trigger, **kwargs):
    """Recreate a dhcp edge with all the networks on a new NSXv edge"""
    usage_msg = ("Need to specify edge-id or net-id parameter")
    if not kwargs.get('property'):
        LOG.error(usage_msg)
        return

    # input validation
    properties = admin_utils.parse_multi_keyval_opt(kwargs['property'])
    old_edge_id = properties.get('edge-id')
    if not old_edge_id:
        # if the net-id property exist - recreate the edge for this network
        net_id = properties.get('net-id')
        if net_id:
            nsx_recreate_dhcp_edge_by_net_id(net_id)
            return
        LOG.error(usage_msg)
        return
    LOG.info("ReCreating NSXv Edge: %s", old_edge_id)

    context = n_context.get_admin_context()

    # verify that this is a DHCP edge
    bindings = nsxv_db.get_nsxv_router_bindings_by_edge(
        context.session, old_edge_id)
    if (not bindings or not bindings[0]['router_id'].startswith(
            nsxv_constants.DHCP_EDGE_PREFIX)):
        LOG.error("Edge %(edge_id)s is not a DHCP edge",
                  {'edge_id': old_edge_id})
        return

    # init the plugin and edge manager
    cfg.CONF.set_override(
        'core_plugin', 'vmware_nsx.shell.admin.plugins.nsxv.resources'
        '.utils.NsxVPluginWrapper')
    with utils.NsxVPluginWrapper() as plugin:
        nsxv_manager = vcns_driver.VcnsDriver(edge_utils.NsxVCallbacks(plugin))
        edge_manager = edge_utils.EdgeManager(nsxv_manager, plugin)

        # find the networks bound to this DHCP edge
        networks_binding = nsxv_db.get_edge_vnic_bindings_by_edge(
            context.session, old_edge_id)
        network_ids = [binding['network_id'] for binding in networks_binding]

        # Delete the old edge
        delete_old_dhcp_edge(context, old_edge_id, bindings)

        # Move all the networks to other (new or existing) edge
        for net_id in network_ids:
            recreate_network_dhcp(context, plugin, edge_manager, old_edge_id,
                                  net_id)
    def _get_all_routers_vnic_indices(self, context, router_ids):

        all_vnic_indices = {}
        if len(router_ids) < 1:
            # there are no routers
            return all_vnic_indices

        intf_ports = self.plugin.get_ports(
            context.elevated(),
            filters={'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF]})

        edge_id = edge_utils.get_router_edge_id(context, router_ids[0])
        edge_vnic_bindings = nsxv_db.get_edge_vnic_bindings_by_edge(
            context.session, edge_id)

        for this_router_id in router_ids:
            # get networks IDs for this router
            router_net_ids = list(
                set([
                    port['network_id'] for port in intf_ports
                    if port['device_id'] == this_router_id
                ]))

            # get vnic index for each network
            vnic_indices = []
            for net_id in router_net_ids:
                vnic_indices.extend([
                    edge_vnic_binding.vnic_index
                    for edge_vnic_binding in edge_vnic_bindings
                    if edge_vnic_binding.network_id == net_id
                ])

            # make sure the list is unique:
            vnic_indices = list(set(vnic_indices))
            # add to the result dict
            all_vnic_indices[this_router_id] = list(vnic_indices)

        return all_vnic_indices
Пример #7
0
def get_nsxv_backup_edges(scope="all"):
    edges = utils.get_nsxv_backend_edges()
    backup_edges = []
    edgeapi = utils.NeutronDbClient()
    for edge in edges:
        if edge['name'].startswith("backup-"):
            # Make sure it is really a backup edge
            edge_vnic_binds = nsxv_db.get_edge_vnic_bindings_by_edge(
                edgeapi.context.session, edge['id'])
            if scope != "all":
                # Make sure the backup edge exists in neutron
                # Return backup edges existing in both neutron and backend
                # when scope != all
                edge_in_neutron = nsxv_db.get_nsxv_router_binding_by_edge(
                    edgeapi.context.session, edge['id'])
                if not edge_vnic_binds and edge_in_neutron:
                    extend_edge_info(edge)
                    backup_edges.append(edge)
            else:
                if not edge_vnic_binds:
                    extend_edge_info(edge)
                    backup_edges.append(edge)
    return backup_edges
Пример #8
0
    def _get_all_routers_vnic_indices(self, context, router_ids):

        all_vnic_indices = {}
        if len(router_ids) < 1:
            # there are no routers
            return all_vnic_indices

        intf_ports = self.plugin.get_ports(
            context.elevated(),
            filters={'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF]})

        edge_id = edge_utils.get_router_edge_id(context, router_ids[0])
        edge_vnic_bindings = nsxv_db.get_edge_vnic_bindings_by_edge(
            context.session, edge_id)

        for this_router_id in router_ids:
            # get networks IDs for this router
            router_net_ids = list(
                set([port['network_id']
                     for port in intf_ports
                     if port['device_id'] == this_router_id]))

            # get vnic index for each network
            vnic_indices = []
            for net_id in router_net_ids:
                vnic_indices.extend([edge_vnic_binding.vnic_index
                                     for edge_vnic_binding
                                     in edge_vnic_bindings
                                     if edge_vnic_binding.network_id == net_id
                                     ])

            # make sure the list is unique:
            vnic_indices = list(set(vnic_indices))
            # add to the result dict
            all_vnic_indices[this_router_id] = list(vnic_indices)

        return all_vnic_indices
Пример #9
0
def get_router_edge_vnic_bindings(edge_id):
    edgeapi = utils.NeutronDbClient()
    return nsxv_db.get_edge_vnic_bindings_by_edge(edgeapi.context.session,
                                                  edge_id)
Пример #10
0
    def _validate_dhcp_edge(self, context, edge_dict, pfx_dict, networks,
                            edge_id, readonly):
        # Also metadata network should be a valid network for the edge
        az_name = self.plugin.get_availability_zone_name_by_edge(
            context, edge_id)
        with locking.LockManager.get_lock(edge_id):
            vnic_binds = nsxv_db.get_edge_vnic_bindings_by_edge(
                context.session, edge_id)
            edge_networks = [bind['network_id'] for bind in vnic_binds]

            # Step (A)
            # Find router bindings which are mapped to dead networks, or
            # do not have interfaces registered in nsxv tables
            for binding in edge_dict[edge_id]:
                router_id = binding['router_id']

                net_pfx = router_id[len(vcns_const.DHCP_EDGE_PREFIX):]
                net_id = pfx_dict.get(net_pfx)

                if net_id is None:
                    # Delete router binding as we do not have such network
                    # in Neutron
                    self.error_count += 1
                    self.error_info = base_job.housekeeper_warning(
                        self.error_info,
                        'router binding %s for edge %s has no matching '
                        'neutron network', router_id, edge_id)

                    if not readonly:
                        nsxv_db.delete_nsxv_router_binding(
                            context.session, binding['router_id'])
                        self.fixed_count += 1
                else:
                    if net_id not in edge_networks:
                        # Create vNic bind here
                        self.error_count += 1
                        self.error_info = base_job.housekeeper_warning(
                            self.error_info,
                            'edge %s vnic binding missing for network %s',
                            edge_id, net_id)

                        if not readonly:
                            nsxv_db.allocate_edge_vnic_with_tunnel_index(
                                context.session, edge_id, net_id, az_name)
                            self.fixed_count += 1

            # Step (B)
            # Find vNic bindings which reference invalid networks or aren't
            # bound to any router binding

            # Reread vNic binds as we might created more or deleted some in
            #  step (A)
            vnic_binds = nsxv_db.get_edge_vnic_bindings_by_edge(
                context.session, edge_id)

            for bind in vnic_binds:
                if bind['network_id'] not in networks:
                    self.error_count += 1
                    self.error_info = base_job.housekeeper_warning(
                        self.error_info,
                        'edge vnic binding for edge %s is for invalid '
                        'network id %s', edge_id, bind['network_id'])

                    if not readonly:
                        nsxv_db.free_edge_vnic_by_network(
                            context.session, edge_id, bind['network_id'])
                        self.fixed_count += 1

            # Step (C)
            # Verify that backend is in sync with Neutron

            # Reread vNic binds as we might deleted some in step (B)
            vnic_binds = nsxv_db.get_edge_vnic_bindings_by_edge(
                context.session, edge_id)

            # Transform to network-keyed dict
            vnic_dict = {
                vnic['network_id']: {
                    'vnic_index': vnic['vnic_index'],
                    'tunnel_index': vnic['tunnel_index']
                }
                for vnic in vnic_binds
            }

            backend_vnics = self.plugin.nsx_v.vcns.get_interfaces(
                edge_id)[1].get('vnics', [])
            if_changed = {}
            self._validate_edge_subinterfaces(context, edge_id, backend_vnics,
                                              vnic_dict, if_changed)
            self._add_missing_subinterfaces(context, edge_id, vnic_binds,
                                            backend_vnics, if_changed,
                                            readonly)

            if not readonly:
                for vnic in backend_vnics:
                    if if_changed[vnic['index']]:
                        self.plugin.nsx_v.vcns.update_interface(edge_id, vnic)

                    self._update_router_bindings(context, edge_id)

                self.fixed_count += self.fixed_sub_if_count
Пример #11
0
def get_router_edge_vnic_bindings(edge_id):
    edgeapi = utils.NeutronDbClient()
    return nsxv_db.get_edge_vnic_bindings_by_edge(
        edgeapi.context.session, edge_id)