| 1 |
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
from neutron_lib.plugins import directory |
| 17 |
from oslo_utils import excutils |
| 18 |
|
| 19 |
from neutron.db import servicetype_db as st_db |
| 20 |
from neutron.services import provider_configuration as pconf |
| 21 |
from neutron.services import service_base |
| 22 |
|
| 23 |
from networking_l2gw.db.l2gateway import l2gateway_db |
| 24 |
from networking_l2gw.services.l2gateway.common import config |
| 25 |
from networking_l2gw.services.l2gateway.common import constants |
| 26 |
from networking_l2gw.services.l2gateway import exceptions as exc |
| 27 |
|
| 28 |
from neutron_lib.db import api as db_api |
| 29 |
from neutron_lib import exceptions as n_exc |
| 30 |
from oslo_log import log as logging |
| 31 |
|
| 32 |
LOG = logging.getLogger(__name__) |
| 33 |
|
| 34 |
|
| 35 |
class L2GatewayPlugin(l2gateway_db.L2GatewayMixin): |
| 36 |
|
| 37 |
"""Implementation of the Neutron l2 gateway Service Plugin. |
| 38 |
|
| 39 |
This class manages the workflow of L2 gateway request/response. |
| 40 |
""" |
| 41 |
|
| 42 |
supported_extension_aliases = ["l2-gateway", |
| 43 |
"l2-gateway-connection"] |
| 44 |
|
| 45 |
def __init__(self): |
| 46 |
"""Do the initialization for the l2 gateway service plugin here.""" |
| 47 |
config.register_l2gw_opts_helper() |
| 48 |
self.service_type_manager = st_db.ServiceTypeManager.get_instance() |
| 49 |
self.service_type_manager.add_provider_configuration( |
| 50 |
constants.L2GW, pconf.ProviderConfiguration('networking_l2gw')) |
| 51 |
self._load_drivers() |
| 52 |
LOG.info("L2Gateway Service Plugin using Service Driver: %s", |
| 53 |
self.default_provider) |
| 54 |
self.driver = self.drivers[self.default_provider] |
| 55 |
if len(self.drivers) > 1: |
| 56 |
LOG.warning("Multiple drivers configured for L2Gateway, " |
| 57 |
"although running multiple drivers in parallel" |
| 58 |
" is not yet supported") |
| 59 |
super().__init__() |
| 60 |
l2gateway_db.subscribe() |
| 61 |
|
| 62 |
def _load_drivers(self): |
| 63 |
"""Loads plugin-drivers specified in configuration.""" |
| 64 |
self.drivers, self.default_provider = service_base.load_drivers( |
| 65 |
'L2GW', self) |
| 66 |
|
| 67 |
def _get_driver_for_provider(self, provider): |
| 68 |
if provider in self.drivers: |
| 69 |
return self.drivers[provider] |
| 70 |
# raise if not associated (should never be reached) |
| 71 |
raise n_exc.Invalid("Error retrieving driver for provider %s" % |
| 72 |
provider) |
| 73 |
|
| 74 |
@property |
| 75 |
def _core_plugin(self): |
| 76 |
return directory.get_plugin() |
| 77 |
|
| 78 |
def get_plugin_type(self): |
| 79 |
"""Get type of the plugin.""" |
| 80 |
return constants.L2GW |
| 81 |
|
| 82 |
def get_plugin_description(self): |
| 83 |
"""Get description of the plugin.""" |
| 84 |
return constants.L2_GATEWAY_SERVICE_PLUGIN |
| 85 |
|
| 86 |
def add_port_mac(self, context, port_dict): |
| 87 |
"""Process a created Neutron port.""" |
| 88 |
self.driver.add_port_mac(context, port_dict) |
| 89 |
|
| 90 |
def delete_port_mac(self, context, port): |
| 91 |
"""Process a deleted Neutron port.""" |
| 92 |
self.driver.delete_port_mac(context, port) |
| 93 |
|
| 94 |
def create_l2_gateway(self, context, l2_gateway): |
| 95 |
"""Create the L2Gateway.""" |
| 96 |
self.validate_l2_gateway_for_create(context, l2_gateway) |
| 97 |
self.driver.create_l2_gateway(context, l2_gateway) |
| 98 |
with db_api.CONTEXT_WRITER.using(context): |
| 99 |
l2_gateway_instance = super().create_l2_gateway(context, |
| 100 |
l2_gateway) |
| 101 |
self.driver.create_l2_gateway_precommit(context, |
| 102 |
l2_gateway_instance) |
| 103 |
try: |
| 104 |
self.driver.create_l2_gateway_postcommit( |
| 105 |
context, l2_gateway_instance) |
| 106 |
except exc.L2GatewayServiceDriverError: |
| 107 |
with excutils.save_and_reraise_exception(): |
| 108 |
LOG.error("L2GatewayPlugin.create_l2_gateway_postcommit " |
| 109 |
"failed, deleting l2gateway '%s'", |
| 110 |
l2_gateway_instance['id']) |
| 111 |
self.delete_l2_gateway(context, l2_gateway_instance['id']) |
| 112 |
return l2_gateway_instance |
| 113 |
|
| 114 |
def update_l2_gateway(self, context, l2_gateway_id, l2_gateway): |
| 115 |
"""Update the L2Gateway.""" |
| 116 |
self.validate_l2_gateway_for_update(context, l2_gateway_id, l2_gateway) |
| 117 |
self.driver.update_l2_gateway(context, l2_gateway_id, l2_gateway) |
| 118 |
with db_api.CONTEXT_WRITER.using(context): |
| 119 |
l2_gateway_instance = super().update_l2_gateway(context, |
| 120 |
l2_gateway_id, |
| 121 |
l2_gateway) |
| 122 |
self.driver.update_l2_gateway_precommit(context, |
| 123 |
l2_gateway_instance) |
| 124 |
try: |
| 125 |
self.driver.update_l2_gateway_postcommit( |
| 126 |
context, l2_gateway_instance) |
| 127 |
except exc.L2GatewayServiceDriverError: |
| 128 |
with excutils.save_and_reraise_exception(): |
| 129 |
LOG.error("L2GatewayPlugin.update_l2_gateway_postcommit" |
| 130 |
" failed for l2gateway %s", l2_gateway_id) |
| 131 |
return l2_gateway_instance |
| 132 |
|
| 133 |
def delete_l2_gateway(self, context, l2_gateway_id): |
| 134 |
"""Delete the L2Gateway.""" |
| 135 |
self.validate_l2_gateway_for_delete(context, l2_gateway_id) |
| 136 |
self.driver.delete_l2_gateway(context, l2_gateway_id) |
| 137 |
with db_api.CONTEXT_WRITER.using(context): |
| 138 |
super().delete_l2_gateway(context, |
| 139 |
l2_gateway_id) |
| 140 |
self.driver.delete_l2_gateway_precommit(context, l2_gateway_id) |
| 141 |
try: |
| 142 |
self.driver.delete_l2_gateway_postcommit(context, l2_gateway_id) |
| 143 |
except exc.L2GatewayServiceDriverError: |
| 144 |
with excutils.save_and_reraise_exception(): |
| 145 |
LOG.error("L2GatewayPlugin.delete_l2_gateway_postcommit" |
| 146 |
" failed for l2gateway %s", l2_gateway_id) |
| 147 |
|
| 148 |
def create_l2_gateway_connection(self, context, l2_gateway_connection): |
| 149 |
"""Create an L2Gateway Connection |
| 150 |
|
| 151 |
Bind a Neutron VXLAN Network to Physical Network Segment. |
| 152 |
""" |
| 153 |
self.validate_l2_gateway_connection_for_create( |
| 154 |
context, l2_gateway_connection) |
| 155 |
self.driver.create_l2_gateway_connection(context, |
| 156 |
l2_gateway_connection) |
| 157 |
with db_api.CONTEXT_WRITER.using(context): |
| 158 |
l2_gateway_conn_instance = super().create_l2_gateway_connection( |
| 159 |
context, l2_gateway_connection) |
| 160 |
self.driver.create_l2_gateway_connection_precommit( |
| 161 |
context, l2_gateway_conn_instance) |
| 162 |
try: |
| 163 |
self.driver.create_l2_gateway_connection_postcommit( |
| 164 |
context, l2_gateway_conn_instance) |
| 165 |
except exc.L2GatewayServiceDriverError: |
| 166 |
with excutils.save_and_reraise_exception(): |
| 167 |
LOG.error("L2GatewayPlugin." |
| 168 |
"create_l2_gateway_connection_postcommit " |
| 169 |
"failed, deleting connection '%s'", |
| 170 |
l2_gateway_conn_instance['id']) |
| 171 |
self.delete_l2_gateway_connection( |
| 172 |
context, l2_gateway_conn_instance['id']) |
| 173 |
|
| 174 |
return l2_gateway_conn_instance |
| 175 |
|
| 176 |
def delete_l2_gateway_connection(self, context, l2_gateway_connection_id): |
| 177 |
"""Delete an L2Gateway Connection |
| 178 |
|
| 179 |
Unbind a Neutron VXLAN Network from Physical Network Segment. |
| 180 |
""" |
| 181 |
self.validate_l2_gateway_connection_for_delete( |
| 182 |
context, l2_gateway_connection_id) |
| 183 |
self.driver.delete_l2_gateway_connection( |
| 184 |
context, l2_gateway_connection_id) |
| 185 |
with db_api.CONTEXT_WRITER.using(context): |
| 186 |
super().delete_l2_gateway_connection( |
| 187 |
context, l2_gateway_connection_id) |
| 188 |
self.driver.delete_l2_gateway_connection_precommit( |
| 189 |
context, l2_gateway_connection_id) |
| 190 |
try: |
| 191 |
self.driver.delete_l2_gateway_connection_postcommit( |
| 192 |
context, l2_gateway_connection_id) |
| 193 |
except exc.L2GatewayServiceDriverError: |
| 194 |
with excutils.save_and_reraise_exception(): |
| 195 |
LOG.error( |
| 196 |
"L2GatewayPlugin.delete_l2_gateway_connection_postcommit" |
| 197 |
" failed for connection %s", l2_gateway_connection_id) |