Exemplo n.º 1
0
    def _get_edge_id_by_rtr_id(self, context, rtr_id):
        binding = nsxv_db.get_nsxv_router_binding(
            context.session,
            rtr_id)

        if binding:
            return binding['edge_id']
Exemplo n.º 2
0
 def _get_md_proxy_for_router(self, context, router_id):
     binding = nsxv_db.get_nsxv_router_binding(context.session, router_id)
     md_proxy = None
     if binding:
         az_name = binding['availability_zone']
         md_proxy = self._core_plugin.get_metadata_proxy_handler(az_name)
     return md_proxy
Exemplo n.º 3
0
    def _get_edge_id_by_rtr_id(self, context, rtr_id):
        binding = nsxv_db.get_nsxv_router_binding(
            context.session,
            rtr_id)

        if binding:
            return binding['edge_id']
Exemplo n.º 4
0
def nsx_recreate_router(router_id):
    # 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:
        context = n_context.get_admin_context()

        router = plugin.get_router(context, router_id)
        if router.get('distributed'):
            LOG.error("Recreating a distributed router is not supported")
            return
        router_driver = plugin._router_managers.get_tenant_router_driver(
            context, router['router_type'])

        # Check if it is already attached to an edge
        binding = nsxv_db.get_nsxv_router_binding(context.session, router_id)
        if binding:
            old_edge_id = binding['edge_id']
            # detach the router from this edge
            LOG.info("Detaching the router from edge %s", old_edge_id)
            router_driver.detach_router(context, router_id, {'router': router})

        # attach the router to a new edge
        appliance_size = router.get(routersize.ROUTER_SIZE)
        router_driver.attach_router(context,
                                    router_id, {'router': router},
                                    appliance_size=appliance_size)
        # find out who is the new edge to print it
        new_edge_id = router_driver._get_edge_id_or_raise(context, router_id)
        LOG.info("Router %(router)s was attached to edge %(edge)s", {
            'router': router_id,
            'edge': new_edge_id
        })
Exemplo n.º 5
0
def neutron_clean_backup_edge(resource, event, trigger, **kwargs):
    """Delete a backup edge from the neutron, and backend by it's name

    The name of the backup edge is the router-id column in the BD table
    nsxv_router_bindings, and it is also printed by list-mismatches
    """
    errmsg = ("Need to specify router-id property. Add --property "
              "router-id=<router-id>")
    if not kwargs.get('property'):
        LOG.error("%s", errmsg)
        return
    properties = admin_utils.parse_multi_keyval_opt(kwargs['property'])
    router_id = properties.get('router-id')
    if not router_id:
        LOG.error("%s", errmsg)
        return

    # look for the router-binding entry
    edgeapi = utils.NeutronDbClient()
    rtr_binding = nsxv_db.get_nsxv_router_binding(
            edgeapi.context.session, router_id)
    if not rtr_binding:
        LOG.error('Backup %s was not found in DB', router_id)
        return

    edge_id = rtr_binding['edge_id']
    if edge_id:
        # delete from backend too
        _delete_edge_from_nsx_and_neutron(edge_id, router_id)
    else:
        # delete only from DB
        _delete_backup_from_neutron_db(None, router_id)
Exemplo n.º 6
0
    def _get_edge_id_by_rtr_id(self, rtr_id, context=None):
        if not context:
            context = self.context
        binding = nsxv_db.get_nsxv_router_binding(context.session, rtr_id)

        if binding:
            return binding['edge_id']
