Debian Codegraph @

neutron_taas/services/taas/taas_plugin.py

raw
1 # Copyright (C) 2016 Midokura SARL.
2 # Copyright (C) 2015 Ericsson AB
3 # Copyright (c) 2015 Gigamon
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16
17 from neutron.db import servicetype_db as st_db
18 from neutron.services import provider_configuration as pconf
19 from neutron.services import service_base
20 from neutron_lib.callbacks import events
21 from neutron_lib.callbacks import registry
22 from neutron_lib.callbacks import resources
23 from neutron_lib import constants
24 from neutron_lib.db import api as db_api
25 from neutron_lib import exceptions as n_exc
26 from neutron_lib.exceptions import taas as taas_exc
27
28 from neutron_taas.common import constants as taas_consts
29 from neutron_taas.db import taas_db
30 from neutron_taas.services.taas.service_drivers import (service_driver_context
31 as sd_context)
32
33 from oslo_log import log as logging
34
35 LOG = logging.getLogger(__name__)
36
37
38 def add_provider_configuration(type_manager, service_type):
39 type_manager.add_provider_configuration(
40 service_type,
41 pconf.ProviderConfiguration('neutron_taas', service_type))
42
43
44 @registry.has_registry_receivers
45 class TaasPlugin(taas_db.Taas_db_Mixin):
46
47 supported_extension_aliases = ["taas",
48 "taas-vlan-filter"]
49 path_prefix = "/taas"
50
51 def __init__(self):
52
53 LOG.debug("TAAS PLUGIN INITIALIZATION")
54 self.service_type_manager = st_db.ServiceTypeManager.get_instance()
55 add_provider_configuration(self.service_type_manager,
56 taas_consts.TAAS)
57 self._load_drivers()
58 self.driver = self._get_driver_for_provider(self.default_provider)
59 LOG.info(("TAAS Plugin initialized using service drivers: "
60 "%(service_drivers)s, default: %(default_driver)s"),
61 {'service_drivers': self.drivers.keys(),
62 'default_driver': self.default_provider})
63
64 def _load_drivers(self):
65 """Loads plugin-drivers specified in configuration."""
66 self.drivers, self.default_provider = service_base.load_drivers(
67 taas_consts.TAAS, self)
68
69 def _get_driver_for_provider(self, provider):
70 if provider in self.drivers:
71 return self.drivers[provider]
72 raise n_exc.Invalid("Error retrieving driver for provider %s" %
73 provider)
74
75 @db_api.CONTEXT_WRITER
76 def create_tap_service(self, context, tap_service):
77 LOG.debug("create_tap_service() called")
78
79 t_s = tap_service['tap_service']
80 tenant_id = t_s['tenant_id']
81 port_id = t_s['port_id']
82
83 # Get port details
84 port = self.get_port_details(context, port_id)
85
86 # Check if the port is owned by the tenant.
87 if port['tenant_id'] != tenant_id:
88 raise taas_exc.PortDoesNotBelongToTenant()
89
90 # Extract the host where the port is located
91 host = port['binding:host_id']
92
93 if host is not None:
94 LOG.debug("Host on which the port is created = %s", host)
95 else:
96 LOG.debug("Host could not be found, Port Binding disbaled!")
97
98 # Create tap service in the db model
99 ts = super().create_tap_service(context, tap_service)
100 driver_context = sd_context.TapServiceContext(self, context, ts)
101 self.driver.create_tap_service_precommit(driver_context)
102
103 self.driver.create_tap_service_postcommit(driver_context)
104
105 return ts
106
107 @db_api.CONTEXT_WRITER
108 def delete_tap_service(self, context, id):
109 LOG.debug("delete_tap_service() called")
110
111 # Get all the tap Flows that are associated with the Tap service
112 # and delete them as well
113 t_f_collection = self.get_tap_flows(
114 context,
115 filters={'tap_service_id': [id]}, fields=['id'])
116
117 for t_f in t_f_collection:
118 self.delete_tap_flow(context, t_f['id'])
119
120 ts = self.get_tap_service(context, id)
121 driver_context = sd_context.TapServiceContext(self, context, ts)
122 if ts['status'] == constants.ACTIVE:
123 ts['status'] = constants.PENDING_DELETE
124 super().update_tap_service(
125 context, id, {'tap_service': ts})
126 method = self.driver.delete_tap_service_precommit
127 else:
128 super().delete_tap_service(context, id)
129 method = self.driver.delete_tap_service_postcommit
130
131 method(driver_context)
132
133 @db_api.CONTEXT_WRITER
134 def create_tap_flow(self, context, tap_flow):
135 LOG.debug("create_tap_flow() called")
136
137 t_f = tap_flow['tap_flow']
138 tenant_id = t_f['tenant_id']
139
140 # Check if the tenant id of the source port is the same as the
141 # tenant_id of the tap service we are attaching it to.
142
143 ts = self.get_tap_service(context, t_f['tap_service_id'])
144 ts_tenant_id = ts['tenant_id']
145
146 if tenant_id != ts_tenant_id:
147 raise taas_exc.TapServiceNotBelongToTenant()
148
149 # create tap flow in the db model
150 tf = super().create_tap_flow(context, tap_flow)
151 driver_context = sd_context.TapFlowContext(self, context, tf)
152 self.driver.create_tap_flow_precommit(driver_context)
153
154 self.driver.create_tap_flow_postcommit(driver_context)
155 return tf
156
157 @db_api.CONTEXT_WRITER
158 def delete_tap_flow(self, context, id):
159 LOG.debug("delete_tap_flow() called")
160
161 tf = self.get_tap_flow(context, id)
162 driver_context = sd_context.TapFlowContext(self, context, tf)
163 if tf['status'] == constants.ACTIVE:
164 tf['status'] = constants.PENDING_DELETE
165 super().update_tap_flow(context, id, {'tap_flow': tf})
166 method = self.driver.delete_tap_flow_precommit
167 else:
168 super().delete_tap_flow(context, id)
169 method = self.driver.delete_tap_flow_postcommit
170
171 method(driver_context)
172
173 @registry.receives(resources.PORT, [events.PRECOMMIT_DELETE])
174 def handle_delete_port(self, resource, event, trigger, payload):
175 context = payload.context
176 deleted_port = payload.latest_state
177 if not deleted_port:
178 LOG.error("TaaS: Handle Delete Port: Invalid port object received")
179 return
180
181 deleted_port_id = deleted_port['id']
182 LOG.info("TaaS: Handle Delete Port: %s", deleted_port_id)
183
184 # Get list of configured tap-services
185 t_s_collection = self.get_tap_services(
186 context,
187 filters={'port_id': [deleted_port_id]}, fields=['id'])
188
189 for t_s in t_s_collection:
190 try:
191 self.delete_tap_service(context, t_s['id'])
192 except taas_exc.TapServiceNotFound:
193 LOG.debug("Not found tap_service: %s", t_s['id'])
194
195 t_f_collection = self.get_tap_flows(
196 context,
197 filters={'source_port': [deleted_port_id]}, fields=['id'])
198
199 for t_f in t_f_collection:
200 try:
201 self.delete_tap_flow(context, t_f['id'])
202 except taas_exc.TapFlowNotFound:
203 LOG.debug("Not found tap_flow: %s", t_f['id'])