Passed
Push — master ( 51d10d...074f60 )
by Humberto
02:18
created

build.storehouse   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 79.69%

Importance

Changes 0
Metric Value
eloc 76
dl 0
loc 115
ccs 51
cts 64
cp 0.7969
rs 10
c 0
b 0
f 0
wmc 20

11 Methods

Rating   Name   Duplication   Size   Complexity  
A StoreHouse.__new__() 0 9 2
A StoreHouse.__init__() 0 10 2
A StoreHouse._save_status_callback() 0 6 2
A StoreHouse.get_data() 0 11 4
A StoreHouse.get_stored_box() 0 9 1
A StoreHouse.create_box() 0 7 1
A StoreHouse._get_or_create_a_box_from_list_of_boxes() 0 6 2
A StoreHouse._get_box_callback() 0 6 2
A StoreHouse.list_stored_boxes() 0 8 1
A StoreHouse._create_box_callback() 0 7 2
A StoreHouse.save_status() 0 11 1
1
"""Module to handle the storehouse."""
2 1
import time
3
4 1
from kytos.core import log
5 1
from kytos.core.events import KytosEvent
6 1
from napps.kytos.topology import settings
7
8 1
DEFAULT_BOX_RESTORE_TIMER = 0.1
9 1
BOX_RESTORE_ATTEMPTS = 20
10
11
12 1
class StoreHouse:
13
    """Class to handle storehouse."""
14
15 1
    @classmethod
16
    def __new__(cls, *args, **kwargs):
17
        # pylint: disable=unused-argument
18
        """Make this class a Singleton."""
19 1
        instance = cls.__dict__.get("__instance__")
20 1
        if instance is not None:
21 1
            return instance
22 1
        cls.__instance__ = instance = object.__new__(cls)
23 1
        return instance
24
25 1
    def __init__(self, controller):
26
        """Create a storehouse client instance."""
27 1
        self.controller = controller
28 1
        self.namespace = 'kytos.topology.status'
29 1
        self.box_restore_timer = getattr(settings, 'BOX_RESTORE_TIMER',
30
                                         DEFAULT_BOX_RESTORE_TIMER)
31
32 1
        if 'box' not in self.__dict__:
33 1
            self.box = None
34 1
        self.list_stored_boxes()
35
36 1
    def get_data(self):
37
        """Return the persistence box data."""
38
        # Wait for box retrieve from storehouse
39 1
        i = 0
40 1
        while not self.box and i < BOX_RESTORE_ATTEMPTS:
41
            time.sleep(self.box_restore_timer)
42
            i += 1
43 1
        if not self.box:
44
            error = 'Error retrieving persistence box from storehouse.'
45
            raise FileNotFoundError(error)
46 1
        return self.box.data
47
48 1
    def create_box(self):
49
        """Create a persistence box to store administrative changes."""
50 1
        content = {'namespace': self.namespace,
51
                   'callback': self._create_box_callback,
52
                   'data': {}}
53 1
        event = KytosEvent(name='kytos.storehouse.create', content=content)
54 1
        self.controller.buffers.app.put(event)
55
56 1
    def _create_box_callback(self, _event, data, error):
57
        """Execute the callback to handle create_box."""
58
        if error:
59
            log.error(f'Can\'t create persistence'
60
                      f'box with namespace {self.namespace}')
61
62
        self.box = data
63
64 1
    def list_stored_boxes(self):
65
        """List all persistence box stored in storehouse."""
66 1
        name = 'kytos.storehouse.list'
67 1
        content = {'namespace': self.namespace,
68
                   'callback': self._get_or_create_a_box_from_list_of_boxes}
69
70 1
        event = KytosEvent(name=name, content=content)
71 1
        self.controller.buffers.app.put(event)
72
73 1
    def _get_or_create_a_box_from_list_of_boxes(self, _event, data, _error):
74
        """Create a persistence box or retrieve the stored box."""
75 1
        if data:
76 1
            self.get_stored_box(data[0])
77
        else:
78 1
            self.create_box()
79
80 1
    def get_stored_box(self, box_id):
81
        """Get persistence box from storehouse."""
82 1
        content = {'namespace': self.namespace,
83
                   'callback': self._get_box_callback,
84
                   'box_id': box_id,
85
                   'data': {}}
86 1
        name = 'kytos.storehouse.retrieve'
87 1
        event = KytosEvent(name=name, content=content)
88 1
        self.controller.buffers.app.put(event)
89
90 1
    def _get_box_callback(self, _event, data, error):
91
        """Handle get_box method saving the box or logging with the error."""
92
        if error:
93
            log.error('Persistence box not found.')
94
95
        self.box = data
96
97 1
    def save_status(self, status):
98
        """Save the network administrative status using storehouse."""
99 1
        self.box.data[status.get('id')] = status
100
101 1
        content = {'namespace': self.namespace,
102
                   'box_id': self.box.box_id,
103
                   'data': self.box.data,
104
                   'callback': self._save_status_callback}
105
106 1
        event = KytosEvent(name='kytos.storehouse.update', content=content)
107 1
        self.controller.buffers.app.put(event)
108
109 1
    def _save_status_callback(self, _event, data, error):
110
        """Display the saved network status in the log."""
111
        if error:
112
            log.error(f'Can\'t update persistence box {data.box_id}.')
113
114
        log.info('Network administrative status saved in '
115
                 f'{self.namespace}.{data.box_id}')
116