diff --git a/neutron/api/v2/attributes.py b/neutron/api/v2/attributes.py index d94a0a9723b..c85f4afca90 100644 --- a/neutron/api/v2/attributes.py +++ b/neutron/api/v2/attributes.py @@ -703,8 +703,6 @@ RESOURCE_ATTRIBUTE_MAP = { 'is_visible': True}, 'status': {'allow_post': False, 'allow_put': False, 'is_visible': True}, - 'mtu': {'allow_post': False, 'allow_put': False, - 'is_visible': True}, 'tenant_id': {'allow_post': True, 'allow_put': False, 'validate': {'type:string': TENANT_ID_MAX_LEN}, 'required_by_policy': True, diff --git a/neutron/db/netmtu_db.py b/neutron/db/netmtu_db.py new file mode 100644 index 00000000000..7d6acf79f69 --- /dev/null +++ b/neutron/db/netmtu_db.py @@ -0,0 +1,29 @@ +# Copyright (c) 2015 OpenStack Foundation. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from neutron.api.v2 import attributes +from neutron.db import db_base_plugin_v2 +from neutron.extensions import netmtu + + +class Netmtu_db_mixin(object): + """Mixin class to add network MTU methods to db_base_plugin_v2.""" + + def _extend_network_dict_mtu(self, network_res, network_db): + network_res[netmtu.MTU] = network_db.mtu + return network_res + + db_base_plugin_v2.NeutronDbPluginV2.register_dict_extend_funcs( + attributes.NETWORKS, ['_extend_network_dict_mtu']) diff --git a/neutron/extensions/netmtu.py b/neutron/extensions/netmtu.py new file mode 100644 index 00000000000..55245fdf9a6 --- /dev/null +++ b/neutron/extensions/netmtu.py @@ -0,0 +1,54 @@ +# Copyright 2015 Openstack Foundation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from neutron.api import extensions + + +MTU = 'mtu' +EXTENDED_ATTRIBUTES_2_0 = { + 'networks': { + MTU: {'allow_post': False, 'allow_put': False, + 'is_visible': True}, + }, +} + + +class Netmtu(extensions.ExtensionDescriptor): + """Extension class supporting network MTU.""" + + @classmethod + def get_name(cls): + return "Network MTU" + + @classmethod + def get_alias(cls): + return "net-mtu" + + @classmethod + def get_description(cls): + return "Provides MTU attribute for a network resource." + + @classmethod + def get_namespace(cls): + return "http://docs.openstack.org/ext/net_mtu/api/v1.0" + + @classmethod + def get_updated(cls): + return "2015-03-25T10:00:00-00:00" + + def get_extended_resources(self, version): + if version == "2.0": + return EXTENDED_ATTRIBUTES_2_0 + else: + return {} diff --git a/neutron/plugins/ml2/plugin.py b/neutron/plugins/ml2/plugin.py index 01089ca8f9a..7866103ef58 100644 --- a/neutron/plugins/ml2/plugin.py +++ b/neutron/plugins/ml2/plugin.py @@ -53,6 +53,7 @@ from neutron.db import dvr_mac_db from neutron.db import external_net_db from neutron.db import extradhcpopt_db from neutron.db import models_v2 +from neutron.db import netmtu_db from neutron.db import quota_db # noqa from neutron.db import securitygroups_rpc_base as sg_db_rpc from neutron.extensions import allowedaddresspairs as addr_pair @@ -89,7 +90,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2, sg_db_rpc.SecurityGroupServerRpcMixin, agentschedulers_db.DhcpAgentSchedulerDbMixin, addr_pair_db.AllowedAddressPairsMixin, - extradhcpopt_db.ExtraDhcpOptMixin): + extradhcpopt_db.ExtraDhcpOptMixin, + netmtu_db.Netmtu_db_mixin): """Implement the Neutron L2 abstractions using modules. @@ -112,7 +114,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2, "quotas", "security-group", "agent", "dhcp_agent_scheduler", "multi-provider", "allowed-address-pairs", - "extra_dhcp_opt", "subnet_allocation"] + "extra_dhcp_opt", "subnet_allocation", + "net-mtu"] @property def supported_extension_aliases(self): diff --git a/neutron/tests/unit/test_db_plugin.py b/neutron/tests/unit/test_db_plugin.py index 4b87ef4527e..87619a50afb 100644 --- a/neutron/tests/unit/test_db_plugin.py +++ b/neutron/tests/unit/test_db_plugin.py @@ -2223,12 +2223,6 @@ class TestNetworksV2(NeutronDbPluginV2TestCase): self.assertEqual(ctx_manager.exception.code, webob.exc.HTTPForbidden.code) - def test_create_network_default_mtu(self): - name = 'net1' - with self.network(name=name) as net: - self.assertEqual(net['network']['mtu'], - constants.DEFAULT_NETWORK_MTU) - def test_create_network_vlan_transparent(self): name = "vlan_transparent" cfg.CONF.set_override('vlan_transparent', True) diff --git a/neutron/tests/unit/test_extension_netmtu.py b/neutron/tests/unit/test_extension_netmtu.py new file mode 100644 index 00000000000..ca57d7a052f --- /dev/null +++ b/neutron/tests/unit/test_extension_netmtu.py @@ -0,0 +1,74 @@ +# Copyright 2015 Openstack Foundation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from neutron.common import constants +from neutron.db import db_base_plugin_v2 +from neutron.db import netmtu_db +from neutron.extensions import netmtu +from neutron.tests.unit import test_db_plugin + + +class NetmtuExtensionManager(object): + + def get_resources(self): + return [] + + def get_actions(self): + return [] + + def get_request_extensions(self): + return [] + + def get_extended_resources(self, version): + return netmtu.get_extended_resources(version) + + +class NetmtuExtensionTestPlugin(db_base_plugin_v2.NeutronDbPluginV2, + netmtu_db.Netmtu_db_mixin): + """Test plugin to mixin the network MTU extensions. + """ + + supported_extension_aliases = ["net-mtu"] + + +class NetmtuExtensionTestCase(test_db_plugin.TestNetworksV2): + """Test API extension net-mtu attributes. + """ + + def setUp(self): + plugin = ('neutron.tests.unit.test_extension_netmtu.' + + 'NetmtuExtensionTestPlugin') + ext_mgr = NetmtuExtensionManager() + super(NetmtuExtensionTestCase, self).setUp(plugin=plugin, + ext_mgr=ext_mgr) + + def test_list_networks_with_fields_mtu(self): + with self.network(name='net1') as net1: + req = self.new_list_request('networks', + params='fields=name&fields=mtu') + res = self.deserialize(self.fmt, req.get_response(self.api)) + self.assertEqual(1, len(res['networks'])) + self.assertEqual(res['networks'][0]['name'], + net1['network']['name']) + self.assertEqual(res['networks'][0].get('mtu'), + constants.DEFAULT_NETWORK_MTU) + + def test_show_network_mtu(self): + with self.network(name='net1') as net: + req = self.new_show_request('networks', net['network']['id']) + res = self.deserialize(self.fmt, req.get_response(self.api)) + self.assertEqual(res['network']['name'], + net['network']['name']) + self.assertEqual(res['network']['mtu'], + constants.DEFAULT_NETWORK_MTU)