diff --git a/vmware_nsx/plugins/nsx_v3/plugin.py b/vmware_nsx/plugins/nsx_v3/plugin.py index b801f5a0ef..6f272a7c0c 100644 --- a/vmware_nsx/plugins/nsx_v3/plugin.py +++ b/vmware_nsx/plugins/nsx_v3/plugin.py @@ -357,10 +357,18 @@ class NsxV3Plugin(addr_pair_db.AllowedAddressPairsMixin, pnet.SEGMENTATION_ID)) net_type, physical_net, vlan_id = self._validate_provider_create( context, net_data) - net_name = net_data['name'] + neutron_net_id = uuidutils.generate_uuid() + # update the network name to indicate the neutron id too. + net_name = utils.get_name_and_uuid(net_data['name'] or 'network', + neutron_net_id) tags = utils.build_v3_tags_payload( net_data, resource_type='os-neutron-net-id', project_name=context.tenant_name) + # update the neutron id in the tags + for tag in tags: + if tag['scope'] == 'os-neutron-net-id': + tag['tag'] = neutron_net_id + admin_state = net_data.get('admin_state_up', True) # Create network on the backend @@ -371,31 +379,16 @@ class NsxV3Plugin(addr_pair_db.AllowedAddressPairsMixin, 'tags': tags, 'admin_state': admin_state, 'vlan_id': vlan_id}) - result = nsxlib.create_logical_switch(net_name, physical_net, tags, + nsx_result = nsxlib.create_logical_switch(net_name, physical_net, tags, admin_state=admin_state, vlan_id=vlan_id) - # The logical switch's UUID is used as the id for the neutron network - # (via net_data, which is used below, in create_network). Now that we - # have a UUID for the logical switch, set that as the neutron network - # id in the switch's tags. Note errors but no rollback is needed here. - network_id = result['id'] - net_data['id'] = network_id - network_tags = result['tags'] - for tag in network_tags: - if tag['scope'] == 'os-neutron-net-id': - tag['tag'] = network_id - try: - nsxlib.update_logical_switch( - network_id, - name=utils.get_name_and_uuid(net_data['name'] or 'network', - network_id), - tags=network_tags) - except nsx_exc.ManagerError: - LOG.exception(_LE("Unable to update network name and tags on NSX " - "backend for network %s"), network_id) - - return (is_provider_net, net_type, physical_net, vlan_id) + net_data['id'] = neutron_net_id + return (is_provider_net, + net_type, + physical_net, + vlan_id, + nsx_result['id']) def _extend_network_dict_provider(self, context, network, bindings=None): if not bindings: @@ -411,12 +404,14 @@ class NsxV3Plugin(addr_pair_db.AllowedAddressPairsMixin, def create_network(self, context, network): net_data = network['network'] external = net_data.get(ext_net_extn.EXTERNAL) + is_backend_network = False if attributes.is_attr_set(external) and external: is_provider_net, net_type, physical_net, vlan_id = ( self._validate_external_net_create(net_data)) else: - is_provider_net, net_type, physical_net, vlan_id = ( + is_provider_net, net_type, physical_net, vlan_id, nsx_net_id = ( self._create_network_at_the_backend(context, net_data)) + is_backend_network = True tenant_id = net_data['tenant_id'] self._ensure_default_security_group(context, tenant_id) @@ -449,6 +444,15 @@ class NsxV3Plugin(addr_pair_db.AllowedAddressPairsMixin, net_type, physical_net, vlan_id)] self._extend_network_dict_provider(context, created_net, bindings=net_bindings) + if is_backend_network: + # Add neutron-id <-> nsx-id mapping to the DB + # after the network creation is done + neutron_net_id = created_net['id'] + nsx_db.add_neutron_nsx_network_mapping( + context.session, + neutron_net_id, + nsx_net_id) + except Exception: with excutils.save_and_reraise_exception(): # Undo creation on the backend @@ -502,6 +506,7 @@ class NsxV3Plugin(addr_pair_db.AllowedAddressPairsMixin, raise def delete_network(self, context, network_id): + nsx_net_id = self._get_network_nsx_id(context, network_id) # First call DB operation for delete network as it will perform # checks on active ports self._retry_delete_network(context, network_id) @@ -509,11 +514,24 @@ class NsxV3Plugin(addr_pair_db.AllowedAddressPairsMixin, # TODO(salv-orlando): Handle backend failure, possibly without # requiring us to un-delete the DB object. For instance, ignore # failures occurring if logical switch is not found - nsxlib.delete_logical_switch(network_id) + nsxlib.delete_logical_switch(nsx_net_id) else: # TODO(berlin): delete subnets public announce on the network pass + def _get_network_nsx_id(self, context, neutron_id): + # get the nsx switch id from the DB mapping + mappings = nsx_db.get_nsx_switch_ids(context.session, neutron_id) + if not mappings or len(mappings) == 0: + LOG.warning(_LW("Unable to find NSX mappings for neutron " + "network:%s"), neutron_id) + # fallback in case we didn't find the id in the db mapping + # This should not happen, but added here in case the network was + # created before this code was added. + return neutron_id + else: + return mappings[0] + def update_network(self, context, id, network): original_net = super(NsxV3Plugin, self).get_network(context, id) net_data = network['network'] @@ -531,8 +549,10 @@ class NsxV3Plugin(addr_pair_db.AllowedAddressPairsMixin, if (not self._network_is_external(context, id) and 'name' in net_data or 'admin_state_up' in net_data): try: + # get the nsx switch id from the DB mapping + nsx_id = self._get_network_nsx_id(context, id) nsxlib.update_logical_switch( - id, + nsx_id, name=utils.get_name_and_uuid(net_data['name'] or 'network', id), admin_state=net_data.get('admin_state_up'))