| 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 abc |
| 14 |
import threading |
| 15 |
import time |
| 16 |
|
| 17 |
|
| 18 |
class BaseOvnDbSynchronizer(metaclass=abc.ABCMeta): |
| 19 |
|
| 20 |
_required_mechanism_drivers = [] |
| 21 |
_required_service_plugins = [] |
| 22 |
_required_ml2_ext_drivers = [] |
| 23 |
|
| 24 |
def __init__(self, core_plugin, ovn_driver, mode, is_maintenance=False): |
| 25 |
"""Initialize the OVN DB synchronizer. |
| 26 |
|
| 27 |
:param core_plugin: The Neutron core plugin instance |
| 28 |
:param ovn_driver: The OVN driver instance containing the OVN NB and SB |
| 29 |
API clients |
| 30 |
:param mode: The synchronization mode, valid values are: |
| 31 |
'off', 'log', 'repair' or 'migrate' |
| 32 |
:param is_maintenance: Flag indicating if running in maintenance mode. |
| 33 |
Defaults to False |
| 34 |
""" |
| 35 |
self.core_plugin = core_plugin |
| 36 |
self.ovn_nb_api = ovn_driver.nb_ovn |
| 37 |
self.ovn_sb_api = ovn_driver.sb_ovn |
| 38 |
self.ovn_driver = ovn_driver |
| 39 |
self.mode = mode |
| 40 |
self.is_maintenance = is_maintenance |
| 41 |
self._thread = None |
| 42 |
|
| 43 |
def sync(self, delay_seconds=10): |
| 44 |
time.sleep(delay_seconds) |
| 45 |
self._thread = threading.Thread(target=self.do_sync) |
| 46 |
self._thread.start() |
| 47 |
|
| 48 |
@classmethod |
| 49 |
def _get_parent_class_attribute(cls, cls_attr): |
| 50 |
parent_attibutes = set() |
| 51 |
for parent in cls.__mro__: |
| 52 |
parent_attibutes |= set(getattr(parent, cls_attr, set())) |
| 53 |
return list(parent_attibutes | set(getattr(cls, cls_attr, []))) |
| 54 |
|
| 55 |
@classmethod |
| 56 |
def get_required_mechanism_drivers(cls): |
| 57 |
return cls._get_parent_class_attribute('_required_mechanism_drivers') |
| 58 |
|
| 59 |
@classmethod |
| 60 |
def get_required_service_plugins(cls): |
| 61 |
return cls._get_parent_class_attribute('_required_service_plugins') |
| 62 |
|
| 63 |
@classmethod |
| 64 |
def get_required_ml2_extension_drivers(cls): |
| 65 |
return cls._get_parent_class_attribute('_required_ml2_ext_drivers') |
| 66 |
|
| 67 |
@abc.abstractmethod |
| 68 |
def do_sync(self): |
| 69 |
"""Method to sync the OVN DB.""" |
| 70 |
|
| 71 |
def stop(self): |
| 72 |
# TODO(ralonsoh): this method and the function executed in the thread |
| 73 |
# should support a fast exit way. |
| 74 |
try: |
| 75 |
self._thread.join() |
| 76 |
except AttributeError: |
| 77 |
# Haven't started syncing |
| 78 |
pass |