From 8b978d02f70ba36871900b62ca160e2ad6dc08ef Mon Sep 17 00:00:00 2001 From: Akihiro MOTOKI Date: Wed, 16 Jan 2013 11:59:02 +0900 Subject: [PATCH] Support Port Binding Extension in NEC plugin Fixes bug 1099894 nova/network/quantumv2/api fill the bridge name according to binding:vif_type attribute passed from Quantum. Otherwise the bridge is set to None and launching an instance will fail. blueprint vif-plugging-improvements Note that binding:capabilities is under discussion and it may be changed in the future. This commit just adds NEC plugin support same as OVS and Linux bridge plugin. Change-Id: Ifd43c3c53615246187621613f72490b62a09b2d6 --- quantum/plugins/nec/nec_plugin.py | 45 +++++++++++++++++++++-- quantum/tests/unit/nec/test_nec_plugin.py | 9 ++++- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/quantum/plugins/nec/nec_plugin.py b/quantum/plugins/nec/nec_plugin.py index a4f2f4058..b4f94443c 100644 --- a/quantum/plugins/nec/nec_plugin.py +++ b/quantum/plugins/nec/nec_plugin.py @@ -21,6 +21,7 @@ from quantum import context from quantum.db import dhcp_rpc_base from quantum.db import l3_db from quantum.db import l3_rpc_base +from quantum.extensions import portbindings #NOTE(amotoki): quota_db cannot be removed, it is for db model from quantum.db import quota_db from quantum.openstack.common import log as logging @@ -30,6 +31,7 @@ from quantum.plugins.nec.common import exceptions as nexc from quantum.plugins.nec.db import api as ndb from quantum.plugins.nec.db import nec_plugin_base from quantum.plugins.nec import ofc_manager +from quantum import policy LOG = logging.getLogger(__name__) @@ -58,9 +60,15 @@ class NECPluginV2(nec_plugin_base.NECPluginV2Base, l3_db.L3_NAT_db_mixin): NOTE: This is for Quantum API V2. Codes for V1.0 and V1.1 are available at https://github.com/nec-openstack/quantum-openflow-plugin . + + The port binding extension enables an external application relay + information to and from the plugin. """ - supported_extension_aliases = ["router", "quotas"] + supported_extension_aliases = ["router", "quotas", "binding"] + + binding_view = "extension:port_binding:view" + binding_set = "extension:port_binding:set" def __init__(self): ndb.initialize() @@ -73,6 +81,12 @@ class NECPluginV2(nec_plugin_base.NECPluginV2Base, l3_db.L3_NAT_db_mixin): self.setup_rpc() + def _check_view_auth(self, context, resource, action): + return policy.check(context, action, resource) + + def _enforce_set_auth(self, context, resource, action): + policy.enforce(context, action, resource) + def setup_rpc(self): self.topic = topics.PLUGIN self.conn = rpc.create_connection(new=True) @@ -312,6 +326,14 @@ class NECPluginV2(nec_plugin_base.NECPluginV2Base, l3_db.L3_NAT_db_mixin): nets = self._filter_nets_l3(context, nets, filters) return [self._fields(net, fields) for net in nets] + def _extend_port_dict_binding(self, context, port): + if self._check_view_auth(context, port, self.binding_view): + port[portbindings.VIF_TYPE] = portbindings.VIF_TYPE_OVS + port[portbindings.CAPABILITIES] = { + portbindings.CAP_PORT_FILTER: + 'security-group' in self.supported_extension_aliases} + return port + def create_port(self, context, port): """Create a new port entry on DB, then try to activate it.""" LOG.debug(_("NECPluginV2.create_port() called, port=%s ."), port) @@ -320,8 +342,7 @@ class NECPluginV2(nec_plugin_base.NECPluginV2Base, l3_db.L3_NAT_db_mixin): OperationalStatus.BUILD) self.activate_port_if_ready(context, new_port) - - return new_port + return self._extend_port_dict_binding(context, new_port) def update_port(self, context, id, port): """Update port, and handle packetfilters associated with the port. @@ -342,7 +363,7 @@ class NECPluginV2(nec_plugin_base.NECPluginV2Base, l3_db.L3_NAT_db_mixin): else: self.deactivate_port(context, old_port) - return new_port + return self._extend_port_dict_binding(context, new_port) def delete_port(self, context, id, l3_port_check=True): """Delete port and packet_filters associated with the port.""" @@ -366,6 +387,22 @@ class NECPluginV2(nec_plugin_base.NECPluginV2Base, l3_db.L3_NAT_db_mixin): self.disassociate_floatingips(context, id) super(NECPluginV2, self).delete_port(context, id) + def get_port(self, context, id, fields=None): + session = context.session + with session.begin(subtransactions=True): + port = super(NECPluginV2, self).get_port(context, id, fields) + self._extend_port_dict_binding(context, port) + return self._fields(port, fields) + + def get_ports(self, context, filters=None, fields=None): + session = context.session + with session.begin(subtransactions=True): + ports = super(NECPluginV2, self).get_ports(context, filters, + fields) + for port in ports: + self._extend_port_dict_binding(context, port) + return [self._fields(port, fields) for port in ports] + # For PacketFilter Extension def _activate_packet_filter_if_ready(self, context, packet_filter, diff --git a/quantum/tests/unit/nec/test_nec_plugin.py b/quantum/tests/unit/nec/test_nec_plugin.py index 1d1aebdf5..54412ac59 100644 --- a/quantum/tests/unit/nec/test_nec_plugin.py +++ b/quantum/tests/unit/nec/test_nec_plugin.py @@ -13,7 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +from quantum.extensions import portbindings from quantum.tests.unit import test_db_plugin as test_plugin +from quantum.tests.unit import _test_extension_portbindings as test_bindings class NecPluginV2TestCase(test_plugin.QuantumDbPluginV2TestCase): @@ -33,8 +35,11 @@ class TestNecV2HTTPResponse(test_plugin.TestV2HTTPResponse, pass -class TestNecPortsV2(test_plugin.TestPortsV2, NecPluginV2TestCase): - pass +class TestNecPortsV2(test_plugin.TestPortsV2, NecPluginV2TestCase, + test_bindings.PortBindingsTestCase): + + VIF_TYPE = portbindings.VIF_TYPE_OVS + HAS_SECURITY_GROUP = False class TestNecNetworksV2(test_plugin.TestNetworksV2, NecPluginV2TestCase): -- 2.45.2