Debian Codegraph @

neutron_taas/services/taas/service_drivers/taas_rpc.py

raw
1 # Copyright (C) 2018 AT&T
2 # Copyright (C) 2016 Midokura SARL.
3 # Copyright (C) 2015 Ericsson AB
4 # Copyright (c) 2015 Gigamon
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17
18 from neutron_lib.api.definitions import portbindings
19 from neutron_lib import constants
20 from neutron_lib.db import api as db_api
21 from neutron_lib import exceptions as n_exc
22 from neutron_lib import rpc as n_rpc
23
24 from neutron_taas.common import constants as taas_consts
25 from neutron_taas.common import topics
26 from neutron_taas.services.taas import service_drivers
27 from neutron_taas.services.taas.service_drivers import (service_driver_context
28 as sd_context)
29 from neutron_taas.services.taas.service_drivers import taas_agent_api
30 from neutron_taas.services.taas import taas_plugin
31
32 from oslo_config import cfg
33 from oslo_log import helpers as log_helpers
34 from oslo_log import log as logging
35 from oslo_utils import excutils
36
37 LOG = logging.getLogger(__name__)
38
39
40 class TaasCallbacks:
41
42 def __init__(self, rpc_driver, plugin):
43 super().__init__()
44 self.rpc_driver = rpc_driver
45 self.plugin = plugin
46
47 def sync_tap_resources(self, context, sync_tap_res, host):
48 """Handle Rpc from Agent to sync up Tap resources."""
49 LOG.debug("In RPC Call for Sync Tap Resources: MSG=%s", sync_tap_res)
50
51 # Get list of configured tap-services
52 active_tss = self.plugin.get_tap_services(
53 context,
54 filters={'status': [constants.ACTIVE]})
55
56 for ts in active_tss:
57 # If tap-service port is bound to a different host than the one
58 # which sent this RPC, then continue.
59 ts_port = self.plugin.get_port_details(
60 context, ts['port_id'])
61 if ts_port['binding:host_id'] != host:
62 continue
63
64 driver_context = sd_context.TapServiceContext(self.plugin,
65 context, ts)
66 try:
67 self.rpc_driver.create_tap_service_postcommit(driver_context)
68 except Exception:
69 with excutils.save_and_reraise_exception():
70 LOG.error("Failed to create tap service on driver,"
71 "deleting tap_service %s", ts['id'])
72 # pylint: disable=E1003
73 super(taas_plugin.TaasPlugin,
74 self.plugin).delete_tap_service(context, ts['id'])
75
76 # Get all the active tap flows for current tap-service
77 active_tfs = self.plugin.get_tap_flows(
78 context,
79 filters={'tap_service_id': [ts['id']],
80 'status': [constants.ACTIVE]})
81
82 # Filter out the tap flows associated with distinct tap services
83 for tf in active_tfs:
84 driver_context = sd_context.TapFlowContext(self.plugin,
85 context, tf)
86 try:
87 self.rpc_driver.create_tap_flow_postcommit(driver_context)
88 except Exception:
89 with excutils.save_and_reraise_exception():
90 LOG.error("Failed to create tap flow on driver,"
91 "deleting tap_flow %s", tf['id'])
92 # pylint: disable=E1003
93 super(taas_plugin.TaasPlugin,
94 self.plugin).delete_tap_flow(context, tf['id'])
95
96 @db_api.CONTEXT_WRITER
97 def set_tap_service_status(self, context, msg, status, host=None):
98 """Handle Rpc from Agent to set the status of Tap resources."""
99 LOG.info("In RPC Call to set tap service status: Host=%s, "
100 "MSG=%s, STATUS=%s", host, msg, status)
101
102 # Clear the resource from DB once agent indicates successful deletion
103 # by mech driver.
104 if status == constants.INACTIVE:
105 ts = self.plugin.get_tap_service(context, msg['id'])
106 driver_context = sd_context.TapServiceContext(self.plugin,
107 context,
108 ts)
109 # pylint: disable=E1003
110 super(taas_plugin.TaasPlugin,
111 self.plugin).delete_tap_service(context, msg['id'])
112 self.plugin.driver.delete_tap_service_postcommit(
113 driver_context)
114 return
115
116 ts = self.plugin.get_tap_service(context, msg['id'])
117 ts['status'] = status
118 # pylint: disable=E1003
119 super(taas_plugin.TaasPlugin, self.plugin).update_tap_service(
120 context,
121 msg['id'],
122 {'tap_service': ts})
123
124 @db_api.CONTEXT_WRITER
125 def set_tap_flow_status(self, context, msg, status, host=None):
126 """Handle Rpc from Agent to set the status of Tap resources."""
127 LOG.info("In RPC Call to set tap flow status: Host=%s, "
128 "MSG=%s, STATUS=%s", host, msg, status)
129
130 # Clear the resource from DB once agent indicates successful deletion
131 # by mech driver.
132 if status == constants.INACTIVE:
133 tf = self.plugin.get_tap_flow(context, msg['id'])
134 driver_context = sd_context.TapFlowContext(self.plugin,
135 context,
136 tf)
137 # pylint: disable=E1003
138 super(taas_plugin.TaasPlugin, self.plugin).delete_tap_flow(
139 context, msg['id'])
140 self.plugin.driver.delete_tap_flow_postcommit(driver_context)
141 return
142
143 tf = self.plugin.get_tap_flow(context, msg['id'])
144 tf['status'] = status
145 # pylint: disable=E1003
146 super(taas_plugin.TaasPlugin, self.plugin).update_tap_flow(
147 context, msg['id'], {'tap_flow': tf})
148
149
150 class TaasRpcDriver(service_drivers.TaasBaseDriver):
151 """Taas Rpc Service Driver class"""
152
153 def __init__(self, service_plugin):
154 LOG.debug("Loading TaasRpcDriver.")
155 super().__init__(service_plugin)
156 self.endpoints = [TaasCallbacks(self, service_plugin)]
157 self.conn = n_rpc.Connection()
158 self.conn.create_consumer(topics.TAAS_PLUGIN,
159 self.endpoints, fanout=False)
160
161 self.conn.consume_in_threads()
162
163 self.agent_rpc = taas_agent_api.TaasAgentApi(
164 topics.TAAS_AGENT,
165 cfg.CONF.host
166 )
167
168 def _get_taas_id(self, context, tf):
169 ts = self.service_plugin.get_tap_service(context,
170 tf['tap_service_id'])
171 taas_id = (self.service_plugin.get_tap_id_association(
172 context,
173 tap_service_id=ts['id']))['taas_id']
174 return taas_id
175
176 def create_tap_service_precommit(self, context):
177 ts = context.tap_service
178 tap_id_association = context._plugin.create_tap_id_association(
179 context._plugin_context, ts['id'])
180 context.tap_id_association = tap_id_association
181
182 def create_tap_service_postcommit(self, context):
183 """Send tap service creation RPC message to agent.
184
185 This RPC message includes taas_id that is added vlan_range_start to
186 so that taas-ovs-agent can use taas_id as VLANID.
187 """
188 # Get taas id associated with the Tap Service
189 ts = context.tap_service
190 tap_id_association = context.tap_id_association
191 taas_vlan_id = tap_id_association['taas_id']
192 port = self.service_plugin.get_port_details(context._plugin_context,
193 ts['port_id'])
194 host = port['binding:host_id']
195
196 rpc_msg = {'tap_service': ts,
197 'taas_id': taas_vlan_id,
198 'port': port}
199
200 self.agent_rpc.create_tap_service(context._plugin_context,
201 rpc_msg, host)
202
203 def delete_tap_service_precommit(self, context):
204 """Send tap service deletion RPC message to agent.
205
206 This RPC message includes taas_id that is added vlan_range_start to
207 so that taas-ovs-agent can use taas_id as VLANID.
208 """
209 ts = context.tap_service
210 tap_id_association = context.tap_id_association
211 taas_vlan_id = tap_id_association['taas_id']
212
213 try:
214 port = self.service_plugin.get_port_details(
215 context._plugin_context,
216 ts['port_id'])
217 host = port['binding:host_id']
218 except n_exc.PortNotFound:
219 # if not found, we just pass to None
220 port = None
221 host = None
222
223 rpc_msg = {'tap_service': ts,
224 'taas_id': taas_vlan_id,
225 'port': port}
226
227 self.agent_rpc.delete_tap_service(context._plugin_context,
228 rpc_msg, host)
229
230 def delete_tap_service_postcommit(self, context):
231 pass
232
233 def create_tap_flow_precommit(self, context):
234 pass
235
236 def create_tap_flow_postcommit(self, context):
237 """Send tap flow creation RPC message to agent."""
238 tf = context.tap_flow
239 taas_id = self._get_taas_id(context._plugin_context, tf)
240 # Extract the host where the source port is located
241 port = self.service_plugin.get_port_details(context._plugin_context,
242 tf['source_port'])
243 host = port['binding:host_id']
244 port_mac = port['mac_address']
245 # Extract the tap-service port
246 ts = self.service_plugin.get_tap_service(context._plugin_context,
247 tf['tap_service_id'])
248 ts_port = self.service_plugin.get_port_details(
249 context._plugin_context, ts['port_id'])
250 # Get network data where the tap flow port is located
251 tf_nw = self.service_plugin.get_port_network_data(
252 context._plugin_context, port)
253 # Send RPC message to both the source port host and
254 # tap service(destination) port host
255 rpc_msg = {'tap_flow': tf,
256 'port_mac': port_mac,
257 'taas_id': taas_id,
258 'port': port,
259 'tap_service_port': ts_port,
260 'tf_nw': tf_nw
261 }
262
263 self.agent_rpc.create_tap_flow(context._plugin_context, rpc_msg, host)
264
265 def delete_tap_flow_precommit(self, context):
266 """Send tap flow deletion RPC message to agent."""
267 tf = context.tap_flow
268 taas_id = self._get_taas_id(context._plugin_context, tf)
269 # Extract the host where the source port is located
270 port = self.service_plugin.get_port_details(context._plugin_context,
271 tf['source_port'])
272 host = port['binding:host_id']
273 port_mac = port['mac_address']
274 # Extract the tap-service port
275 ts = self.service_plugin.get_tap_service(context._plugin_context,
276 tf['tap_service_id'])
277 ts_port = self.service_plugin.get_port_details(
278 context._plugin_context, ts['port_id'])
279
280 src_vlans_list = []
281 vlan_filter_list = []
282
283 if port.get(portbindings.VNIC_TYPE) == portbindings.VNIC_DIRECT:
284 # Get all the tap Flows that are associated with the Tap service
285 active_tfs = self.service_plugin.get_tap_flows(
286 context._plugin_context,
287 filters={'tap_service_id': [tf['tap_service_id']],
288 'status': [constants.ACTIVE]},
289 fields=['source_port', 'vlan_filter'])
290
291 for tap_flow in active_tfs:
292 source_port = self.service_plugin.get_port_details(
293 context._plugin_context, tap_flow['source_port'])
294
295 LOG.debug("taas: active TF's source_port %(source_port)s",
296 {'source_port': source_port})
297
298 src_vlans = ""
299 if source_port.get(portbindings.VIF_DETAILS):
300 src_vlans = source_port[portbindings.VIF_DETAILS].get(
301 portbindings.VIF_DETAILS_VLAN)
302
303 # If no VLAN filter configured on source port,
304 # then include all vlans
305 if not src_vlans or src_vlans == '0':
306 src_vlans = taas_consts.VLAN_RANGE
307
308 src_vlans_list.append(src_vlans)
309
310 vlan_filter = tap_flow['vlan_filter']
311 # If no VLAN filter configured for tap-flow,
312 # then include all vlans
313 if not vlan_filter:
314 vlan_filter = taas_consts.VLAN_RANGE
315
316 vlan_filter_list.append(vlan_filter)
317
318 # Send RPC message to both the source port host and
319 # tap service(destination) port host
320 rpc_msg = {'tap_flow': tf,
321 'port_mac': port_mac,
322 'taas_id': taas_id,
323 'port': port,
324 'tap_service_port': ts_port,
325 'source_vlans_list': src_vlans_list,
326 'vlan_filter_list': vlan_filter_list}
327
328 self.agent_rpc.delete_tap_flow(context._plugin_context, rpc_msg, host)
329
330 def delete_tap_flow_postcommit(self, context):
331 pass
332
333 @log_helpers.log_method_call
334 def create_tap_mirror_precommit(self, context):
335 pass
336
337 @log_helpers.log_method_call
338 def create_tap_mirror_postcommit(self, context):
339 """Send Tap Mirror creation RPC message to agent.
340
341 This RPC message includes ....
342 """
343 # Get taas id associated with the Tap Service
344 tm = context.tap_mirror
345 port = self.service_plugin.get_port_details(context._plugin_context,
346 tm['port_id'])
347 host = port['binding:host_id']
348
349 rpc_msg = {'tap_mirror': tm,
350 'port': port}
351
352 self.agent_rpc.create_tap_mirror(context._plugin_context,
353 rpc_msg, host)
354
355 @log_helpers.log_method_call
356 def delete_tap_mirror_precommit(self, context):
357 """Send Tap Mirror deletion RPC message to agent.
358
359 This RPC message includes .....
360 """
361 tm = context.tap_mirror
362
363 try:
364 port = self.service_plugin.get_port_details(
365 context._plugin_context,
366 tm['port_id'])
367 host = port['binding:host_id']
368 except n_exc.PortNotFound:
369 # if not found, we just pass to None
370 port = None
371 host = None
372
373 rpc_msg = {'tap_mirror': tm,
374 'port': port}
375
376 self.agent_rpc.delete_tap_mirror(context._plugin_context,
377 rpc_msg, host)
378
379 @log_helpers.log_method_call
380 def delete_tap_mirror_postcommit(self, context):
381 pass