def setUp(self):
     super(EdgeManagerTestCase, self).setUp()
     cfg.CONF.set_override('backup_edge_pool', [], 'nsxv')
     self.edge_manager = edge_utils.EdgeManager(self.nsxv_manager, None)
     self.check = mock.patch.object(self.edge_manager,
                                    'check_edge_active_at_backend').start()
     self.check.side_effect = self.check_edge_active_at_backend
     self.default_edge_pool_dicts = {
         nsxv_constants.SERVICE_EDGE: {
             nsxv_constants.LARGE: {
                 'minimum_pooled_edges': 1,
                 'maximum_pooled_edges': 3
             },
             nsxv_constants.COMPACT: {
                 'minimum_pooled_edges': 1,
                 'maximum_pooled_edges': 3
             }
         },
         nsxv_constants.VDR_EDGE: {}
     }
     self.vdr_edge_pool_dicts = {
         nsxv_constants.SERVICE_EDGE: {},
         nsxv_constants.VDR_EDGE: {
             nsxv_constants.LARGE: {
                 'minimum_pooled_edges': 1,
                 'maximum_pooled_edges': 3
             }
         }
     }
示例#2
0
def nsx_recreate_router_edge(old_edge_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)
        context = n_context.get_admin_context()

        # verify that this is a Router edge
        router_ids = edge_manager.get_routers_on_edge(context, old_edge_id)
        if not router_ids:
            LOG.error("Edge %(edge_id)s is not a router edge",
                      {'edge_id': old_edge_id})
            return

        # all the routers on the same edge have the same type, so it
        # is ok to check the type once
        example_router = plugin.get_router(context, router_ids[0])
        if example_router.get('distributed'):
            LOG.error("Recreating a distributed router edge is not "
                      "supported")
            return
        router_driver = plugin._router_managers.get_tenant_router_driver(
            context, example_router['router_type'])

        # load all the routers before deleting their binding
        routers = []
        for router_id in router_ids:
            routers.append(plugin.get_router(context, router_id))

        # delete the backend edge and all the relevant DB entries
        delete_old_edge(context, old_edge_id)

        # Go over all the relevant routers
        for router in routers:
            router_id = router['id']
            az_name = _get_router_az_from_plugin_router(router)
            # clean up other objects related to this router
            if plugin.metadata_proxy_handler:
                md_proxy = plugin.get_metadata_proxy_handler(az_name)
                md_proxy.cleanup_router_edge(context, router_id)

            # 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
            })
示例#3
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)
示例#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)
    def setUp(self):
        super(EdgeUtilsTestCase, self).setUp()
        self.edge_manager = edge_utils.EdgeManager(self.nsxv_manager, None)

        # Args for vcns interface configuration
        self.internal_ip = '10.0.0.1'
        self.uplink_ip = '192.168.111.30'
        self.subnet_mask = '255.255.255.0'
        self.pref_len = '24'
        self.edge_id = 'dummy'
        self.orig_vnics = ({}, {
            'vnics': [{
                'addressGroups': {
                    'addressGroups': [{
                        'subnetMask': self.subnet_mask,
                        'subnetPrefixLength': self.pref_len,
                        'primaryAddress': self.uplink_ip
                    }]
                },
                'type': 'uplink',
                'index': 1
            }, {
                'addressGroups': {
                    'addressGroups': [{
                        'subnetMask': self.subnet_mask,
                        'subnetPrefixLength': self.pref_len,
                        'primaryAddress': self.internal_ip
                    }]
                },
                'type': 'internal',
                'index': 2
            }]
        })

        # Args for vcns vdr interface configuration
        self.vdr_ip = '10.0.0.1'
        self.vnic = 1
        self.orig_vdr = ({}, {
            'index': 2,
            'addressGroups': {
                'addressGroups': [{
                    'subnetMask': self.subnet_mask,
                    'subnetPrefixLength': self.pref_len,
                    'primaryAddress': self.vdr_ip
                }]
            },
            'type': 'internal'
        })
