| 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 |
import queue |
| 14 |
import threading |
| 15 |
|
| 16 |
from neutron.conf.plugins.ml2.drivers.ovn import ovn_conf |
| 17 |
from neutron_lib.callbacks import events |
| 18 |
from neutron_lib.callbacks import registry |
| 19 |
from neutron_lib.callbacks import resources |
| 20 |
from oslo_config import cfg |
| 21 |
from oslo_log import helpers as log_helpers |
| 22 |
from oslo_log import log as logging |
| 23 |
from ovs.stream import Stream |
| 24 |
|
| 25 |
from neutron_taas.services.taas.service_drivers.ovn.ovsdb import impl_idl_taas |
| 26 |
|
| 27 |
|
| 28 |
LOG = logging.getLogger(__name__) |
| 29 |
CONF = cfg.CONF |
| 30 |
|
| 31 |
|
| 32 |
class TaasOvnProviderHelper(): |
| 33 |
|
| 34 |
def __init__(self): |
| 35 |
ovn_conf.register_opts() |
| 36 |
self._requests = queue.Queue() |
| 37 |
self._helper_thread = threading.Thread(target=self._request_handler) |
| 38 |
self._helper_thread.daemon = True |
| 39 |
self._check_and_set_ssl_files() |
| 40 |
self._taas_mirror_func_map = { |
| 41 |
'mirror_del': self.mirror_del, |
| 42 |
'mirror_add': self.mirror_add, |
| 43 |
} |
| 44 |
self._subscribe() |
| 45 |
self._helper_thread.start() |
| 46 |
|
| 47 |
def _subscribe(self): |
| 48 |
registry.subscribe(self._post_fork_initialize, |
| 49 |
resources.PROCESS, |
| 50 |
events.AFTER_INIT) |
| 51 |
|
| 52 |
def _post_fork_initialize(self, resource, event, trigger, payload=None): |
| 53 |
self.ovn_nbdb = impl_idl_taas.OvnNbIdlForTaas() |
| 54 |
self.ovn_nbdb_api = self.ovn_nbdb.start() |
| 55 |
|
| 56 |
def _check_and_set_ssl_files(self): |
| 57 |
priv_key_file = CONF.ovn.ovn_nb_private_key |
| 58 |
cert_file = CONF.ovn.ovn_nb_certificate |
| 59 |
ca_cert_file = CONF.ovn.ovn_nb_ca_cert |
| 60 |
|
| 61 |
if priv_key_file: |
| 62 |
Stream.ssl_set_private_key_file(priv_key_file) |
| 63 |
|
| 64 |
if cert_file: |
| 65 |
Stream.ssl_set_certificate_file(cert_file) |
| 66 |
|
| 67 |
if ca_cert_file: |
| 68 |
Stream.ssl_set_ca_cert_file(ca_cert_file) |
| 69 |
|
| 70 |
def _request_handler(self): |
| 71 |
while True: |
| 72 |
request = self._requests.get() |
| 73 |
request_type = request['type'] |
| 74 |
if request_type == 'exit': |
| 75 |
break |
| 76 |
|
| 77 |
request_handler = self._taas_mirror_func_map.get(request_type) |
| 78 |
try: |
| 79 |
if request_handler: |
| 80 |
request_handler(request['info']) |
| 81 |
self._requests.task_done() |
| 82 |
except Exception: |
| 83 |
# If any unexpected exception happens we don't want the |
| 84 |
# notify_loop to exit. |
| 85 |
LOG.exception('Unexpected exception in request_handler') |
| 86 |
|
| 87 |
def _execute_commands(self, commands): |
| 88 |
with self.ovn_nbdb_api.transaction(check_error=True) as txn: |
| 89 |
for command in commands: |
| 90 |
txn.add(command) |
| 91 |
|
| 92 |
def shutdown(self): |
| 93 |
self._requests.put({'type': 'exit'}) |
| 94 |
self._helper_thread.join() |
| 95 |
if hasattr(self, 'ovn_nbdb'): |
| 96 |
self.ovn_nbdb.stop() |
| 97 |
if hasattr(self, 'ovn_nbdb_api'): |
| 98 |
del self.ovn_nbdb_api |
| 99 |
|
| 100 |
def add_request(self, req): |
| 101 |
self._requests.put(req) |
| 102 |
|
| 103 |
@log_helpers.log_method_call |
| 104 |
def mirror_del(self, request): |
| 105 |
port_id = request.pop('port_id') |
| 106 |
ovn_port = self.ovn_nbdb_api.lookup('Logical_Switch_Port', port_id) |
| 107 |
mirror = self.ovn_nbdb_api.mirror_get( |
| 108 |
request['name']).execute(check_error=True) |
| 109 |
self.ovn_nbdb_api.lsp_detach_mirror( |
| 110 |
ovn_port.name, mirror.uuid, |
| 111 |
if_exist=True).execute(check_error=True) |
| 112 |
self.ovn_nbdb_api.mirror_del( |
| 113 |
mirror.uuid).execute(check_error=True) |
| 114 |
|
| 115 |
@log_helpers.log_method_call |
| 116 |
def mirror_add(self, request): |
| 117 |
port_id = request.pop('port_id') |
| 118 |
ovn_port = self.ovn_nbdb_api.lookup('Logical_Switch_Port', port_id) |
| 119 |
|
| 120 |
mirror = self.ovn_nbdb_api.mirror_add( |
| 121 |
**request).execute(check_error=True) |
| 122 |
self.ovn_nbdb_api.lsp_attach_mirror( |
| 123 |
ovn_port.name, mirror.uuid, |
| 124 |
may_exist=True).execute(check_error=True) |
| 125 |
|
| 126 |
return mirror |