def process_create_port(self, context, data, result): """Implementation of abstract method from ExtensionDriver class.""" port_id = result.get('id') policy_profile_attr = data.get(constants.N1KV_PROFILE) if not attributes.is_attr_set(policy_profile_attr): policy_profile_attr = ( cfg.CONF.ml2_cisco_n1kv.default_policy_profile) with context.session.begin(subtransactions=True): try: n1kv_db.get_policy_binding(port_id, context.session) except n1kv_exc.PortBindingNotFound: if not uuidutils.is_uuid_like(policy_profile_attr): policy_profile = n1kv_db.get_policy_profile_by_name( policy_profile_attr, context.session) if policy_profile: policy_profile_attr = policy_profile.id else: LOG.error( _LE("Policy Profile %(profile)s does " "not exist."), {"profile": policy_profile_attr}) raise ml2_exc.MechanismDriverError() elif not (n1kv_db.get_policy_profile_by_uuid( context.session, policy_profile_attr)): LOG.error( _LE("Policy Profile %(profile)s does not " "exist."), {"profile": policy_profile_attr}) raise ml2_exc.MechanismDriverError() n1kv_db.add_policy_binding(port_id, policy_profile_attr, context.session) result[constants.N1KV_PROFILE] = policy_profile_attr
def process_create_port(self, context, data, result): """Implementation of abstract method from ExtensionDriver class.""" port_id = result.get('id') policy_profile_attr = data.get(constants.N1KV_PROFILE) if not attributes.is_attr_set(policy_profile_attr): policy_profile_attr = (cfg.CONF.ml2_cisco_n1kv. default_policy_profile) with context.session.begin(subtransactions=True): try: n1kv_db.get_policy_binding(port_id, context.session) except n1kv_exc.PortBindingNotFound: if not uuidutils.is_uuid_like(policy_profile_attr): policy_profile = n1kv_db.get_policy_profile_by_name( policy_profile_attr, context.session) if policy_profile: policy_profile_attr = policy_profile.id else: LOG.error(_LE("Policy Profile %(profile)s does " "not exist."), {"profile": policy_profile_attr}) raise ml2_exc.MechanismDriverError() elif not (n1kv_db.get_policy_profile_by_uuid( context.session, policy_profile_attr)): LOG.error(_LE("Policy Profile %(profile)s does not " "exist."), {"profile": policy_profile_attr}) raise ml2_exc.MechanismDriverError() n1kv_db.add_policy_binding(port_id, policy_profile_attr, context.session) result[constants.N1KV_PROFILE] = policy_profile_attr
def _sync_create_ports(self, combined_res_info, vsm_ip): """ Sync ports by creating missing ones on VSM. :param combined_res_info: tuple containing VSM and neutron information :param vsm_ip: string representing the IP address of the VSM """ (vsm_vmn_dict, neutron_ports) = combined_res_info vsm_port_uuids = set() for (k, v) in vsm_vmn_dict.items(): port_dict = v['properties'] port_ids = set(port_dict['portId'].split(',')) vsm_port_uuids = vsm_port_uuids.union(port_ids) for port in neutron_ports: if port['id'] not in vsm_port_uuids: # create these ports on VSM network_uuid = port['network_id'] binding = n1kv_db.get_policy_binding(port['id']) policy_profile_id = binding.profile_id policy_profile = n1kv_db.get_policy_profile_by_uuid( db.get_session(), policy_profile_id) vmnetwork_name = "%s%s_%s" % (n1kv_const.VM_NETWORK_PREFIX, policy_profile_id, network_uuid) try: self.n1kvclient.create_n1kv_port(port, vmnetwork_name, policy_profile, vsm_ip=vsm_ip) except n1kv_exc.VSMError as e: LOG.warning( _LW('Sync Exception: Port creation on VSM ' 'failed: %s'), e.message)
def update_port_postcommit(self, context): """Send port parameters to the VSM.""" port = context.current old_port = context.original # Perform port update on VSM only if a router or DHCP port is bound. if (not old_port['binding:host_id'] and (port['device_owner'] in [ bc.constants.DEVICE_OWNER_ROUTER_INTF, bc.constants.DEVICE_OWNER_DHCP])): session = context._plugin_context.session binding = n1kv_db.get_policy_binding(port['id'], session) policy_profile = n1kv_db.get_policy_profile_by_uuid( session, binding.profile_id) if policy_profile is None: raise ml2_exc.MechanismDriverError() vmnetwork_name = "%s%s_%s" % (n1kv_const.VM_NETWORK_PREFIX, binding.profile_id, port['network_id']) try: # Today an update is just a create, so we call create port self.n1kvclient.create_n1kv_port(port, vmnetwork_name, policy_profile) except(n1kv_exc.VSMError, n1kv_exc.VSMConnectionFailed): raise ml2_exc.MechanismDriverError() LOG.info(_LI("Update port(postcommit) succeeded for port: " "%(id)s on network: %(network_id)s with policy " "profile ID: %(profile_id)s"), {"network_id": port['network_id'], "id": port['id'], "profile_id": policy_profile.id})
def _sync_create_ports(self, combined_res_info, vsm_ip): """ Sync ports by creating missing ones on VSM. :param combined_res_info: tuple containing VSM and neutron information :param vsm_ip: string representing the IP address of the VSM """ (vsm_vmn_dict, neutron_ports) = combined_res_info vsm_port_uuids = set() for (k, v) in vsm_vmn_dict.items(): port_dict = v['properties'] port_ids = set(port_dict['portId'].split(',')) vsm_port_uuids = vsm_port_uuids.union(port_ids) for port in neutron_ports: if port['id'] not in vsm_port_uuids: # create these ports on VSM network_uuid = port['network_id'] binding = n1kv_db.get_policy_binding(port['id']) policy_profile_id = binding.profile_id policy_profile = n1kv_db.get_policy_profile_by_uuid( db.get_session(), policy_profile_id) vmnetwork_name = "%s%s_%s" % (n1kv_const.VM_NETWORK_PREFIX, policy_profile_id, network_uuid) try: self.n1kvclient.create_n1kv_port(port, vmnetwork_name, policy_profile, vsm_ip=vsm_ip) except n1kv_exc.VSMError as e: LOG.warning(_LW('Sync Exception: Port creation on VSM ' 'failed: %s'), e.message)
def _sync_create_ports(self, combined_res_info, vsm_ip): """Sync ports by creating missing ones on VSM.""" (vsm_vmn_dict, neutron_ports) = combined_res_info vsm_port_uuids = set() for port_dict in vsm_vmn_dict.values(): port_props = port_dict['properties'] port_ids = set(port_props['portId'].split(',')) vsm_port_uuids = vsm_port_uuids.union(port_ids) missing_ports = [port for port in neutron_ports if port['id'] not in vsm_port_uuids] for port in missing_ports: # create these ports on VSM network_uuid = port['network_id'] binding = n1kv_db.get_policy_binding(port['id']) policy_profile_id = binding.profile_id policy_profile = n1kv_db.get_policy_profile_by_uuid( db.get_session(), policy_profile_id) if not policy_profile: LOG.error(_LE("Cannot sync port with id %(port_id)s " "because policy profile with id %(profile_id)s" "does not exist."), {"port_id": port['id'], "profile_name": policy_profile}) continue vmnetwork_name = "%s%s_%s" % (n1kv_const.VM_NETWORK_PREFIX, policy_profile_id, network_uuid) try: self.n1kvclient.create_n1kv_port(port, vmnetwork_name, policy_profile, vsm_ip=vsm_ip) except (n1kv_exc.VSMError, n1kv_exc.VSMConnectionFailed): LOG.warning(_LW('Sync Exception: Port create failed for ' '%s.') % port['id'])
def update_port_postcommit(self, context): """Send port parameters to the VSM.""" port = context.current old_port = context.original # Perform port update on VSM only if a router or DHCP port is bound. if (not old_port['binding:host_id'] and (port['device_owner'] in [n_const.DEVICE_OWNER_ROUTER_INTF, n_const.DEVICE_OWNER_DHCP])): session = context._plugin_context.session binding = n1kv_db.get_policy_binding(port['id'], session) policy_profile = n1kv_db.get_policy_profile_by_uuid( session, binding.profile_id) if policy_profile is None: raise ml2_exc.MechanismDriverError() vmnetwork_name = "%s%s_%s" % (n1kv_const.VM_NETWORK_PREFIX, binding.profile_id, port['network_id']) try: # Today an update is just a create, so we call create port self.n1kvclient.create_n1kv_port(port, vmnetwork_name, policy_profile) except(n1kv_exc.VSMError, n1kv_exc.VSMConnectionFailed): raise ml2_exc.MechanismDriverError() LOG.info(_LI("Update port(postcommit) succeeded for port: " "%(id)s on network: %(network_id)s with policy " "profile ID: %(profile_id)s"), {"network_id": port['network_id'], "id": port['id'], "profile_id": policy_profile.id})
def create_port_postcommit(self, context): """Send port parameters to the VSM.""" port = context.current session = context._plugin_context.session binding = n1kv_db.get_policy_binding(port['id'], session) policy_profile = n1kv_db.get_policy_profile_by_uuid(session, binding.profile_id) if policy_profile is None: raise ml2_exc.MechanismDriverError() vmnetwork_name = "%s%s_%s" % (n1kv_const.VM_NETWORK_PREFIX, binding.profile_id, port['network_id']) try: self.n1kvclient.create_n1kv_port(port, vmnetwork_name, policy_profile) except(n1kv_exc.VSMError, n1kv_exc.VSMConnectionFailed) as e: with excutils.save_and_reraise_exception(reraise=False): LOG.info(e.message) raise ml2_exc.MechanismDriverError() LOG.info(_LI("Create port(postcommit) succeeded for port: " "%(id)s on network: %(network_id)s with policy " "profile ID: %(profile_id)s"), {"network_id": port['network_id'], "id": port['id'], "profile_id": policy_profile.id})
def extend_port_dict(self, session, model, result): """Implementation of abstract method from ExtensionDriver class.""" port_id = result.get('id') with session.begin(subtransactions=True): try: res = n1kv_db.get_policy_binding(port_id, session) result[constants.N1KV_PROFILE] = res.profile_id except n1kv_exc.PortBindingNotFound: # Do nothing if the port binding is not found. pass
def test_add_and_get_profile_binding(self): with self.port() as port: TEST_PORT_ID = port['port']['id'] self.assertRaises(c_exc.PortBindingNotFound, n1kv_db.get_policy_binding, TEST_PORT_ID) p = _create_test_policy_profile_if_not_there(self.session) n1kv_db.add_policy_binding(TEST_PORT_ID, p.id) binding = n1kv_db.get_policy_binding(TEST_PORT_ID) self.assertIsNotNone(binding) self.assertEqual(TEST_PORT_ID, binding.port_id) self.assertEqual(p.id, binding.profile_id)
def test_add_and_get_multiple_profile_bindings(self): with self.subnet() as subnet: with self.port(subnet=subnet) as port1: with self.port(subnet=subnet) as port2: TEST_PORT_ID1 = port1['port']['id'] TEST_PORT_ID2 = port2['port']['id'] self.assertRaises(c_exc.PortBindingNotFound, n1kv_db.get_policy_binding, TEST_PORT_ID1) self.assertRaises(c_exc.PortBindingNotFound, n1kv_db.get_policy_binding, TEST_PORT_ID2) p = _create_test_policy_profile_if_not_there(self.session) n1kv_db.add_policy_binding(TEST_PORT_ID1, p.id) binding = n1kv_db.get_policy_binding(TEST_PORT_ID1) self.assertIsNotNone(binding) self.assertEqual(TEST_PORT_ID1, binding.port_id) self.assertEqual(p.id, binding.profile_id) n1kv_db.add_policy_binding(TEST_PORT_ID2, p.id) binding = n1kv_db.get_policy_binding(TEST_PORT_ID2) self.assertIsNotNone(binding) self.assertEqual(TEST_PORT_ID2, binding.port_id) self.assertEqual(p.id, binding.profile_id)
def test_create_port_with_default_n1kv_policy_profile_id( self, restrict_policy_profiles=False): """Test port create without passing policy profile id.""" ml2_config.cfg.CONF.set_override('restrict_policy_profiles', restrict_policy_profiles, 'ml2_cisco_n1kv') with self.port() as port: pp = n1kv_db.get_policy_profile_by_name( test_cisco_n1kv_mech.DEFAULT_PP) profile_binding = n1kv_db.get_policy_binding(port['port']['id']) self.assertEqual(profile_binding.profile_id, pp['id']) # assert that binding for default port-profile exists port_tenant = port['port']['tenant_id'] self.assert_profile_binding_exists( binding='policy_profile_bindings', tenant_id=port_tenant, profile_id=pp['id'])
def _sync_create_ports(self, combined_res_info, vsm_ip): """Sync ports by creating missing ones on VSM.""" (vsm_vmn_dict, neutron_ports) = combined_res_info vsm_port_uuids = set() for port_dict in vsm_vmn_dict.values(): port_props = port_dict['properties'] port_ids = set(port_props['portId'].split(',')) vsm_port_uuids = vsm_port_uuids.union(port_ids) missing_ports = [ port for port in neutron_ports if port['id'] not in vsm_port_uuids ] for port in missing_ports: # create these ports on VSM try: network_uuid = port['network_id'] binding = n1kv_db.get_policy_binding(port['id']) policy_profile_id = binding.profile_id policy_profile = n1kv_db.get_policy_profile_by_uuid( db.get_session(), policy_profile_id) if not policy_profile: LOG.error( _LE("Cannot sync port with id %(port_id)s " "because policy profile with id " "%(profile_id)s does not exist."), { "port_id": port['id'], "profile_name": policy_profile }) continue vmnetwork_name = "%s%s_%s" % (n1kv_const.VM_NETWORK_PREFIX, policy_profile_id, network_uuid) self.n1kvclient.create_n1kv_port(port, vmnetwork_name, policy_profile, vsm_ip=vsm_ip) except (n1kv_exc.VSMError, n1kv_exc.VSMConnectionFailed): LOG.warning( _LW('Sync Exception: Port create failed for ' '%s.') % port['id']) except n1kv_exc.PolicyProfileNotFound: LOG.warning( _LW('Sync Exception: Port create failed as Policy ' 'profile %s does not exist on all VSM'), policy_profile_id)
def test_create_port_with_default_n1kv_policy_profile_id( self, restrict_policy_profiles=False): """Test port create without passing policy profile id.""" ml2_config.cfg.CONF.set_override( 'restrict_policy_profiles', restrict_policy_profiles, 'ml2_cisco_n1kv') with self.port() as port: pp = n1kv_db.get_policy_profile_by_name( test_cisco_n1kv_mech.DEFAULT_PP) profile_binding = n1kv_db.get_policy_binding(port['port']['id']) self.assertEqual(profile_binding.profile_id, pp['id']) # assert that binding for default port-profile exists port_tenant = port['port']['tenant_id'] self.assert_profile_binding_exists( binding='policy_profile_bindings', tenant_id=port_tenant, profile_id=pp['id'] )
def create_port_postcommit(self, context): """Send port parameters to the VSM.""" port = context.current session = context._plugin_context.session binding = n1kv_db.get_policy_binding(port['id'], session) policy_profile = n1kv_db.get_policy_profile_by_uuid(session, binding.profile_id) if policy_profile is None: raise ml2_exc.MechanismDriverError() vmnetwork_name = "%s%s_%s" % (n1kv_const.VM_NETWORK_PREFIX, binding.profile_id, port['network_id']) try: self.n1kvclient.create_n1kv_port(port, vmnetwork_name, policy_profile) except(n1kv_exc.VSMError, n1kv_exc.VSMConnectionFailed): raise ml2_exc.MechanismDriverError() LOG.info(_LI("Create port(postcommit) succeeded for port: " "%(id)s on network: %(network_id)s with policy " "profile ID: %(profile_id)s"), {"network_id": port['network_id'], "id": port['id'], "profile_id": policy_profile.id})
def test_create_port_with_default_n1kv_policy_profile_id(self): """Test port create without passing policy profile id.""" with self.port() as port: pp = n1kv_db.get_policy_profile_by_name('default-pp') profile_binding = n1kv_db.get_policy_binding(port['port']['id']) self.assertEqual(profile_binding.profile_id, pp['id'])
def test_create_port_with_default_n1kv_policy_profile_id(self): """Test port create without passing policy profile id.""" with self.port() as port: pp = n1kv_db.get_policy_profile_by_name(DEFAULT_PP) profile_binding = n1kv_db.get_policy_binding(port['port']['id']) self.assertEqual(profile_binding.profile_id, pp['id'])