Exemplo n.º 7
0
def nsx_recreate_router(router_id):
    # 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:
        context = n_context.get_admin_context()

        router = plugin.get_router(context, router_id)
        if router.get('distributed'):
            LOG.error("Recreating a distributed router is not supported")
            return
        router_driver = plugin._router_managers.get_tenant_router_driver(
            context, router['router_type'])

        # Check if it is already attached to an edge
        binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                  router_id)
        if binding:
            old_edge_id = binding['edge_id']
            # detach the router from this edge
            LOG.info("Detaching the router from edge %s", old_edge_id)
            router_driver.detach_router(context, router_id,
                                        {'router': router})

        # attach the router to a new edge
        appliance_size = router.get(routersize.ROUTER_SIZE)
        router_driver.attach_router(context, router_id,
                                    {'router': router},
                                    appliance_size=appliance_size)
        # find out who is the new edge to print it
        new_edge_id = router_driver._get_edge_id_or_raise(
            context, router_id)
        LOG.info("Router %(router)s was attached to edge %(edge)s",
                 {'router': router_id, 'edge': new_edge_id})
Exemplo n.º 8
0
 def _get_md_proxy_for_router(self, context, router_id):
     binding = nsxv_db.get_nsxv_router_binding(context.session,
                                               router_id)
     md_proxy = None
     if binding:
         az_name = binding['availability_zone']
         md_proxy = self._core_plugin.get_metadata_proxy_handler(
             az_name)
     return md_proxy
Exemplo n.º 9
0
    def _get_edge_id_by_rtr_id(self, rtr_id, context=None):
        if not context:
            context = self.context
        binding = nsxv_db.get_nsxv_router_binding(
            context.session,
            rtr_id)

        if binding:
            return binding['edge_id']
Exemplo n.º 10
0
    def _get_router_edge_info(self, context, router_id):
        edge_binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                       router_id)
        if not edge_binding:
            return None, None

        # Indicates which routes should be advertised - connected or static.
        advertise_static_routes = False
        if edge_binding['edge_type'] != nsxv_constants.SERVICE_EDGE:
            # Distributed router
            plr_id = self._edge_manager.get_plr_by_tlr_id(context, router_id)
            edge_binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                           plr_id)
            if not edge_binding:
                # Distributed router isn't bound to plr
                return None, None
            # PLR for distributed router, advertise static routes.
            advertise_static_routes = True
        return edge_binding['edge_id'], advertise_static_routes
Exemplo n.º 11
0
    def _get_router_edge_info(self, context, router_id):
        edge_binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                       router_id)
        if not edge_binding:
            return None, None

        # Idicates which routes should be advertised - connected or static.
        advertise_static_routes = False
        if edge_binding['edge_type'] != nsxv_constants.SERVICE_EDGE:
            # Distributed router
            plr_id = self._edge_manager.get_plr_by_tlr_id(context, router_id)
            edge_binding = nsxv_db.get_nsxv_router_binding(
                context.session, plr_id)
            if not edge_binding:
                # Distributed router isn't bound to plr
                return None, None
            # PLR for distributed router, advertise static routes.
            advertise_static_routes = True
        return edge_binding['edge_id'], advertise_static_routes
Exemplo n.º 12
0
    def _get_router_edge_id(self, context, vpnservice_id):
        vpnservice = self.service_plugin._get_vpnservice(context,
                                                         vpnservice_id)
        router_id = vpnservice['router_id']
        edge_binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                       router_id)
        if not edge_binding:
            msg = _("Couldn't find edge binding for router %s") % router_id
            raise nsxv_exc.NsxPluginException(err_msg=msg)

        if edge_binding['edge_type'] == nsxv_constants.VDR_EDGE:
            edge_manager = self._core_plugin.edge_manager
            router_id = edge_manager.get_plr_by_tlr_id(context, router_id)
            binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                      router_id)
            edge_id = binding['edge_id']
        else:
            # Get exclusive edge id
            edge_id = edge_binding['edge_id']
        return router_id, edge_id
Exemplo n.º 13
0
    def _get_router_edge_id(self, context, vpnservice_id):
        vpnservice = self.service_plugin._get_vpnservice(
            context, vpnservice_id)
        router_id = vpnservice['router_id']
        edge_binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                       router_id)
        if not edge_binding:
            msg = _("Couldn't find edge binding for router %s") % router_id
            raise nsxv_exc.NsxPluginException(err_msg=msg)

        if edge_binding['edge_type'] == nsxv_constants.VDR_EDGE:
            edge_manager = self._core_plugin.edge_manager
            router_id = edge_manager.get_plr_by_tlr_id(context, router_id)
            binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                      router_id)
            edge_id = binding['edge_id']
        else:
            # Get exclusive edge id
            edge_id = edge_binding['edge_id']
        return router_id, edge_id
