def create_l2_gateway_connection_postcommit(self, context, gw_connection): network_id = gw_connection.get('network_id') virtual_wire = nsx_db.get_nsx_switch_ids(context.session, network_id) # In NSX-v, there will be only one device configured per L2 gateway. # The name of the device shall carry the backend DLR. l2gw_id = gw_connection.get(l2gw_const.L2GATEWAY_ID) device = self._get_device(context, l2gw_id) device_name = device.get('device_name') device_id = device.get('id') interface = self._get_l2_gw_interfaces(context, device_id) interface_name = interface[0].get("interface_name") bridge_name = "bridge-" + uuidutils.generate_uuid() bridge_dict = { "bridges": { "bridge": { "name": bridge_name, "virtualWire": virtual_wire[0], "dvportGroup": interface_name } } } try: self._nsxv.create_bridge(device_name, bridge_dict) except exceptions.VcnsApiException: LOG.exception("Failed to update NSX, " "rolling back changes on neutron.") raise l2gw_exc.L2GatewayServiceDriverError( method='create_l2_gateway_connection_postcommit') return
def get_nsx_switch_ids(session, cluster, neutron_network_id): """Return the NSX switch id for a given neutron network. First lookup for mappings in Neutron database. If no mapping is found, query the NSX backend and add the mappings. """ nsx_switch_ids = nsx_db.get_nsx_switch_ids( session, neutron_network_id) if not nsx_switch_ids: # Find logical switches from backend. # This is a rather expensive query, but it won't be executed # more than once for each network in Neutron's lifetime nsx_switches = switchlib.get_lswitches(cluster, neutron_network_id) if not nsx_switches: LOG.warning("Unable to find NSX switches for Neutron network " "%s", neutron_network_id) return nsx_switch_ids = [] with session.begin(subtransactions=True): for nsx_switch in nsx_switches: nsx_switch_id = nsx_switch['uuid'] nsx_switch_ids.append(nsx_switch_id) # Create DB mapping nsx_db.add_neutron_nsx_network_mapping( session, neutron_network_id, nsx_switch_id) return nsx_switch_ids
def get_nsx_switch_ids(session, cluster, neutron_network_id): """Return the NSX switch id for a given neutron network. First lookup for mappings in Neutron database. If no mapping is found, query the NSX backend and add the mappings. """ nsx_switch_ids = nsx_db.get_nsx_switch_ids(session, neutron_network_id) if not nsx_switch_ids: # Find logical switches from backend. # This is a rather expensive query, but it won't be executed # more than once for each network in Neutron's lifetime nsx_switches = switchlib.get_lswitches(cluster, neutron_network_id) if not nsx_switches: LOG.warning( "Unable to find NSX switches for Neutron network " "%s", neutron_network_id) return nsx_switch_ids = [] with session.begin(subtransactions=True): for nsx_switch in nsx_switches: nsx_switch_id = nsx_switch['uuid'] nsx_switch_ids.append(nsx_switch_id) # Create DB mapping nsx_db.add_neutron_nsx_network_mapping(session, neutron_network_id, nsx_switch_id) return nsx_switch_ids
def handle_qos_notification(policy_obj, event_type, dvs): # Check if QoS policy rule was created/deleted/updated # Only if the policy rule was updated, we need to update the dvs if (event_type == callbacks_events.UPDATED and hasattr(policy_obj, "rules")): # Reload the policy as admin so we will have a context context = n_context.get_admin_context() admin_policy = qos_policy.QosPolicy.get_object( context, id=policy_obj.id) # get all the bound networks of this policy networks = admin_policy.get_bound_networks() qos_rule = NsxVQosRule(context=context, qos_policy_id=policy_obj.id) for net_id in networks: # update the new bw limitations for this network net_morefs = nsx_db.get_nsx_switch_ids(context.session, net_id) for moref in net_morefs: # update the qos restrictions of the network dvs.update_port_groups_config( net_id, moref, dvs.update_port_group_spec_qos, qos_rule)
def create_l2_gateway_connection_postcommit(self, context, gw_connection): network_id = gw_connection.get('network_id') virtual_wire = nsx_db.get_nsx_switch_ids(context.session, network_id) # In NSX-v, there will be only one device configured per L2 gateway. # The name of the device shall carry the backend DLR. l2gw_id = gw_connection.get(l2gw_const.L2GATEWAY_ID) device = self._get_device(context, l2gw_id) device_name = device.get('device_name') device_id = device.get('id') interface = self._get_l2_gw_interfaces(context, device_id) interface_name = interface[0].get("interface_name") # bridge name length cannot exceed 40 characters bridge_name = "brg-" + uuidutils.generate_uuid() bridge_dict = {"bridges": {"bridge": {"name": bridge_name, "virtualWire": virtual_wire[0], "dvportGroup": interface_name}}} try: self._nsxv.create_bridge(device_name, bridge_dict) except exceptions.VcnsApiException: LOG.exception("Failed to update NSX, " "rolling back changes on neutron.") raise l2gw_exc.L2GatewayServiceDriverError( method='create_l2_gateway_connection_postcommit') return
def get_network_nsx_id(session, neutron_id): # get the nsx switch id from the DB mapping mappings = nsx_db.get_nsx_switch_ids(session, neutron_id) if not mappings or len(mappings) == 0: LOG.debug("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 get_network_nsx_id(session, neutron_id): # get the nsx switch id from the DB mapping mappings = nsx_db.get_nsx_switch_ids(session, neutron_id) if not mappings or len(mappings) == 0: LOG.debug("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 handle_qos_policy_notification(policy_obj, dvs): # Reload the policy as admin so we will have a context context = n_context.get_admin_context() admin_policy = qos_policy.QosPolicy.get_object(context, id=policy_obj.id) # get all the bound networks of this policy networks = admin_policy.get_bound_networks() qos_rule = NsxVQosRule(context=context, qos_policy_id=policy_obj.id) for net_id in networks: # update the new bw limitations for this network net_morefs = nsx_db.get_nsx_switch_ids(context.session, net_id) for moref in net_morefs: # update the qos restrictions of the network dvs.update_port_groups_config(net_id, moref, dvs.update_port_group_spec_qos, qos_rule)
def create_l2_gateway_connection(self, context, l2_gateway_connection): """Create a L2 gateway connection.""" gw_connection = l2_gateway_connection.get( l2gw_const.CONNECTION_RESOURCE_NAME) l2gw_id = gw_connection.get(l2gw_const.L2GATEWAY_ID) gw_db = self._get_l2_gateway(context, l2gw_id) if gw_db.network_connections: raise nsx_exc.NsxL2GWInUse(gateway_id=l2gw_id) l2gw_connection = super(NsxvL2GatewayDriver, self).create_l2_gateway_connection( context, l2_gateway_connection) network_id = gw_connection.get('network_id') virtual_wire = nsx_db.get_nsx_switch_ids(context.session, network_id) # In NSX-v, there will be only one device configured per L2 gateway. # The name of the device shall carry the backend DLR. device = self._get_device(context, l2gw_id) device_name = device.get('device_name') device_id = device.get('id') interface = self._get_l2_gw_interfaces(context, device_id) interface_name = interface[0].get("interface_name") bridge_name = "bridge-" + uuidutils.generate_uuid() bridge_dict = { "bridges": { "bridge": { "name": bridge_name, "virtualWire": virtual_wire[0], "dvportGroup": interface_name } } } try: self._nsxv.create_bridge(device_name, bridge_dict) except exceptions.VcnsApiException: with excutils.save_and_reraise_exception(): super(NsxvL2GatewayDriver, self).delete_l2_gateway_connection( context, l2gw_connection['id']) LOG.exception( _LE("Failed to update NSX, " "rolling back changes on neutron")) return l2gw_connection
def test_metadata_proxy_with_get_subnets(self): # Test if get_subnets() handles advanced-service-provider extension, # which is used when processing metadata requests. with self.network() as n1, self.network() as n2: with self.subnet(network=n1) as s1, self.subnet(network=n2) as s2: # Get all the subnets. subnets = self._list('subnets')['subnets'] self.assertEqual(len(subnets), 2) self.assertEqual(set([s['id'] for s in subnets]), set([s1['subnet']['id'], s2['subnet']['id']])) lswitch_id = nsx_db.get_nsx_switch_ids( context.get_admin_context().session, n1['network']['id'])[0] # Get only the subnets associated with a particular advanced # service provider (i.e. logical switch). subnets = self._list('subnets', query_params='%s=%s' % (as_providers.ADV_SERVICE_PROVIDERS, lswitch_id))['subnets'] self.assertEqual(len(subnets), 1) self.assertEqual(subnets[0]['id'], s1['subnet']['id'])
def handle_qos_notification(policy_obj, event_type, dvs): # Check if QoS policy rule was created/deleted/updated # Only if the policy rule was updated, we need to update the dvs if (event_type == callbacks_events.UPDATED and hasattr(policy_obj, "rules")): # Reload the policy as admin so we will have a context context = n_context.get_admin_context() admin_policy = qos_policy.QosPolicy.get_object(context, id=policy_obj.id) # get all the bound networks of this policy networks = admin_policy.get_bound_networks() qos_rule = NsxVQosRule(context=context, qos_policy_id=policy_obj.id) for net_id in networks: # update the new bw limitations for this network net_morefs = nsx_db.get_nsx_switch_ids(context.session, net_id) for moref in net_morefs: # update the qos restrictions of the network dvs.update_port_groups_config(net_id, moref, dvs.update_port_group_spec_qos, qos_rule)
def create_l2_gateway_connection(self, context, l2_gateway_connection): """Create a L2 gateway connection.""" gw_connection = l2_gateway_connection.get(l2gw_const. CONNECTION_RESOURCE_NAME) l2gw_id = gw_connection.get(l2gw_const.L2GATEWAY_ID) gw_db = self._get_l2_gateway(context, l2gw_id) if gw_db.network_connections: raise nsx_exc.NsxL2GWInUse(gateway_id=l2gw_id) l2gw_connection = super( NsxvL2GatewayDriver, self).create_l2_gateway_connection( context, l2_gateway_connection) network_id = gw_connection.get('network_id') virtual_wire = nsx_db.get_nsx_switch_ids(context.session, network_id) # In NSX-v, there will be only one device configured per L2 gateway. # The name of the device shall carry the backend DLR. device = self._get_device(context, l2gw_id) device_name = device.get('device_name') device_id = device.get('id') interface = self._get_l2_gw_interfaces(context, device_id) interface_name = interface[0].get("interface_name") bridge_name = "bridge-" + uuidutils.generate_uuid() bridge_dict = {"bridges": {"bridge": {"name": bridge_name, "virtualWire": virtual_wire[0], "dvportGroup": interface_name}}} try: self._nsxv.create_bridge(device_name, bridge_dict) except exceptions.VcnsApiException: with excutils.save_and_reraise_exception(): super(NsxvL2GatewayDriver, self).delete_l2_gateway_connection( context, l2gw_connection['id']) LOG.exception(_LE("Failed to update NSX, " "rolling back changes on neutron")) return l2gw_connection
def net_id_to_lswitch_id(self, net_id): lswitch_ids = nsx_db.get_nsx_switch_ids(self.context.session, net_id) return lswitch_ids[0] if lswitch_ids else None
def get_network_nsx_id(context, neutron_id): # get the nsx switch id from the DB mapping mappings = nsx_db.get_nsx_switch_ids(context.session, neutron_id) if mappings and len(mappings) > 0: return mappings[0]
def get_network_nsx_id(context, neutron_id): # get the nsx switch id from the DB mapping mappings = nsx_db.get_nsx_switch_ids(context.session, neutron_id) if mappings and len(mappings) > 0: return mappings[0]
def net_id_to_lswitch_id(self, net_id): lswitch_ids = nsx_db.get_nsx_switch_ids(self.context.session, net_id) return lswitch_ids[0] if lswitch_ids else None