File: db_sync.py

package info (click to toggle)
python-neutron-lib 3.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 7,744 kB
  • sloc: python: 23,121; sh: 137; makefile: 24
file content (78 lines) | stat: -rw-r--r-- 2,830 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import abc
import threading
import time


class BaseOvnDbSynchronizer(metaclass=abc.ABCMeta):

    _required_mechanism_drivers = []
    _required_service_plugins = []
    _required_ml2_ext_drivers = []

    def __init__(self, core_plugin, ovn_driver, mode, is_maintenance=False):
        """Initialize the OVN DB synchronizer.

        :param core_plugin: The Neutron core plugin instance
        :param ovn_driver: The OVN driver instance containing the OVN NB and SB
                           API clients
        :param mode: The synchronization mode, valid values are:
                     'off', 'log', 'repair' or 'migrate'
        :param is_maintenance: Flag indicating if running in maintenance mode.
                               Defaults to False
        """
        self.core_plugin = core_plugin
        self.ovn_nb_api = ovn_driver.nb_ovn
        self.ovn_sb_api = ovn_driver.sb_ovn
        self.ovn_driver = ovn_driver
        self.mode = mode
        self.is_maintenance = is_maintenance
        self._thread = None

    def sync(self, delay_seconds=10):
        time.sleep(delay_seconds)
        self._thread = threading.Thread(target=self.do_sync)
        self._thread.start()

    @classmethod
    def _get_parent_class_attribute(cls, cls_attr):
        parent_attibutes = set()
        for parent in cls.__mro__:
            parent_attibutes |= set(getattr(parent, cls_attr, set()))
        return list(parent_attibutes | set(getattr(cls, cls_attr, [])))

    @classmethod
    def get_required_mechanism_drivers(cls):
        return cls._get_parent_class_attribute('_required_mechanism_drivers')

    @classmethod
    def get_required_service_plugins(cls):
        return cls._get_parent_class_attribute('_required_service_plugins')

    @classmethod
    def get_required_ml2_extension_drivers(cls):
        return cls._get_parent_class_attribute('_required_ml2_ext_drivers')

    @abc.abstractmethod
    def do_sync(self):
        """Method to sync the OVN DB."""

    def stop(self):
        # TODO(ralonsoh): this method and the function executed in the thread
        # should support a fast exit way.
        try:
            self._thread.join()
        except AttributeError:
            # Haven't started syncing
            pass