Exemplo n.º 14
0
 def _create_l2_gateway_edge(self, context):
     # Create a dedicated DLR
     lrouter = {'name': nsxv_constants.L2_GATEWAY_EDGE,
                'id': uuidutils.generate_uuid()}
     self._edge_manager.create_lrouter(context,
                                       lrouter, lswitch=None, dist=True)
     edge_binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                    lrouter['id'])
     if not edge_binding:
         raise nsx_exc.NsxL2GWDeviceNotFound()
     # Enable edge HA on the DLR
     if cfg.CONF.nsxv.edge_ha:
         edge_id = edge_binding['edge_id']
         self._edge_manager.nsxv_manager.update_edge_ha(edge_id)
     return edge_binding['edge_id']
Exemplo n.º 15
0
def recreate_network_dhcp(context, plugin, edge_manager, old_edge_id, net_id):
    """Handle the DHCP edge recreation of a network
    """
    LOG.info("Moving network %s to a new edge", net_id)
    # delete the old binding
    resource_id = (nsxv_constants.DHCP_EDGE_PREFIX + net_id)[:36]
    nsxv_db.delete_nsxv_router_binding(context.session, resource_id)

    # Delete the old static binding of the networks` compute ports
    port_filters = {'network_id': [net_id], 'device_owner': ['compute:None']}
    compute_ports = plugin.get_ports(context, filters=port_filters)
    if old_edge_id:
        for port in compute_ports:
            # Delete old binding from the DB
            nsxv_db.delete_edge_dhcp_static_binding(context.session,
                                                    old_edge_id,
                                                    port['mac_address'])

    # Go over all the subnets with DHCP
    net_filters = {'network_id': [net_id], 'enable_dhcp': [True]}
    subnets = plugin.get_subnets(context, filters=net_filters)
    for subnet in subnets:
        LOG.info("Moving subnet %s to a new edge", subnet['id'])
        # allocate / reuse the new dhcp edge
        new_resource_id = edge_manager.create_dhcp_edge_service(
            context, net_id, subnet)
        if new_resource_id:
            # also add fw rules and metadata, once for the new edge
            plugin._update_dhcp_service_new_edge(context, resource_id)

    # Update the ip of the dhcp port
    LOG.info("Creating network %s DHCP address group", net_id)
    address_groups = plugin._create_network_dhcp_address_group(context, net_id)
    plugin.edge_manager.update_dhcp_edge_service(context,
                                                 net_id,
                                                 address_groups=address_groups)

    # find out the id of the new edge:
    new_binding = nsxv_db.get_nsxv_router_binding(context.session, resource_id)
    if new_binding:
        LOG.info("Network %(net_id)s was moved to edge %(edge_id)s", {
            'net_id': net_id,
            'edge_id': new_binding['edge_id']
        })
    else:
        LOG.error("Network %(net_id)s was not moved to a new edge",
                  {'net_id': net_id})
Exemplo n.º 16
0
def nsx_recreate_dhcp_edge_by_net_id(net_id):
    """Recreate a dhcp edge for a specific network without an edge"""
    LOG.info("ReCreating NSXv Edge for network: %s", net_id)

    context = n_context.get_admin_context()

    # 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)

        # verify that there is no DHCP edge for this network at the moment
        resource_id = (nsxv_constants.DHCP_EDGE_PREFIX + net_id)[:36]
        router_binding = nsxv_db.get_nsxv_router_binding(
            context.session, resource_id)
        if router_binding:
            # make sure there is no real edge
            if router_binding['edge_id']:
                edge_id = router_binding['edge_id']
                try:
                    nsxv_manager.vcns.get_edge(edge_id)
                except exceptions.ResourceNotFound:
                    # No edge on backend
                    # prevent logger from logging this exception
                    sys.exc_clear()
                    LOG.info("Edge %s does not exist on the NSX", edge_id)
                else:
                    LOG.warning(
                        "Network %(net_id)s already has a dhcp edge: "
                        "%(edge_id)s", {
                            'edge_id': edge_id,
                            'net_id': net_id
                        })
                    return
            # delete this old entry
            nsxv_db.delete_nsxv_router_binding(context.session, resource_id)

        # Verify that the network exists on neutron
        try:
            plugin.get_network(context, net_id)
        except nl_exc.NetworkNotFound:
            LOG.error("Network %s does not exist", net_id)
            return
        recreate_network_dhcp(context, plugin, edge_manager, None, net_id)
