Passed
Pull Request — master (#126)
by
unknown
02:06
created

build.storehouse.StoreHouse._delete_box_callback()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3.1852

Importance

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