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 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 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 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 _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 _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 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 test_get_policy_profile_by_uuid(self): ml2_n1kv_config.cfg.CONF.set_override('n1kv_vsm_ips', TEST_VSM_HOSTS, 'ml2_cisco_n1kv') profile = _create_test_policy_profile_if_not_there(self.session) got_profile = n1kv_db.get_policy_profile_by_uuid(self.session, TEST_POLICY_PROFILE['id']) self.assertEqual(profile.id, got_profile.id) self.assertEqual(profile.name, got_profile.name) ml2_n1kv_config.cfg.CONF.set_override('n1kv_vsm_ips', TEST_MULTI_VSM_HOSTS, 'ml2_cisco_n1kv') self.assertRaises(c_exc.PolicyProfileNotFound, n1kv_db.get_policy_profile_by_uuid, self.session, TEST_POLICY_PROFILE['id'])
def test_get_policy_profile_by_uuid(self): ml2_n1kv_config.cfg.CONF.set_override('n1kv_vsm_ips', TEST_VSM_HOSTS, 'ml2_cisco_n1kv') profile = _create_test_policy_profile_if_not_there(self.session) got_profile = n1kv_db.get_policy_profile_by_uuid( self.session, TEST_POLICY_PROFILE['id']) self.assertEqual(profile.id, got_profile.id) self.assertEqual(profile.name, got_profile.name) ml2_n1kv_config.cfg.CONF.set_override('n1kv_vsm_ips', TEST_MULTI_VSM_HOSTS, 'ml2_cisco_n1kv') self.assertRaises(c_exc.PolicyProfileNotFound, n1kv_db.get_policy_profile_by_uuid, self.session, TEST_POLICY_PROFILE['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 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) tenant_id = context.tenant_id or data.get('tenant_id') default_policy_profile_name = (cfg.CONF.ml2_cisco_n1kv. default_policy_profile) if not bc_attr.is_attr_set(policy_profile_attr): policy_profile_attr = default_policy_profile_name with context.session.begin(subtransactions=True): try: if not uuidutils.is_uuid_like(policy_profile_attr): policy_profile = n1kv_db.get_policy_profile_by_name( policy_profile_attr, context.session) else: policy_profile = (n1kv_db.get_policy_profile_by_uuid( context.session, policy_profile_attr)) n1kv_db.get_profile_binding(db_session=context.session, tenant_id=tenant_id, profile_id=policy_profile.id) except n1kv_exc.PolicyProfileNotFound: LOG.error(_LE("Policy Profile %(profile)s does " "not exist."), {"profile": policy_profile_attr}) raise ml2_exc.ExtensionDriverError(driver='N1Kv ML2') except n1kv_exc.ProfileTenantBindingNotFound: if context.is_admin: session = context.session n1kv_db.update_policy_profile_binding_with_tenant_id( policy_profile.id, tenant_id, session) elif (cfg.CONF.ml2_cisco_n1kv.restrict_policy_profiles and policy_profile.name != default_policy_profile_name): LOG.error(_LE("Policy Profile %s is " "not owned by this tenant.") % policy_profile_attr) raise ml2_exc.ExtensionDriverError(driver='N1Kv ML2') n1kv_db.add_policy_binding(port_id, policy_profile.id, context.session) result[constants.N1KV_PROFILE] = policy_profile.id
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) tenant_id = context.tenant_id or data.get('tenant_id') default_policy_profile_name = (cfg.CONF.ml2_cisco_n1kv. default_policy_profile) if not bc.is_attr_set(policy_profile_attr): policy_profile_attr = default_policy_profile_name with context.session.begin(subtransactions=True): try: if not uuidutils.is_uuid_like(policy_profile_attr): policy_profile = n1kv_db.get_policy_profile_by_name( policy_profile_attr, context.session) else: policy_profile = (n1kv_db.get_policy_profile_by_uuid( context.session, policy_profile_attr)) n1kv_db.get_profile_binding(db_session=context.session, tenant_id=tenant_id, profile_id=policy_profile.id) except n1kv_exc.PolicyProfileNotFound: LOG.error("Policy Profile %(profile)s does " "not exist.", {"profile": policy_profile_attr}) raise ml2_exc.ExtensionDriverError(driver='N1Kv ML2') except n1kv_exc.ProfileTenantBindingNotFound: if context.is_admin: session = context.session n1kv_db.update_policy_profile_binding_with_tenant_id( policy_profile.id, tenant_id, session) elif (cfg.CONF.ml2_cisco_n1kv.restrict_policy_profiles and policy_profile.name != default_policy_profile_name): LOG.error("Policy Profile %s is " "not owned by this tenant." % policy_profile_attr) raise ml2_exc.ExtensionDriverError(driver='N1Kv ML2') n1kv_db.add_policy_binding(port_id, policy_profile.id, context.session) result[constants.N1KV_PROFILE] = 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): 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 _get_policy_profile(self, session, pprofile_id): profile = n1kv_db.get_policy_profile_by_uuid(session, pprofile_id) if profile is None: raise n1kv_exc.PolicyProfileNotFound(profile=pprofile_id) else: return profile
def _get_policy_profile(self, session, pprofile_id): return n1kv_db.get_policy_profile_by_uuid(session, pprofile_id)