Exemplo n.º 17
0
def recreate_network_dhcp(context, plugin, edge_manager, old_edge_id, net_id):
    """Handle the DHCP edge recreation of a network
    """
    LOG.info("Moving network %s to a new edge", net_id)
    # delete the old binding
    resource_id = (nsxv_constants.DHCP_EDGE_PREFIX + net_id)[:36]
    nsxv_db.delete_nsxv_router_binding(context.session, resource_id)

    # Delete the old static binding of the networks` compute ports
    port_filters = {'network_id': [net_id],
                    'device_owner': ['compute:None']}
    compute_ports = plugin.get_ports(context, filters=port_filters)
    if old_edge_id:
        for port in compute_ports:
            # Delete old binding from the DB
            nsxv_db.delete_edge_dhcp_static_binding(context.session,
                old_edge_id, port['mac_address'])

    # Go over all the subnets with DHCP
    net_filters = {'network_id': [net_id], 'enable_dhcp': [True]}
    subnets = plugin.get_subnets(context, filters=net_filters)
    for subnet in subnets:
        LOG.info("Moving subnet %s to a new edge", subnet['id'])
        # allocate / reuse the new dhcp edge
        new_resource_id = edge_manager.create_dhcp_edge_service(
            context, net_id, subnet)
        if new_resource_id:
            # also add fw rules and metadata, once for the new edge
            plugin._update_dhcp_service_new_edge(context, resource_id)

    # Update the ip of the dhcp port
    LOG.info("Creating network %s DHCP address group", net_id)
    address_groups = plugin._create_network_dhcp_address_group(
        context, net_id)
    plugin.edge_manager.update_dhcp_edge_service(
        context, net_id, address_groups=address_groups)

    # find out the id of the new edge:
    new_binding = nsxv_db.get_nsxv_router_binding(
        context.session, resource_id)
    if new_binding:
        LOG.info("Network %(net_id)s was moved to edge %(edge_id)s",
                 {'net_id': net_id, 'edge_id': new_binding['edge_id']})
    else:
        LOG.error("Network %(net_id)s was not moved to a new edge",
                 {'net_id': net_id})
Exemplo n.º 18
0
def nsx_recreate_dhcp_edge_by_net_id(net_id):
    """Recreate a dhcp edge for a specific network without an edge"""
    LOG.info("ReCreating NSXv Edge for network: %s", net_id)

    context = n_context.get_admin_context()

    # 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)

        # verify that there is no DHCP edge for this network at the moment
        resource_id = (nsxv_constants.DHCP_EDGE_PREFIX + net_id)[:36]
        router_binding = nsxv_db.get_nsxv_router_binding(
            context.session, resource_id)
        if router_binding:
            # make sure there is no real edge
            if router_binding['edge_id']:
                edge_id = router_binding['edge_id']
                try:
                    nsxv_manager.vcns.get_edge(edge_id)
                except exceptions.ResourceNotFound:
                    # No edge on backend
                    # prevent logger from logging this exception
                    sys.exc_clear()
                    LOG.info("Edge %s does not exist on the NSX", edge_id)
                else:
                    LOG.warning("Network %(net_id)s already has a dhcp edge: "
                                "%(edge_id)s",
                                {'edge_id': edge_id,
                                 'net_id': net_id})
                    return
            # delete this old entry
            nsxv_db.delete_nsxv_router_binding(context.session, resource_id)

        # Verify that the network exists on neutron
        try:
            plugin.get_network(context, net_id)
        except nl_exc.NetworkNotFound:
            LOG.error("Network %s does not exist", net_id)
            return
        recreate_network_dhcp(context, plugin, edge_manager,
                              None, net_id)
