def test_get_name_and_uuid(self): uuid = 'afc40f8a-4967-477e-a17a-9d560d1786c7' suffix = '_afc40...786c7' expected = 'maldini%s' % suffix short_name = utils.get_name_and_uuid('maldini', uuid) self.assertEqual(expected, short_name) name = 'X' * 255 expected = '%s%s' % ('X' * (80 - len(suffix)), suffix) short_name = utils.get_name_and_uuid(name, uuid) self.assertEqual(expected, short_name)
def create_or_update_policy(self, context, policy): policy_id = policy.id tags = self._get_tags(context, policy) pol_name = utils.get_name_and_uuid(policy.name or 'policy', policy.id) shapers = [] dscp = None if (hasattr(policy, "rules")): for rule in policy["rules"]: if rule.rule_type == qos_consts.RULE_TYPE_BANDWIDTH_LIMIT: # the NSX direction is opposite to the neutron one is_ingress = rule.direction == n_consts.EGRESS_DIRECTION shapers.append(self._get_shaper_from_rule( rule, is_ingress=is_ingress)) elif rule.rule_type == qos_consts.RULE_TYPE_DSCP_MARKING: dscp = self._get_dscp_from_rule(rule) else: LOG.warning("The NSX-Policy plugin does not support QoS " "rule of type %s", rule.rule_type) self._nsxpolicy.qos_profile.create_or_overwrite( pol_name, profile_id=policy_id, description=policy.get('description'), dscp=dscp, shaper_configurations=shapers, tags=tags)
def test_dscp_rule_create_profile(self, *mocks): # test the profile update when a QoS DSCP rule is created _policy = QoSPolicy(self.ctxt, **self.policy_data['policy']) # add a rule to the policy setattr(_policy, "rules", [self.dscp_rule]) with mock.patch.object(QoSPolicy, 'get_object', return_value=_policy),\ mock.patch('vmware_nsxlib.v3.policy.core_resources.' 'NsxQosProfileApi.' 'create_or_overwrite') as create_profile,\ mock.patch('neutron.objects.db.api.update_object', return_value=self.dscp_rule_data): self.qos_plugin.update_policy_dscp_marking_rule( self.ctxt, self.dscp_rule.id, _policy.id, self.dscp_rule_data) # validate the data on the profile rule_dict = self.dscp_rule_data['dscp_marking_rule'] dscp_mark = rule_dict['dscp_mark'] exp_name = utils.get_name_and_uuid(self.policy.name, self.policy.id) expected_dscp = policy_defs.QoSDscp( mode=policy_defs.QoSDscp.QOS_DSCP_UNTRUSTED, priority=dscp_mark) create_profile.assert_called_once_with( exp_name, profile_id=self.policy.id, description=self.policy_data["policy"]["description"], dscp=mock.ANY, shaper_configurations=[], tags=self._get_expected_tags()) # Compare the dscp obj actual_dscp = create_profile.call_args[1]['dscp'] self.assertEqual(expected_dscp.get_obj_dict(), actual_dscp.get_obj_dict())
def create_or_update_policy(self, context, policy): policy_id = policy.id tags = self._get_tags(context, policy) pol_name = utils.get_name_and_uuid(policy.name or 'policy', policy.id) shapers = [] dscp = None if (hasattr(policy, "rules")): for rule in policy["rules"]: if rule.rule_type == qos_consts.RULE_TYPE_BANDWIDTH_LIMIT: # the NSX direction is opposite to the neutron one is_ingress = rule.direction == n_consts.EGRESS_DIRECTION shapers.append( self._get_shaper_from_rule(rule, is_ingress=is_ingress)) elif rule.rule_type == qos_consts.RULE_TYPE_DSCP_MARKING: dscp = self._get_dscp_from_rule(rule) else: LOG.warning( "The NSX-Policy plugin does not support QoS " "rule of type %s", rule.rule_type) self._nsxpolicy.qos_profile.create_or_overwrite( pol_name, profile_id=policy_id, description=policy.get('description'), dscp=dscp, shaper_configurations=shapers, tags=tags)
def nsx_update_metadata_proxy(resource, event, trigger, **kwargs): """Update Metadata proxy for NSXv3 CrossHairs.""" nsx_version = utils.get_connected_nsxlib().get_version() if not nsx_utils.is_nsx_version_1_1_0(nsx_version): LOG.info(_LI("This utility is not available for NSX version %s"), nsx_version) return metadata_proxy_uuid = None if kwargs.get('property'): properties = admin_utils.parse_multi_keyval_opt(kwargs['property']) metadata_proxy_uuid = properties.get('metadata_proxy_uuid') if not metadata_proxy_uuid: LOG.error(_LE("metadata_proxy_uuid is not defined")) return cfg.CONF.set_override('dhcp_agent_notification', False) cfg.CONF.set_override('native_dhcp_metadata', True, 'nsx_v3') cfg.CONF.set_override('metadata_proxy_uuid', metadata_proxy_uuid, 'nsx_v3') plugin = utils.NsxV3PluginWrapper() nsx_client = utils.get_nsxv3_client() port_resource = resources.LogicalPort(nsx_client) # For each Neutron network, check if it is an internal metadata network. # If yes, delete the network and associated router interface. # Otherwise, create a logical switch port with MD-Proxy attachment. for network in neutron_client.get_networks(): if _is_metadata_network(network): # It is a metadata network, find the attached router, # remove the router interface and the network. filters = {'device_owner': const.ROUTER_INTERFACE_OWNERS, 'fixed_ips': { 'subnet_id': [network['subnets'][0]], 'ip_address': [nsx_rpc.METADATA_GATEWAY_IP]}} ports = neutron_client.get_ports(filters=filters) if not ports: continue router_id = ports[0]['device_id'] interface = {'subnet_id': network['subnets'][0]} plugin.remove_router_interface(router_id, interface) LOG.info(_LI("Removed metadata interface on router %s"), router_id) plugin.delete_network(network['id']) LOG.info(_LI("Removed metadata network %s"), network['id']) else: lswitch_id = neutron_client.net_id_to_lswitch_id(network['id']) if not lswitch_id: continue tags = nsx_utils.build_v3_tags_payload( network, resource_type='os-neutron-net-id', project_name='admin') name = nsx_utils.get_name_and_uuid('%s-%s' % ( 'mdproxy', network['name'] or 'network'), network['id']) port_resource.create( lswitch_id, metadata_proxy_uuid, tags=tags, name=name, attachment_type=nsx_constants.ATTACHMENT_MDPROXY) LOG.info(_LI("Enabled native metadata proxy for network %s"), network['id'])
def build_dhcp_server_config(network, subnet, port, project_name): # Prepare the configutation for a new logical DHCP server. server_ip = "%s/%u" % (port['fixed_ips'][0]['ip_address'], netaddr.IPNetwork(subnet['cidr']).prefixlen) dns_servers = subnet['dns_nameservers'] if not dns_servers or not validators.is_attr_set(dns_servers): dns_servers = cfg.CONF.nsx_v3.nameservers gateway_ip = subnet['gateway_ip'] if not validators.is_attr_set(gateway_ip): gateway_ip = None # The following code is based on _generate_opts_per_subnet() in # neutron/agent/linux/dhcp.py. It prepares DHCP options for a subnet. # Add route for directly connected network. host_routes = [{'network': subnet['cidr'], 'next_hop': '0.0.0.0'}] # Copy routes from subnet host_routes attribute. for hr in subnet['host_routes']: if hr['destination'] == constants.IPv4_ANY: if not gateway_ip: gateway_ip = hr['nexthop'] else: host_routes.append({ 'network': hr['destination'], 'next_hop': hr['nexthop'] }) # If gateway_ip is defined, add default route via this gateway. if gateway_ip: host_routes.append({ 'network': constants.IPv4_ANY, 'next_hop': gateway_ip }) options = {'option121': {'static_routes': host_routes}} name = utils.get_name_and_uuid(network['name'] or 'dhcpserver', network['id']) tags = utils.build_v3_tags_payload(network, resource_type='os-neutron-net-id', project_name=project_name) return { 'name': name, 'dhcp_profile_id': cfg.CONF.nsx_v3.dhcp_profile_uuid, 'server_ip': server_ip, 'dns_servers': dns_servers, 'domain_name': cfg.CONF.nsx_v3.dns_domain, 'gateway_ip': gateway_ip, 'options': options, 'tags': tags }
def test_policy_create_profile(self, *mocks): # test the profile creation when a QoS policy is created with mock.patch('vmware_nsxlib.v3.policy.core_resources.' 'NsxQosProfileApi.create_or_overwrite', return_value=self.fake_profile) as create_profile,\ mock.patch.object(QoSPolicy, 'get_object', return_value=self.policy),\ mock.patch.object(QoSPolicy, 'create'): self.qos_plugin.create_policy(self.ctxt, self.policy_data) exp_name = utils.get_name_and_uuid(self.policy.name, self.policy.id) create_profile.assert_called_once_with( exp_name, profile_id=self.policy.id, description=self.policy_data["policy"]["description"], dscp=None, shaper_configurations=[], tags=self._get_expected_tags())
def test_ingress_bw_rule_create_profile(self, *mocks): # test the profile update when a ingress QoS BW rule is created _policy = QoSPolicy(self.ctxt, **self.policy_data['policy']) # add a rule to the policy setattr(_policy, "rules", [self.ingress_rule]) with mock.patch.object(QoSPolicy, 'get_object', return_value=_policy),\ mock.patch('vmware_nsxlib.v3.policy.core_resources.' 'NsxQosProfileApi.' 'create_or_overwrite') as create_profile,\ mock.patch('neutron.objects.db.api.update_object', return_value=self.ingress_rule_data): self.qos_plugin.update_policy_bandwidth_limit_rule( self.ctxt, self.ingress_rule.id, _policy.id, self.ingress_rule_data) # validate the data on the profile rule_dict = self.ingress_rule_data['bandwidth_limit_rule'] expected_bw = int(round(float(rule_dict['max_kbps']) / 1024)) expected_burst = rule_dict['max_burst_kbps'] * 128 expected_peak = int(expected_bw * self.peak_bw_multiplier) exp_name = utils.get_name_and_uuid(self.policy.name, self.policy.id) # ingress neutron rule -> egress nsx args shaper_type = policy_defs.QoSRateLimiter.EGRESS_RATE_LIMITER_TYPE expected_shaper = policy_defs.QoSRateLimiter( resource_type=shaper_type, enabled=True, burst_size=expected_burst, peak_bandwidth=expected_peak, average_bandwidth=expected_bw) create_profile.assert_called_once_with( exp_name, profile_id=self.policy.id, description=self.policy_data["policy"]["description"], dscp=None, shaper_configurations=[mock.ANY], tags=self._get_expected_tags()) # Compare the shaper actual_shaper = create_profile.call_args[1][ 'shaper_configurations'][0] self.assertEqual(expected_shaper.get_obj_dict(), actual_shaper.get_obj_dict())
def test_metadata_proxy_with_create_network(self): # Test if native metadata proxy is enabled on a network when it is # created. with mock.patch.object(nsx_resources.LogicalPort, 'create') as create_logical_port: with self.network() as network: nsx_net_id = self.plugin._get_network_nsx_id( context.get_admin_context(), network['network']['id']) tags = utils.build_v3_tags_payload( network['network'], resource_type='os-neutron-net-id', project_name=None) name = utils.get_name_and_uuid( '%s-%s' % ('mdproxy', network['network']['name'] or 'network'), network['network']['id']) create_logical_port.assert_called_once_with( nsx_net_id, cfg.CONF.nsx_v3.metadata_proxy_uuid, tags=tags, name=name, attachment_type=nsx_constants.ATTACHMENT_MDPROXY)
def test_rule_delete_profile(self): # test the profile update when a QoS rule is deleted _policy = QoSPolicy(self.ctxt, **self.policy_data['policy']) # The mock will return the policy without the rule, # as if it was deleted with mock.patch.object(QoSPolicy, 'get_object', return_value=_policy),\ mock.patch('vmware_nsxlib.v3.policy.core_resources.' 'NsxQosProfileApi.' 'create_or_overwrite') as set_profile: setattr(_policy, "rules", [self.rule]) self.qos_plugin.delete_policy_bandwidth_limit_rule( self.ctxt, self.rule.id, self.policy.id) # validate the data on the profile exp_name = utils.get_name_and_uuid(self.policy.name, self.policy.id) set_profile.assert_called_once_with( exp_name, profile_id=self.policy.id, description=self.policy_data["policy"]["description"], dscp=None, shaper_configurations=[], tags=self._get_expected_tags())