示例#6
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)
示例#7
0
def nsx_update_dhcp_edge_binding(resource, event, trigger, **kwargs):
    """Resync DHCP bindings on NSXv Edge"""

    if not kwargs['property']:
        LOG.error(_LE("Need to specify edge-id parameter"))
        return
    else:
        properties = admin_utils.parse_multi_keyval_opt(kwargs['property'])
        edge_id = properties.get('edge-id')
        LOG.info(_LI("Updating NSXv Edge: %s"), edge_id)
        # Need to create a NeutronDbPlugin object; so that we are able to
        # do neutron list-ports.
        plugin = db_base_plugin_v2.NeutronDbPluginV2()
        nsxv_manager = vcns_driver.VcnsDriver(edge_utils.NsxVCallbacks(plugin))
        edge_manager = edge_utils.EdgeManager(nsxv_manager, plugin)
        try:
            edge_manager.update_dhcp_service_config(neutron_db.context,
                                                    edge_id)
        except exceptions.ResourceNotFound:
            LOG.error(_LE("Edge %s not found"), edge_id)
示例#8
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)
示例#9
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
示例#10
0
def get_orphaned_router_bindings():
    context = n_context.get_admin_context()
    orphaned_list = []

    with utils.NsxVPluginWrapper() as plugin:
        networks = plugin.get_networks(context, fields=['id'])
        net_ids = [x['id'] for x in networks]
        routers = plugin.get_routers(context, fields=['id'])
        rtr_ids = [x['id'] for x in routers]

        nsxv_manager = vcns_driver.VcnsDriver(edge_utils.NsxVCallbacks(plugin))
        edge_manager = edge_utils.EdgeManager(nsxv_manager, plugin)
        plr_tlr_ids = {}
        for tlr_id in rtr_ids:
            plr_id = edge_manager.get_plr_by_tlr_id(context, tlr_id)
            if plr_id:
                plr_tlr_ids[plr_id] = tlr_id

        for binding in get_router_edge_bindings():
            if not router_binding_obj_exist(context, binding, net_ids, rtr_ids,
                                            plr_tlr_ids):
                orphaned_list.append(binding)
    return orphaned_list
示例#11
0
 def setUp(self):
     super(EdgeDHCPManagerTestCase, self).setUp()
     self.edge_manager = edge_utils.EdgeManager(self.nsxv_manager, None)
     self.check = mock.patch.object(self.edge_manager,
                                    'check_edge_active_at_backend').start()
     self.check.return_value = True
示例#12
0
def nsx_recreate_router_edge(resource, event, trigger, **kwargs):
    """Recreate a router edge with all the data on 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 Router 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 Router edge
    router_ids = edge_manager.get_routers_on_edge(context, old_edge_id)
    if not router_ids:
        LOG.error(_LE("Edge %(edge_id)s is not a router edge"),
                  {'edge_id': old_edge_id})
        return

    # all the routers on the same edge have the same type, so it
    # is ok to check the type once
    example_router = plugin.get_router(context, router_ids[0])
    router_driver = plugin._router_managers.get_tenant_router_driver(
        context, example_router['router_type'])
    if router_driver.get_type() == "distributed":
        LOG.error(
            _LE("Recreating a distributed  driver edge is not "
                "supported"))
        return

    # load all the routers before deleting their binding
    routers = []
    for router_id in router_ids:
        routers.append(plugin.get_router(context, router_id))

    # delete the backend edge and all the relevant DB entries
    delete_old_edge(context, old_edge_id)

    # Go over all the relevant routers
    for router in routers:
        router_id = router['id']
        # clean up other objects related to this router
        if plugin.metadata_proxy_handler:
            plugin.metadata_proxy_handler.cleanup_router_edge(router_id)

        # 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(_LI("Router %(router)s was attached to edge %(edge)s"), {
            'router': router_id,
            'edge': new_edge_id
        })