Exemplo n.º 19
0
 def _create_l2_gateway_edge(self, context):
     # Create a dedicated DLR
     lrouter = {'name': nsxv_constants.L2_GATEWAY_EDGE,
                'id': uuidutils.generate_uuid()}
     # Create the router on the default availability zone
     availability_zone = (nsx_az.ConfiguredAvailabilityZones().
         get_default_availability_zone())
     self._edge_manager.create_lrouter(context,
                                       lrouter, lswitch=None, dist=True,
                                       availability_zone=availability_zone)
     edge_binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                    lrouter['id'])
     if not edge_binding:
         raise nsx_exc.NsxL2GWDeviceNotFound()
     # Enable edge HA on the DLR
     if availability_zone.edge_ha:
         edge_id = edge_binding['edge_id']
         self._edge_manager.nsxv_manager.update_edge_ha(edge_id)
     return edge_binding['edge_id']
Exemplo n.º 20
0
 def _create_l2_gateway_edge(self, context):
     # Create a dedicated DLR
     lrouter = {
         'name': nsxv_constants.L2_GATEWAY_EDGE,
         'id': uuidutils.generate_uuid()
     }
     self._edge_manager.create_lrouter(context,
                                       lrouter,
                                       lswitch=None,
                                       dist=True)
     edge_binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                    lrouter['id'])
     if not edge_binding:
         raise nsx_exc.NsxL2GWDeviceNotFound()
     # Enable edge HA on the DLR
     if cfg.CONF.nsxv.edge_ha:
         edge_id = edge_binding['edge_id']
         self._edge_manager.nsxv_manager.update_edge_ha(edge_id)
     return edge_binding['edge_id']
Exemplo n.º 21
0
def get_lbaas_edge_id_for_subnet(context, plugin, subnet_id, tenant_id):
    """
    Grab the id of an Edge appliance that is connected to subnet_id.
    """
    subnet = plugin.get_subnet(context, subnet_id)
    net_id = subnet.get('network_id')
    filters = {'network_id': [net_id],
               'device_owner': ['network:router_interface'],
               'tenant_id': [tenant_id]}
    attached_routers = plugin.get_ports(context.elevated(),
                                        filters=filters,
                                        fields=['device_id'])

    for attached_router in attached_routers:
        router = plugin.get_router(context, attached_router['device_id'])
        if router['router_type'] == 'exclusive':
            rtr_bindings = nsxv_db.get_nsxv_router_binding(context.session,
                                                           router['id'])
            return rtr_bindings['edge_id']
Exemplo n.º 22
0
 def _create_l2_gateway_edge(self, context):
     # Create a dedicated DLR
     lrouter = {'name': nsxv_constants.L2_GATEWAY_EDGE,
                'id': uuidutils.generate_uuid()}
     # Create the router on the default availability zone
     availability_zone = (nsx_az.NsxVAvailabilityZones().
         get_default_availability_zone())
     self._edge_manager.create_lrouter(context,
                                       lrouter, lswitch=None, dist=True,
                                       availability_zone=availability_zone)
     edge_binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                    lrouter['id'])
     if not edge_binding:
         raise nsx_exc.NsxL2GWDeviceNotFound()
     # Enable edge HA on the DLR
     if availability_zone.edge_ha:
         edge_id = edge_binding['edge_id']
         self._edge_manager.nsxv_manager.update_edge_ha(edge_id)
     return edge_binding['edge_id']
