Debian Codegraph @

neutron_taas/services/taas/tap_mirror_plugin.py

raw
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 from neutron.db import servicetype_db as st_db
14 from neutron.services import provider_configuration as pconf
15 from neutron.services import service_base
16 from neutron_lib.api.definitions import portbindings
17 from neutron_lib.api.definitions import tap_mirror as t_m_api_def
18 from neutron_lib.callbacks import events
19 from neutron_lib.callbacks import registry
20 from neutron_lib.callbacks import resources
21 from neutron_lib.db import api as db_api
22 from neutron_lib import exceptions as n_exc
23 from neutron_lib.exceptions import taas as taas_exc
24 from oslo_log import helpers as log_helpers
25 from oslo_log import log as logging
26 from oslo_utils import excutils
27
28 from neutron_taas.common import constants as taas_consts
29 from neutron_taas.db import tap_mirror_db
30 from neutron_taas.extensions import tap_mirror_both_direction as t_m_b_api_def
31 from neutron_taas.services.taas.service_drivers import (service_driver_context
32 as sd_context)
33
34 LOG = logging.getLogger(__name__)
35
36
37 @registry.has_registry_receivers
38 class TapMirrorPlugin(tap_mirror_db.Taas_mirror_db_mixin):
39
40 supported_extension_aliases = [t_m_api_def.ALIAS,
41 t_m_b_api_def.ALIAS,
42 ]
43
44 path_prefix = "/taas"
45
46 def __init__(self):
47 super().__init__()
48 self.service_type_manager = st_db.ServiceTypeManager.get_instance()
49 self.service_type_manager.add_provider_configuration(
50 taas_consts.TAAS,
51 pconf.ProviderConfiguration('neutron_taas', taas_consts.TAAS))
52
53 # Load the service driver from neutron.conf.
54 self.drivers, self.default_provider = service_base.load_drivers(
55 taas_consts.TAAS, self)
56 # Associate driver names to driver objects
57 for driver_name, driver in self.drivers.items():
58 driver.name = driver_name
59
60 self.driver = self._get_driver_for_provider(self.default_provider)
61
62 LOG.info(("Tap Mirror plugin using service drivers: "
63 "%(service_drivers)s, default: %(default_driver)s"),
64 {'service_drivers': self.drivers.keys(),
65 'default_driver': self.default_provider})
66
67 def _get_driver_for_provider(self, provider):
68 if provider in self.drivers:
69 return self.drivers[provider]
70 raise n_exc.Invalid("Error retrieving driver for provider %s" %
71 provider)
72
73 @log_helpers.log_method_call
74 def create_tap_mirror(self, context, tap_mirror):
75 t_m = tap_mirror['tap_mirror']
76 port_id = t_m['port_id']
77 project_id = t_m['project_id']
78
79 with db_api.CONTEXT_READER.using(context):
80 # Get port details
81 port = self.get_port_details(context, port_id)
82 if port['tenant_id'] != project_id:
83 raise taas_exc.PortDoesNotBelongToTenant()
84
85 host = port[portbindings.HOST_ID]
86 if host is not None:
87 LOG.debug("Host on which the port is created = %s", host)
88 else:
89 LOG.debug("Host could not be found, Port Binding disbaled!")
90 # Fail here? Is it a valid usecase to create a mirror for a
91 # port that is not bound?
92
93 with db_api.CONTEXT_WRITER.using(context):
94 self._validate_tap_tunnel_id(context, t_m['directions'])
95 tm = super().create_tap_mirror(context, tap_mirror)
96 # Precommit phase, is it necessary? tunnel id check should be in
97 # it....
98 driver_context = sd_context.TapMirrorContext(self, context, tm)
99 self.driver.create_tap_mirror_precommit(driver_context)
100
101 # Postcommit phase, do the backend stuff, or fail.
102 try:
103 self.driver.create_tap_mirror_postcommit(driver_context)
104 except Exception:
105 pass
106 return tm
107
108 def _validate_tap_tunnel_id(self, context, mirror_directions):
109 mirrors = self.get_tap_mirrors(context)
110 for mirror in mirrors:
111 for direction, tunnel_id in mirror['directions'].items():
112 if tunnel_id in mirror_directions.values():
113 raise taas_exc.TapMirrorTunnelConflict(
114 tunnel_id=tunnel_id)
115
116 @log_helpers.log_method_call
117 def delete_tap_mirror(self, context, id):
118 with db_api.CONTEXT_WRITER.using(context):
119 tm = self.get_tap_mirror(context, id)
120
121 if tm:
122 # Precommit phase
123 driver_context = sd_context.TapMirrorContext(
124 self, context, tm)
125 self.driver.delete_tap_mirror_precommit(driver_context)
126
127 # check if tunnel id was really deleted
128 super().delete_tap_mirror(context, id)
129
130 # Postcommit phase, do the backend stuff, or fail.
131 try:
132 self.driver.delete_tap_mirror_postcommit(driver_context)
133 except Exception:
134 with excutils.save_and_reraise_exception():
135 LOG.error("Failed to delete Tap Mirror on driver. "
136 "tap_mirror: %s", id)
137
138 @registry.receives(resources.PORT, [events.PRECOMMIT_DELETE])
139 @log_helpers.log_method_call
140 def handle_delete_port(self, resource, event, trigger, payload):
141 context = payload.context
142 deleted_port = payload.latest_state
143 if not deleted_port:
144 LOG.error("Tap Mirror: Handle Delete Port: "
145 "Invalid port object received")
146 return
147
148 deleted_port_id = deleted_port['id']
149 LOG.info("Tap Mirror: Handle Delete Port: %s", deleted_port_id)
150
151 tap_mirrors = self.get_tap_mirrors(
152 context,
153 filters={'port_id': [deleted_port_id]}, fields=['id'])
154
155 for t_m in tap_mirrors:
156 try:
157 self.delete_tap_mirror(context, t_m['id'])
158 except taas_exc.TapMirrorNotFound:
159 LOG.debug("Tap Mirror not found: %s", t_m['id'])