| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
|
| 14 |
import sqlalchemy as sa |
| 15 |
from sqlalchemy.orm import exc |
| 16 |
|
| 17 |
from neutron_lib.api.definitions import tap_mirror as mirror_extension |
| 18 |
from neutron_lib.db import api as db_api |
| 19 |
from neutron_lib.db import constants as db_const |
| 20 |
from neutron_lib.db import model_base |
| 21 |
from neutron_lib.db import model_query |
| 22 |
from neutron_lib.db import utils as db_utils |
| 23 |
from neutron_lib.exceptions import taas as taas_exc |
| 24 |
from neutron_lib.plugins import directory |
| 25 |
from oslo_log import helpers as log_helpers |
| 26 |
from oslo_log import log as logging |
| 27 |
from oslo_serialization import jsonutils |
| 28 |
from oslo_utils import uuidutils |
| 29 |
|
| 30 |
from neutron_taas.extensions import tap_mirror as tap_m_extension |
| 31 |
|
| 32 |
LOG = logging.getLogger(__name__) |
| 33 |
|
| 34 |
|
| 35 |
class TapMirror(model_base.BASEV2, model_base.HasId, |
| 36 |
model_base.HasProjectNoIndex): |
| 37 |
"""Represents a Tap Mirror |
| 38 |
|
| 39 |
A Tap Mirror can be a GRE or ERSPAN tunnel representation. |
| 40 |
""" |
| 41 |
|
| 42 |
__tablename__ = 'tap_mirrors' |
| 43 |
|
| 44 |
name = sa.Column(sa.String(db_const.NAME_FIELD_SIZE)) |
| 45 |
description = sa.Column(sa.String(db_const.DESCRIPTION_FIELD_SIZE)) |
| 46 |
port_id = sa.Column(sa.String(db_const.UUID_FIELD_SIZE), |
| 47 |
nullable=False) |
| 48 |
directions = sa.Column(sa.String(255), nullable=False) |
| 49 |
remote_ip = sa.Column(sa.String(db_const.IP_ADDR_FIELD_SIZE), |
| 50 |
nullable=False) |
| 51 |
mirror_type = sa.Column(sa.Enum('erspanv1', 'gre', |
| 52 |
name='tapmirrors_type'), |
| 53 |
nullable=False) |
| 54 |
api_collections = [mirror_extension.COLLECTION_NAME] |
| 55 |
collection_resource_map = { |
| 56 |
mirror_extension.COLLECTION_NAME: mirror_extension.RESOURCE_NAME} |
| 57 |
|
| 58 |
|
| 59 |
class Taas_mirror_db_mixin(tap_m_extension.TapMirrorBase): |
| 60 |
|
| 61 |
def _make_tap_mirror_dict(self, tap_mirror, fields=None): |
| 62 |
res = { |
| 63 |
'id': tap_mirror.get('id'), |
| 64 |
'project_id': tap_mirror.get('project_id'), |
| 65 |
'name': tap_mirror.get('name'), |
| 66 |
'description': tap_mirror.get('description'), |
| 67 |
'port_id': tap_mirror.get('port_id'), |
| 68 |
'directions': jsonutils.loads(tap_mirror.get('directions')), |
| 69 |
'remote_ip': tap_mirror.get('remote_ip'), |
| 70 |
'mirror_type': tap_mirror.get('mirror_type'), |
| 71 |
} |
| 72 |
return db_utils.resource_fields(res, fields) |
| 73 |
|
| 74 |
@db_api.retry_if_session_inactive() |
| 75 |
def get_port_details(self, context, port_id): |
| 76 |
with db_api.CONTEXT_READER.using(context): |
| 77 |
core_plugin = directory.get_plugin() |
| 78 |
return core_plugin.get_port(context, port_id) |
| 79 |
|
| 80 |
@db_api.retry_if_session_inactive() |
| 81 |
@log_helpers.log_method_call |
| 82 |
def create_tap_mirror(self, context, tap_mirror): |
| 83 |
fields = tap_mirror['tap_mirror'] |
| 84 |
project_id = fields.get('project_id') |
| 85 |
with db_api.CONTEXT_WRITER.using(context): |
| 86 |
tap_mirror_db = TapMirror( |
| 87 |
id=uuidutils.generate_uuid(), |
| 88 |
project_id=project_id, |
| 89 |
name=fields.get('name'), |
| 90 |
description=fields.get('description'), |
| 91 |
port_id=fields.get('port_id'), |
| 92 |
directions=jsonutils.dumps(fields.get('directions')), |
| 93 |
remote_ip=fields.get('remote_ip'), |
| 94 |
mirror_type=fields.get('mirror_type'), |
| 95 |
) |
| 96 |
# TODO(lajoskatona): Check tunnel_id... |
| 97 |
context.session.add(tap_mirror_db) |
| 98 |
|
| 99 |
return self._make_tap_mirror_dict(tap_mirror_db) |
| 100 |
|
| 101 |
def _get_tap_mirror(self, context, id): |
| 102 |
with db_api.CONTEXT_READER.using(context): |
| 103 |
try: |
| 104 |
return model_query.get_by_id(context, TapMirror, id) |
| 105 |
except exc.NoResultFound: |
| 106 |
raise taas_exc.TapMirrorNotFound(mirror_id=id) |
| 107 |
|
| 108 |
@log_helpers.log_method_call |
| 109 |
def get_tap_mirror(self, context, id, fields=None): |
| 110 |
with db_api.CONTEXT_READER.using(context): |
| 111 |
t_m = self._get_tap_mirror(context, id) |
| 112 |
return self._make_tap_mirror_dict(t_m, fields) |
| 113 |
|
| 114 |
@db_api.retry_if_session_inactive() |
| 115 |
@log_helpers.log_method_call |
| 116 |
def get_tap_mirrors(self, context, filters=None, fields=None, |
| 117 |
sorts=None, limit=None, marker=None, |
| 118 |
page_reverse=False): |
| 119 |
with db_api.CONTEXT_READER.using(context): |
| 120 |
return model_query.get_collection(context, TapMirror, |
| 121 |
self._make_tap_mirror_dict, |
| 122 |
filters=filters, fields=fields) |
| 123 |
|
| 124 |
@log_helpers.log_method_call |
| 125 |
def delete_tap_mirror(self, context, id): |
| 126 |
with db_api.CONTEXT_WRITER.using(context): |
| 127 |
count = context.session.query(TapMirror).filter_by(id=id).delete() |
| 128 |
|
| 129 |
if not count: |
| 130 |
raise taas_exc.TapMirrorNotFound(mirror_id=id) |
| 131 |
|
| 132 |
@db_api.retry_if_session_inactive() |
| 133 |
@log_helpers.log_method_call |
| 134 |
def update_tap_mirror(self, context, id, tap_mirror): |
| 135 |
t_m = tap_mirror['tap_mirror'] |
| 136 |
with db_api.CONTEXT_WRITER.using(context): |
| 137 |
tap_mirror_db = self._get_tap_mirror(context, id) |
| 138 |
tap_mirror_db.update(t_m) |
| 139 |
return self._make_tap_mirror_dict(tap_mirror_db) |