Exemplo n.º 23
0
def migrate_distributed_routers_dhcp(resource, event, trigger, **kwargs):
    context = n_context.get_admin_context()
    nsxv = utils.get_nsxv_client()
    with utils.NsxVPluginWrapper() as plugin:
        routers = plugin.get_routers(context)
        for router in routers:
            if router.get('distributed', False):
                binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                          router['id'])
                if binding:
                    edge_id = binding['edge_id']
                    with locking.LockManager.get_lock(edge_id):
                        route_obj = nsxv.get_routes(edge_id)[1]
                        routes = route_obj.get('staticRoutes', {}
                                               ).get('staticRoutes', [])
                        new_routes = [route for route in routes if route.get(
                            'network') != '169.254.169.254/32']
                        route_obj['staticRoutes']['staticRoutes'] = new_routes

                        nsxv.update_routes(edge_id, route_obj)
Exemplo n.º 24
0
def get_lbaas_edge_id_for_subnet(context, plugin, subnet_id, tenant_id):
    """
    Grab the id of an Edge appliance that is connected to subnet_id.
    """
    subnet = plugin.get_subnet(context, subnet_id)
    net_id = subnet.get('network_id')
    filters = {
        'network_id': [net_id],
        'device_owner': ['network:router_interface'],
        'tenant_id': [tenant_id]
    }
    attached_routers = plugin.get_ports(context.elevated(),
                                        filters=filters,
                                        fields=['device_id'])

    for attached_router in attached_routers:
        router = plugin.get_router(context, attached_router['device_id'])
        if router['router_type'] == 'exclusive':
            rtr_bindings = nsxv_db.get_nsxv_router_binding(
                context.session, router['id'])
            return rtr_bindings['edge_id']
Exemplo n.º 25
0
def nsx_recreate_dhcp_edge_by_net_id(net_id):
    """Recreate a dhcp edge for a specific network without an edge"""
    LOG.info("ReCreating NSXv Edge for network: %s", net_id)

    context = n_context.get_admin_context()

    # verify that there is no DHCP edge for this network at the moment
    resource_id = (nsxv_constants.DHCP_EDGE_PREFIX + net_id)[:36]
    router_binding = nsxv_db.get_nsxv_router_binding(context.session,
                                                     resource_id)
    if router_binding:
        # make sure there is no edge
        if router_binding['edge_id']:
            LOG.warning(
                "Network %(net_id)s already has a dhcp edge: "
                "%(egde_id)s", {
                    'edge_id': router_binding['edge_id'],
                    'net_id': net_id
                })
            return
        # delete this old entry
        nsxv_db.delete_nsxv_router_binding(context.session, resource_id)

    # 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)

        # check if this network is attached to a distributed router
        vdr_router_id = _get_net_vdr_router_id(plugin, context, net_id)
        if vdr_router_id:
            # recreate the edge as a VDR DHCP edge
            recreate_vdr_dhcp_edge(context, plugin, edge_manager,
                                   vdr_router_id)
        else:
            # This is a regular DHCP edge:
            recreate_network_dhcp(context, plugin, edge_manager, None, net_id)
Exemplo n.º 26
0
def nsx_redistribute_dhcp_edges(resource, event, trigger, **kwargs):
    """If any of the DHCP networks are on a conflicting edge move them"""
    context = n_context.get_admin_context()
    with utils.NsxVPluginWrapper() as plugin:
        nsxv_manager = vcns_driver.VcnsDriver(edge_utils.NsxVCallbacks(plugin))
        edge_manager = edge_utils.EdgeManager(nsxv_manager, plugin)
        # go over all DHCP subnets
        networks = plugin.get_networks(context)
        for network in networks:
            network_id = network['id']
            # Check if the network has a related DHCP edge
            resource_id = (nsxv_constants.DHCP_EDGE_PREFIX + network_id)[:36]
            dhcp_edge_binding = nsxv_db.get_nsxv_router_binding(
                context.session, resource_id)
            if not dhcp_edge_binding:
                continue
            LOG.info("Checking network %s", network_id)
            edge_id = dhcp_edge_binding['edge_id']
            availability_zone = plugin.get_network_az_by_net_id(
                context, network['id'])
            filters = {'network_id': [network_id], 'enable_dhcp': [True]}
            subnets = plugin.get_subnets(context, filters=filters)
            for subnet in subnets:
                (conflict_edge_ids,
                 available_edge_ids) = edge_manager._get_used_edges(
                     context, subnet, availability_zone)
                if edge_id in conflict_edge_ids:
                    # move the DHCP to another edge
                    LOG.info(
                        "Network %(net)s on DHCP edge %(edge)s is "
                        "conflicting with another network and will be "
                        "moved", {
                            'net': network_id,
                            'edge': edge_id
                        })
                    edge_manager.remove_network_from_dhcp_edge(
                        context, network_id, edge_id)
                    edge_manager.create_dhcp_edge_service(
                        context, network_id, subnet)
                    break
