| 1 |
# Copyright 2015 OpenStack Foundation |
| 2 |
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 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 networking_l2gw.db.l2gateway import db_query |
| 17 |
from networking_l2gw.db.l2gateway import l2gateway_models as models |
| 18 |
from networking_l2gw.extensions import l2gateway |
| 19 |
from networking_l2gw.extensions import l2gatewayconnection |
| 20 |
from networking_l2gw.services.l2gateway.common import config |
| 21 |
from networking_l2gw.services.l2gateway.common import constants |
| 22 |
from networking_l2gw.services.l2gateway.common import l2gw_validators |
| 23 |
from networking_l2gw.services.l2gateway import exceptions as l2gw_exc |
| 24 |
|
| 25 |
from neutron_lib.callbacks import events |
| 26 |
from neutron_lib.callbacks import registry |
| 27 |
from neutron_lib.callbacks import resources |
| 28 |
from neutron_lib.db import api as db_api |
| 29 |
from neutron_lib.db import model_query |
| 30 |
from neutron_lib import exceptions |
| 31 |
from neutron_lib.plugins import directory |
| 32 |
from oslo_log import log as logging |
| 33 |
from oslo_utils import uuidutils |
| 34 |
from sqlalchemy.orm import exc as sa_orm_exc |
| 35 |
|
| 36 |
LOG = logging.getLogger(__name__) |
| 37 |
|
| 38 |
|
| 39 |
class L2GatewayMixin(l2gateway.L2GatewayPluginBase, |
| 40 |
db_query.L2GatewayCommonDbMixin, |
| 41 |
l2gatewayconnection.L2GatewayConnectionPluginBase): |
| 42 |
"""Class L2GatewayMixin for handling l2_gateway resource.""" |
| 43 |
gateway_resource = constants.GATEWAY_RESOURCE_NAME |
| 44 |
connection_resource = constants.CONNECTION_RESOURCE_NAME |
| 45 |
config.register_l2gw_opts_helper() |
| 46 |
|
| 47 |
@db_api.retry_if_session_inactive() |
| 48 |
@db_api.CONTEXT_READER |
| 49 |
def _get_l2_gateway(self, context, gw_id): |
| 50 |
try: |
| 51 |
gw = model_query.get_by_id(context, models.L2Gateway, gw_id) |
| 52 |
except sa_orm_exc.NoResultFound: |
| 53 |
raise l2gw_exc.L2GatewayNotFound(gateway_id=gw_id) |
| 54 |
return gw |
| 55 |
|
| 56 |
@db_api.retry_if_session_inactive() |
| 57 |
@db_api.CONTEXT_READER |
| 58 |
def _get_l2_gateways(self, context): |
| 59 |
return model_query.get_collection(context, models.L2Gateway, |
| 60 |
dict_func=None) |
| 61 |
|
| 62 |
@db_api.retry_if_session_inactive() |
| 63 |
@db_api.CONTEXT_READER |
| 64 |
def _get_l2_gw_interfaces(self, context, id): |
| 65 |
return context.session.query(models.L2GatewayInterface).filter_by( |
| 66 |
device_id=id).all() |
| 67 |
|
| 68 |
@db_api.CONTEXT_READER |
| 69 |
def _is_vlan_configured_on_any_interface_for_l2gw(self, |
| 70 |
context, |
| 71 |
l2gw_id): |
| 72 |
devices_db = self._get_l2_gateway_devices(context, l2gw_id) |
| 73 |
for device_model in devices_db: |
| 74 |
interfaces_db = self._get_l2_gw_interfaces(context, |
| 75 |
device_model.id) |
| 76 |
for int_model in interfaces_db: |
| 77 |
query = context.session.query(models.L2GatewayInterface) |
| 78 |
int_db = query.filter_by(id=int_model.id).first() |
| 79 |
seg_id = int_db[constants.SEG_ID] |
| 80 |
if seg_id > 0: |
| 81 |
return True |
| 82 |
return False |
| 83 |
|
| 84 |
@db_api.retry_if_session_inactive() |
| 85 |
def _get_l2_gateway_devices(self, context, l2gw_id): |
| 86 |
return context.session.query(models.L2GatewayDevice).filter_by( |
| 87 |
l2_gateway_id=l2gw_id).all() |
| 88 |
|
| 89 |
def _get_l2gw_devices_by_name_andl2gwid(self, context, device_name, |
| 90 |
l2gw_id): |
| 91 |
return context.session.query(models.L2GatewayDevice).filter_by( |
| 92 |
device_name=device_name, l2_gateway_id=l2gw_id).all() |
| 93 |
|
| 94 |
def _get_l2_gateway_connection(self, context, cn_id): |
| 95 |
try: |
| 96 |
con = context.session.query(models.L2GatewayConnection).get(cn_id) |
| 97 |
except sa_orm_exc.NoResultFound: |
| 98 |
raise l2gw_exc.L2GatewayConnectionNotFound(id=cn_id) |
| 99 |
return con |
| 100 |
|
| 101 |
def _make_l2gw_connections_dict(self, gw_conn, fields=None): |
| 102 |
if gw_conn is None: |
| 103 |
raise l2gw_exc.L2GatewayConnectionNotFound(id="") |
| 104 |
segmentation_id = gw_conn['segmentation_id'] |
| 105 |
if segmentation_id == 0: |
| 106 |
segmentation_id = "" |
| 107 |
res = {'id': gw_conn['id'], |
| 108 |
'network_id': gw_conn['network_id'], |
| 109 |
'l2_gateway_id': gw_conn['l2_gateway_id'], |
| 110 |
'tenant_id': gw_conn['tenant_id'], |
| 111 |
'segmentation_id': segmentation_id |
| 112 |
} |
| 113 |
return self._fields(res, fields) |
| 114 |
|
| 115 |
def _make_l2_gateway_dict(self, l2_gateway, fields=None): |
| 116 |
device_list = [] |
| 117 |
for d in l2_gateway.devices: |
| 118 |
interface_list = [] |
| 119 |
for interfaces_db in d.interfaces: |
| 120 |
seg_id = interfaces_db[constants.SEG_ID] |
| 121 |
if seg_id == 0: |
| 122 |
seg_id = "" |
| 123 |
interface_list.append({'name': |
| 124 |
interfaces_db['interface_name'], |
| 125 |
constants.SEG_ID: |
| 126 |
seg_id}) |
| 127 |
aligned_int__list = self._align_interfaces_list(interface_list) |
| 128 |
device_list.append({'device_name': d['device_name'], |
| 129 |
'id': d['id'], |
| 130 |
'interfaces': aligned_int__list}) |
| 131 |
res = {'id': l2_gateway['id'], |
| 132 |
'name': l2_gateway['name'], |
| 133 |
'devices': device_list, |
| 134 |
'tenant_id': l2_gateway['tenant_id']} |
| 135 |
return self._fields(res, fields) |
| 136 |
|
| 137 |
def _set_mapping_info_defaults(self, mapping_info): |
| 138 |
if not mapping_info.get(constants.SEG_ID): |
| 139 |
mapping_info[constants.SEG_ID] = 0 |
| 140 |
|
| 141 |
def _retrieve_gateway_connections(self, context, gateway_id, |
| 142 |
mapping_info={}, only_one=False): |
| 143 |
filters = {'l2_gateway_id': [gateway_id]} |
| 144 |
for k, v in mapping_info.items(): |
| 145 |
if v and k != constants.SEG_ID: |
| 146 |
filters[k] = [v] |
| 147 |
query = self._get_collection_query(context, |
| 148 |
models.L2GatewayConnection, |
| 149 |
filters) |
| 150 |
return query.one() if only_one else query.all() |
| 151 |
|
| 152 |
def create_l2_gateway(self, context, l2_gateway): |
| 153 |
"""Create a logical gateway.""" |
| 154 |
self._admin_check(context, 'CREATE') |
| 155 |
gw = l2_gateway[self.gateway_resource] |
| 156 |
tenant_id = self._get_tenant_id_for_create(context, gw) |
| 157 |
devices = gw['devices'] |
| 158 |
with db_api.CONTEXT_WRITER.using(context): |
| 159 |
gw_db = models.L2Gateway( |
| 160 |
id=gw.get('id', uuidutils.generate_uuid()), |
| 161 |
tenant_id=tenant_id, |
| 162 |
name=gw.get('name')) |
| 163 |
context.session.add(gw_db) |
| 164 |
l2gw_device_dict = {} |
| 165 |
for device in devices: |
| 166 |
l2gw_device_dict['l2_gateway_id'] = id |
| 167 |
device_name = device['device_name'] |
| 168 |
l2gw_device_dict['device_name'] = device_name |
| 169 |
l2gw_device_dict['id'] = uuidutils.generate_uuid() |
| 170 |
uuid = self._generate_uuid() |
| 171 |
dev_db = models.L2GatewayDevice(id=uuid, |
| 172 |
l2_gateway_id=gw_db.id, |
| 173 |
device_name=device_name) |
| 174 |
context.session.add(dev_db) |
| 175 |
for interface_list in device['interfaces']: |
| 176 |
int_name = interface_list.get('name') |
| 177 |
if constants.SEG_ID in interface_list: |
| 178 |
seg_id_list = interface_list.get(constants.SEG_ID) |
| 179 |
for seg_ids in seg_id_list: |
| 180 |
uuid = self._generate_uuid() |
| 181 |
interface_db = self._get_int_model(uuid, |
| 182 |
int_name, |
| 183 |
dev_db.id, |
| 184 |
seg_ids) |
| 185 |
context.session.add(interface_db) |
| 186 |
else: |
| 187 |
uuid = self._generate_uuid() |
| 188 |
interface_db = self._get_int_model(uuid, |
| 189 |
int_name, |
| 190 |
dev_db.id, |
| 191 |
0) |
| 192 |
context.session.add(interface_db) |
| 193 |
context.session.query(models.L2GatewayDevice).all() |
| 194 |
return self._make_l2_gateway_dict(gw_db) |
| 195 |
|
| 196 |
@db_api.CONTEXT_WRITER |
| 197 |
def update_l2_gateway(self, context, id, l2_gateway): |
| 198 |
"""Update L2Gateway.""" |
| 199 |
gw = l2_gateway[self.gateway_resource] |
| 200 |
devices = gw.get('devices') |
| 201 |
dev_db = None |
| 202 |
l2gw_db = None |
| 203 |
with db_api.CONTEXT_WRITER.using(context): |
| 204 |
l2gw_db = self._get_l2_gateway(context, id) |
| 205 |
if not devices and l2gw_db: |
| 206 |
l2gw_db.name = gw.get('name') |
| 207 |
return self._make_l2_gateway_dict(l2gw_db) |
| 208 |
if devices: |
| 209 |
for device in devices: |
| 210 |
dev_name = device['device_name'] |
| 211 |
dev_db = (self._get_l2gw_devices_by_name_andl2gwid( |
| 212 |
context, dev_name, id)) |
| 213 |
interface_dict_list = [i for i in device['interfaces']] |
| 214 |
interface_db = self._get_l2_gw_interfaces(context, |
| 215 |
dev_db[0].id) |
| 216 |
self._delete_l2_gateway_interfaces(context, interface_db) |
| 217 |
self._update_interfaces_db(context, interface_dict_list, |
| 218 |
dev_db) |
| 219 |
if l2gw_db: |
| 220 |
if gw.get('name'): |
| 221 |
l2gw_db.name = gw.get('name') |
| 222 |
return self._make_l2_gateway_dict(l2gw_db) |
| 223 |
|
| 224 |
def _update_interfaces_db(self, context, interface_dict_list, device_db): |
| 225 |
for interfaces in interface_dict_list: |
| 226 |
int_name = interfaces.get('name') |
| 227 |
if constants.SEG_ID in interfaces: |
| 228 |
seg_id_list = interfaces.get(constants.SEG_ID) |
| 229 |
for seg_ids in seg_id_list: |
| 230 |
uuid = self._generate_uuid() |
| 231 |
int_db = self._get_int_model(uuid, |
| 232 |
int_name, |
| 233 |
device_db[0].id, |
| 234 |
seg_ids) |
| 235 |
context.session.add(int_db) |
| 236 |
else: |
| 237 |
uuid = self._generate_uuid() |
| 238 |
interface_db = self._get_int_model(uuid, |
| 239 |
int_name, |
| 240 |
device_db[0].id, |
| 241 |
0) |
| 242 |
context.session.add(interface_db) |
| 243 |
|
| 244 |
@db_api.CONTEXT_READER |
| 245 |
def get_l2_gateway(self, context, id, fields=None): |
| 246 |
"""get the l2 gateway by id.""" |
| 247 |
self._admin_check(context, 'GET') |
| 248 |
gw_db = self._get_l2_gateway(context, id) |
| 249 |
return self._make_l2_gateway_dict(gw_db, fields) |
| 250 |
|
| 251 |
def delete_l2_gateway(self, context, id): |
| 252 |
"""delete the l2 gateway by id.""" |
| 253 |
gw_db = self._get_l2_gateway(context, id) |
| 254 |
if gw_db: |
| 255 |
with db_api.CONTEXT_WRITER.using(context): |
| 256 |
context.session.delete(gw_db) |
| 257 |
LOG.debug("l2 gateway '%s' was deleted.", id) |
| 258 |
|
| 259 |
@db_api.CONTEXT_READER |
| 260 |
def get_l2_gateways(self, context, filters=None, fields=None, |
| 261 |
sorts=None, |
| 262 |
limit=None, |
| 263 |
marker=None, |
| 264 |
page_reverse=False): |
| 265 |
"""list the l2 gateways available in the neutron DB.""" |
| 266 |
self._admin_check(context, 'GET') |
| 267 |
marker_obj = self._get_marker_obj( |
| 268 |
context, 'l2_gateway', limit, marker) |
| 269 |
return self._get_collection(context, models.L2Gateway, |
| 270 |
self._make_l2_gateway_dict, |
| 271 |
filters=filters, fields=fields, |
| 272 |
sorts=sorts, limit=limit, |
| 273 |
marker_obj=marker_obj, |
| 274 |
page_reverse=page_reverse) |
| 275 |
|
| 276 |
def _update_segmentation_id(self, context, l2gw_id, segmentation_id): |
| 277 |
"""Update segmentation id for interfaces.""" |
| 278 |
device_db = self._get_l2_gateway_devices(context, l2gw_id) |
| 279 |
for device_model in device_db: |
| 280 |
interface_db = self._get_l2_gw_interfaces(context, |
| 281 |
device_model.id) |
| 282 |
for interface_model in interface_db: |
| 283 |
interface_model.segmentation_id = segmentation_id |
| 284 |
|
| 285 |
def _delete_l2_gateway_interfaces(self, context, int_db_list): |
| 286 |
"""delete the l2 interfaces by id.""" |
| 287 |
with db_api.CONTEXT_WRITER.using(context): |
| 288 |
for interfaces in int_db_list: |
| 289 |
context.session.delete(interfaces) |
| 290 |
LOG.debug("l2 gateway interfaces was deleted.") |
| 291 |
|
| 292 |
def create_l2_gateway_connection(self, context, l2_gateway_connection): |
| 293 |
"""Create l2 gateway connection.""" |
| 294 |
gw_connection = l2_gateway_connection[self.connection_resource] |
| 295 |
l2_gw_id = gw_connection.get('l2_gateway_id') |
| 296 |
network_id = gw_connection.get('network_id') |
| 297 |
nw_map = {} |
| 298 |
nw_map['network_id'] = network_id |
| 299 |
nw_map['l2_gateway_id'] = l2_gw_id |
| 300 |
segmentation_id = "" |
| 301 |
if constants.SEG_ID in gw_connection: |
| 302 |
segmentation_id = gw_connection.get(constants.SEG_ID) |
| 303 |
nw_map[constants.SEG_ID] = segmentation_id |
| 304 |
with db_api.CONTEXT_WRITER.using(context): |
| 305 |
gw_db = self._get_l2_gateway(context, l2_gw_id) |
| 306 |
tenant_id = self._get_tenant_id_for_create(context, gw_db) |
| 307 |
nw_map['tenant_id'] = tenant_id |
| 308 |
if not segmentation_id: |
| 309 |
nw_map['segmentation_id'] = "0" |
| 310 |
conn_db = self._retrieve_gateway_connections(context, l2_gw_id, |
| 311 |
nw_map) |
| 312 |
if not conn_db: |
| 313 |
connection_id = uuidutils.generate_uuid() |
| 314 |
nw_map['id'] = connection_id |
| 315 |
gw_db.network_connections.append( |
| 316 |
models.L2GatewayConnection(**nw_map)) |
| 317 |
gw_db = models.L2GatewayConnection( |
| 318 |
id=connection_id, |
| 319 |
tenant_id=tenant_id, |
| 320 |
network_id=network_id, |
| 321 |
l2_gateway_id=l2_gw_id, |
| 322 |
segmentation_id=segmentation_id) |
| 323 |
return self._make_l2gw_connections_dict(gw_db) |
| 324 |
return self._make_l2gw_connections_dict(conn_db[0]) |
| 325 |
|
| 326 |
@db_api.CONTEXT_READER |
| 327 |
def get_l2_gateway_connections_count(self, context, filters=None): |
| 328 |
return len(self._get_collection(context, models.L2GatewayConnection, |
| 329 |
self._make_l2gw_connections_dict, |
| 330 |
filters=filters)) |
| 331 |
|
| 332 |
@db_api.CONTEXT_READER |
| 333 |
def get_l2_gateway_connections(self, context, filters=None, |
| 334 |
fields=None, |
| 335 |
sorts=None, limit=None, marker=None, |
| 336 |
page_reverse=False): |
| 337 |
"""List l2 gateway connections.""" |
| 338 |
self._admin_check(context, 'GET') |
| 339 |
marker_obj = self._get_marker_obj( |
| 340 |
context, 'l2_gateway_connection', limit, marker) |
| 341 |
return self._get_collection(context, models.L2GatewayConnection, |
| 342 |
self._make_l2gw_connections_dict, |
| 343 |
filters=filters, fields=fields, |
| 344 |
sorts=sorts, limit=limit, |
| 345 |
marker_obj=marker_obj, |
| 346 |
page_reverse=page_reverse) |
| 347 |
|
| 348 |
@db_api.CONTEXT_READER |
| 349 |
def get_l2_gateway_connection(self, context, id, fields=None): |
| 350 |
"""Get l2 gateway connection.""" |
| 351 |
self._admin_check(context, 'GET') |
| 352 |
"""Get the l2 gateway connection by id.""" |
| 353 |
gw_db = self._get_l2_gateway_connection(context, id) |
| 354 |
return self._make_l2gw_connections_dict(gw_db, fields) |
| 355 |
|
| 356 |
def delete_l2_gateway_connection(self, context, id): |
| 357 |
"""Delete the l2 gateway connection by id.""" |
| 358 |
with db_api.CONTEXT_WRITER.using(context): |
| 359 |
gw_db = self._get_l2_gateway_connection(context, id) |
| 360 |
context.session.delete(gw_db) |
| 361 |
LOG.debug("l2 gateway '%s' was destroyed.", id) |
| 362 |
|
| 363 |
def _admin_check(self, context, action): |
| 364 |
"""Admin role check helper.""" |
| 365 |
# TODO(selva): his check should be required if the tenant_id is |
| 366 |
# specified in the request, otherwise the policy.yaml do a trick |
| 367 |
# this need further revision. |
| 368 |
if not context.is_admin: |
| 369 |
reason = _('Cannot %s resource for non admin tenant') % action |
| 370 |
raise exceptions.AdminRequired(reason=reason) |
| 371 |
|
| 372 |
def _generate_uuid(self): |
| 373 |
"""Generate uuid helper.""" |
| 374 |
uuid = uuidutils.generate_uuid() |
| 375 |
return uuid |
| 376 |
|
| 377 |
def _get_int_model(self, uuid, interface_name, dev_id, seg_id): |
| 378 |
return models.L2GatewayInterface(id=uuid, |
| 379 |
interface_name=interface_name, |
| 380 |
device_id=dev_id, |
| 381 |
segmentation_id=seg_id) |
| 382 |
|
| 383 |
def get_l2gateway_devices_by_gateway_id(self, context, l2_gateway_id): |
| 384 |
"""Get l2gateway_devices_by id.""" |
| 385 |
session = context.session |
| 386 |
with session.begin(): |
| 387 |
return session.query(models.L2GatewayDevice).filter_by( |
| 388 |
l2_gateway_id=l2_gateway_id).all() |
| 389 |
|
| 390 |
def get_l2gateway_interfaces_by_device_id(self, context, device_id): |
| 391 |
"""Get all l2gateway_interfaces_by device_id.""" |
| 392 |
session = context.session |
| 393 |
with session.begin(): |
| 394 |
return session.query(models.L2GatewayInterface).filter_by( |
| 395 |
device_id=device_id).all() |
| 396 |
|
| 397 |
def validate_device_name(self, context, device_name, l2gw_id): |
| 398 |
if device_name: |
| 399 |
devices_db = self._get_l2gw_devices_by_name_andl2gwid(context, |
| 400 |
device_name, |
| 401 |
l2gw_id) |
| 402 |
if not devices_db: |
| 403 |
raise l2gw_exc.L2GatewayDeviceNameNotFound(device_name=device_name) |
| 404 |
|
| 405 |
def _validate_any_seg_id_empty_in_interface_dict(self, devices): |
| 406 |
"""Validate segmentation_id for consistency.""" |
| 407 |
for device in devices: |
| 408 |
interface_list = device['interfaces'] |
| 409 |
if not interface_list: |
| 410 |
raise l2gw_exc.L2GatewayInterfaceRequired() |
| 411 |
if constants.SEG_ID in interface_list[0]: |
| 412 |
for interfaces in interface_list[1:len(interface_list)]: |
| 413 |
if constants.SEG_ID not in interfaces: |
| 414 |
raise l2gw_exc.L2GatewaySegmentationRequired() |
| 415 |
if constants.SEG_ID not in interface_list[0]: |
| 416 |
for interfaces in interface_list[1:len(interface_list)]: |
| 417 |
if constants.SEG_ID in interfaces: |
| 418 |
raise l2gw_exc.L2GatewaySegmentationRequired() |
| 419 |
|
| 420 |
def _align_interfaces_list(self, interface_list): |
| 421 |
"""Align interfaces list based on input dict for multiple seg ids.""" |
| 422 |
interface_dict = {} |
| 423 |
aligned_interface_list = [] |
| 424 |
for interfaces in interface_list: |
| 425 |
actual__name = interfaces.get('name') |
| 426 |
if actual__name in interface_dict: |
| 427 |
interface_name = interface_dict.get(actual__name) |
| 428 |
seg_id_list = interfaces.get(constants.SEG_ID) |
| 429 |
interface_name.append(str(seg_id_list)) |
| 430 |
interface_dict.update({actual__name: interface_name}) |
| 431 |
else: |
| 432 |
seg_id = str(interfaces.get(constants.SEG_ID)).split() |
| 433 |
interface_dict.update({actual__name: seg_id}) |
| 434 |
for name in interface_dict: |
| 435 |
aligned_interface_list.append({'segmentation_id': |
| 436 |
interface_dict[name], |
| 437 |
'name': name}) |
| 438 |
return aligned_interface_list |
| 439 |
|
| 440 |
@db_api.CONTEXT_READER |
| 441 |
def _get_l2_gateway_connections(self, context): |
| 442 |
"""Get l2 gateway connections.""" |
| 443 |
try: |
| 444 |
con = context.session.query(models.L2GatewayConnection).all() |
| 445 |
except sa_orm_exc.NoResultFound: |
| 446 |
raise l2gw_exc.L2GatewayConnectionNotFound( |
| 447 |
id="") |
| 448 |
return con |
| 449 |
|
| 450 |
@db_api.CONTEXT_READER |
| 451 |
def _get_l2gw_ids_by_interface_switch(self, context, interface_name, |
| 452 |
switch_name): |
| 453 |
"""Get l2 gateway ids by interface and switch.""" |
| 454 |
connections = self._get_l2_gateway_connections(context) |
| 455 |
l2gw_id_list = [] |
| 456 |
if connections: |
| 457 |
for connection in connections: |
| 458 |
l2gw_id = connection.l2_gateway_id |
| 459 |
devices = self._get_l2_gateway_device_by_name_id(context, |
| 460 |
switch_name, |
| 461 |
l2gw_id) |
| 462 |
if devices: |
| 463 |
for device in devices: |
| 464 |
interfaces = self._get_l2_gw_interfaces(context, |
| 465 |
device.id) |
| 466 |
for interface in interfaces: |
| 467 |
if interface_name == interface.interface_name: |
| 468 |
l2gw_id_list.append(l2gw_id) |
| 469 |
else: |
| 470 |
LOG.debug("l2 gateway devices are empty") |
| 471 |
else: |
| 472 |
LOG.debug("l2 gateway connections are empty") |
| 473 |
return l2gw_id_list |
| 474 |
|
| 475 |
def _delete_connection_by_l2gw_id(self, context, l2gw_id): |
| 476 |
"""Delete the l2 gateway connection by l2gw id.""" |
| 477 |
with db_api.CONTEXT_WRITER.using(context): |
| 478 |
con_db = self._get_l2_gateway_connection_by_l2gw_id(context, |
| 479 |
l2gw_id) |
| 480 |
if con_db: |
| 481 |
context.session.delete(con_db[0]) |
| 482 |
LOG.debug("l2 gateway connection was destroyed.") |
| 483 |
|
| 484 |
def _get_l2_gateway_connection_by_l2gw_id(self, context, l2gw_id): |
| 485 |
"""Get the l2 gateway connection by l2gw id.""" |
| 486 |
try: |
| 487 |
con = context.session.query(models.L2GatewayConnection).filter_by( |
| 488 |
l2_gateway_id=l2gw_id).all() |
| 489 |
except sa_orm_exc.NoResultFound: |
| 490 |
raise l2gw_exc.L2GatewayConnectionNotFound( |
| 491 |
id=l2gw_id) |
| 492 |
return con |
| 493 |
|
| 494 |
def _get_l2_gateway_device_by_name_id(self, context, device_name, l2gw_id): |
| 495 |
"""Get the l2 gateway device by name and id.""" |
| 496 |
try: |
| 497 |
gw = context.session.query(models.L2GatewayDevice).filter_by( |
| 498 |
device_name=device_name, l2_gateway_id=l2gw_id).all() |
| 499 |
except sa_orm_exc.NoResultFound: |
| 500 |
raise l2gw_exc.L2GatewayDeviceNotFound( |
| 501 |
device_id=device_name) |
| 502 |
return gw |
| 503 |
|
| 504 |
def validate_l2_gateway_for_create(self, context, l2_gateway): |
| 505 |
self._admin_check(context, 'CREATE') |
| 506 |
gw = l2_gateway[self.gateway_resource] |
| 507 |
devices = gw['devices'] |
| 508 |
self._validate_any_seg_id_empty_in_interface_dict(devices) |
| 509 |
|
| 510 |
def validate_l2_gateway_for_delete(self, context, l2gw_id): |
| 511 |
self._admin_check(context, 'DELETE') |
| 512 |
gw_db = self._get_l2_gateway(context, l2gw_id) |
| 513 |
if gw_db.network_connections: |
| 514 |
raise l2gw_exc.L2GatewayInUse(gateway_id=l2gw_id) |
| 515 |
return |
| 516 |
|
| 517 |
def validate_l2_gateway_for_update(self, context, id, l2_gateway): |
| 518 |
self._admin_check(context, 'UPDATE') |
| 519 |
gw = l2_gateway[self.gateway_resource] |
| 520 |
devices = None |
| 521 |
dev_db = None |
| 522 |
if 'devices' in gw: |
| 523 |
devices = gw['devices'] |
| 524 |
if not devices: |
| 525 |
return |
| 526 |
with db_api.CONTEXT_READER.using(context): |
| 527 |
# Attemp to retrieve l2gw |
| 528 |
gw_db = self._get_l2_gateway(context, id) |
| 529 |
if devices: |
| 530 |
for device in devices: |
| 531 |
dev_name = device['device_name'] |
| 532 |
dev_db = self._get_l2gw_devices_by_name_andl2gwid( |
| 533 |
context, dev_name, id) |
| 534 |
if not dev_db: |
| 535 |
raise l2gw_exc.L2GatewayDeviceNotFound( |
| 536 |
device_id=dev_name) |
| 537 |
self.validate_device_name(context, dev_name, id) |
| 538 |
interface_list = device['interfaces'] |
| 539 |
if not interface_list: |
| 540 |
raise l2gw_exc.L2GatewayInterfaceRequired() |
| 541 |
if constants.SEG_ID in interface_list[0]: |
| 542 |
for interfaces in interface_list: |
| 543 |
if constants.SEG_ID not in interfaces: |
| 544 |
raise l2gw_exc.L2GatewaySegmentationRequired() |
| 545 |
if constants.SEG_ID not in interface_list[0]: |
| 546 |
for interfaces in interface_list[1:]: |
| 547 |
if constants.SEG_ID in interfaces: |
| 548 |
raise l2gw_exc.L2GatewaySegmentationRequired() |
| 549 |
if gw_db.devices: |
| 550 |
if gw_db.devices[0].interfaces[0]['segmentation_id']: |
| 551 |
if constants.SEG_ID not in interface_list[0]: |
| 552 |
raise l2gw_exc.L2GatewaySegmentationIDExists() |
| 553 |
else: |
| 554 |
if constants.SEG_ID in interface_list[0]: |
| 555 |
raise l2gw_exc.\ |
| 556 |
L2GatewaySegmentationIDNotExists() |
| 557 |
|
| 558 |
def validate_l2_gateway_connection_for_create(self, context, |
| 559 |
l2_gateway_connection): |
| 560 |
self._admin_check(context, 'CREATE') |
| 561 |
gw_connection = l2_gateway_connection[self.connection_resource] |
| 562 |
l2_gw_id = gw_connection.get('l2_gateway_id') |
| 563 |
network_id = gw_connection.get('network_id') |
| 564 |
plugin = directory.get_plugin() |
| 565 |
plugin.get_network(context, network_id) |
| 566 |
nw_map = {} |
| 567 |
nw_map['network_id'] = network_id |
| 568 |
nw_map['l2_gateway_id'] = l2_gw_id |
| 569 |
segmentation_id = "" |
| 570 |
if constants.SEG_ID in gw_connection: |
| 571 |
segmentation_id = gw_connection.get(constants.SEG_ID) |
| 572 |
nw_map[constants.SEG_ID] = segmentation_id |
| 573 |
is_vlan = self._is_vlan_configured_on_any_interface_for_l2gw(context, |
| 574 |
l2_gw_id) |
| 575 |
network_id = l2gw_validators.validate_network_mapping_list(nw_map, |
| 576 |
is_vlan) |
| 577 |
with db_api.CONTEXT_WRITER.using(context): |
| 578 |
if self._retrieve_gateway_connections(context, |
| 579 |
l2_gw_id, |
| 580 |
nw_map): |
| 581 |
raise l2gw_exc.L2GatewayConnectionExists(mapping=nw_map, |
| 582 |
gateway_id=l2_gw_id) |
| 583 |
|
| 584 |
@db_api.CONTEXT_READER |
| 585 |
def validate_l2_gateway_connection_for_delete(self, context, |
| 586 |
l2_gateway_conn_id): |
| 587 |
self._admin_check(context, 'DELETE') |
| 588 |
gw_db = self._get_l2_gateway_connection(context, |
| 589 |
l2_gateway_conn_id) |
| 590 |
if gw_db is None: |
| 591 |
raise l2gw_exc.L2GatewayConnectionNotFound( |
| 592 |
id=l2_gateway_conn_id) |
| 593 |
|
| 594 |
|
| 595 |
def l2gw_callback(resource, event, trigger, payload): |
| 596 |
LOG.info("AAAA0 l2gw_callback ") |
| 597 |
l2gwservice = directory.get_plugin(constants.L2GW) |
| 598 |
context = payload.context |
| 599 |
port_dict = payload.latest_state |
| 600 |
if l2gwservice: |
| 601 |
if event == events.AFTER_UPDATE: |
| 602 |
l2gwservice.add_port_mac(context, port_dict) |
| 603 |
elif event == events.AFTER_DELETE: |
| 604 |
l2gwservice.delete_port_mac(context, port_dict) |
| 605 |
|
| 606 |
|
| 607 |
def subscribe(): |
| 608 |
interested_events = (events.AFTER_UPDATE, |
| 609 |
events.AFTER_DELETE) |
| 610 |
for x in interested_events: |
| 611 |
registry.subscribe( |
| 612 |
l2gw_callback, resources.PORT, x) |