Exemplo n.º 27
0
def migrate_distributed_routers_dhcp(resource, event, trigger, **kwargs):
    context = n_context.get_admin_context()
    nsxv = utils.get_nsxv_client()
    with utils.NsxVPluginWrapper() as plugin:
        routers = plugin.get_routers(context)
        for router in routers:
            if router.get('distributed', False):
                binding = nsxv_db.get_nsxv_router_binding(
                    context.session, router['id'])
                if binding:
                    edge_id = binding['edge_id']
                    with locking.LockManager.get_lock(edge_id):
                        route_obj = nsxv.get_routes(edge_id)[1]
                        routes = route_obj.get('staticRoutes',
                                               {}).get('staticRoutes', [])
                        new_routes = [
                            route for route in routes
                            if route.get('network') != '169.254.169.254/32'
                        ]
                        route_obj['staticRoutes']['staticRoutes'] = new_routes

                        nsxv.update_routes(edge_id, route_obj)
Exemplo n.º 28
0
def nsx_redistribute_dhcp_edges(resource, event, trigger, **kwargs):
    """If any of the DHCP networks are on a conflicting edge move them"""
    context = n_context.get_admin_context()
    with utils.NsxVPluginWrapper() as plugin:
        nsxv_manager = vcns_driver.VcnsDriver(
                           edge_utils.NsxVCallbacks(plugin))
        edge_manager = edge_utils.EdgeManager(nsxv_manager, plugin)
        # go over all DHCP subnets
        networks = plugin.get_networks(context)
        for network in networks:
            network_id = network['id']
            # Check if the network has a related DHCP edge
            resource_id = (nsxv_constants.DHCP_EDGE_PREFIX + network_id)[:36]
            dhcp_edge_binding = nsxv_db.get_nsxv_router_binding(
                context.session, resource_id)
            if not dhcp_edge_binding:
                continue
            LOG.info("Checking network %s", network_id)
            edge_id = dhcp_edge_binding['edge_id']
            availability_zone = plugin.get_network_az_by_net_id(
                context, network['id'])
            filters = {'network_id': [network_id], 'enable_dhcp': [True]}
            subnets = plugin.get_subnets(context, filters=filters)
            for subnet in subnets:
                (conflict_edge_ids,
                 available_edge_ids) = edge_manager._get_used_edges(
                    context, subnet, availability_zone)
                if edge_id in conflict_edge_ids:
                    # move the DHCP to another edge
                    LOG.info("Network %(net)s on DHCP edge %(edge)s is "
                             "conflicting with another network and will be "
                             "moved",
                             {'net': network_id, 'edge': edge_id})
                    edge_manager.remove_network_from_dhcp_edge(
                        context, network_id, edge_id)
                    edge_manager.create_dhcp_edge_service(
                        context, network_id, subnet)
                    break
Exemplo n.º 29
0
 def _get_router_edge_id(self, context, router_id):
     binding = nsxv_db.get_nsxv_router_binding(context.session, router_id)
     return binding['edge_id']
 def _get_router_edge_id(self, context, router_id):
     binding = nsxv_db.get_nsxv_router_binding(context.session, router_id)
     return binding['edge_id']