1
|
|
|
"""Module to handle the storehouse.""" |
2
|
1 |
|
from kytos.core import log |
3
|
1 |
|
from kytos.core.events import KytosEvent |
4
|
|
|
|
5
|
|
|
|
6
|
1 |
|
class StoreHouse: |
7
|
|
|
"""Class to handle storehouse.""" |
8
|
|
|
|
9
|
1 |
|
@classmethod |
10
|
|
|
def __new__(cls, *args, **kwargs): |
11
|
|
|
# pylint: disable=unused-argument |
12
|
|
|
"""Make this class a Singleton.""" |
13
|
1 |
|
instance = cls.__dict__.get("__instance__") |
14
|
1 |
|
if instance is not None: |
15
|
1 |
|
return instance |
16
|
1 |
|
cls.__instance__ = instance = object.__new__(cls) |
17
|
1 |
|
return instance |
18
|
|
|
|
19
|
1 |
|
def __init__(self, controller): |
20
|
|
|
"""Create a storehouse client instance.""" |
21
|
1 |
|
self.controller = controller |
22
|
1 |
|
self.namespace = 'kytos.topology.status' |
23
|
1 |
|
self.list_persistence_boxes() |
24
|
1 |
|
if 'box' not in self.__dict__: |
25
|
1 |
|
self.box = None |
26
|
|
|
|
27
|
1 |
|
def get_persistence_data(self): |
28
|
|
|
"""Return the persistence box data.""" |
29
|
1 |
|
if not self.box: |
30
|
1 |
|
return {} |
31
|
1 |
|
self.get_stored_box(self.box.box_id) |
32
|
1 |
|
return self.box.data |
33
|
|
|
|
34
|
1 |
|
def create_persistence_box(self): |
35
|
|
|
"""Create a new persistence box.""" |
36
|
1 |
|
content = {'namespace': self.namespace, |
37
|
|
|
'callback': self._create_persistence_box_callback, |
38
|
|
|
'data': {}} |
39
|
1 |
|
event = KytosEvent(name='kytos.storehouse.create', content=content) |
40
|
1 |
|
self.controller.buffers.app.put(event) |
41
|
1 |
|
log.info('Creating network persistence box in storehouse.') |
42
|
|
|
|
43
|
1 |
|
def _create_persistence_box_callback(self, _event, data, error): |
44
|
|
|
"""Execute the callback to handle create_persistence_box.""" |
45
|
|
|
if error: |
46
|
|
|
log.error(f'Can\'t create persistence' |
47
|
|
|
f'box with namespace {self.namespace}') |
48
|
|
|
|
49
|
|
|
self.box = data |
50
|
|
|
log.info(f'Persistence box was created.') |
51
|
|
|
|
52
|
1 |
|
def list_persistence_boxes(self): |
53
|
|
|
"""List all network persistences box.""" |
54
|
1 |
|
name = 'kytos.storehouse.list' |
55
|
1 |
|
content = {'namespace': self.namespace, |
56
|
|
|
'callback': self._get_or_create_a_box_from_list_of_boxes} |
57
|
|
|
|
58
|
1 |
|
event = KytosEvent(name=name, content=content) |
59
|
1 |
|
self.controller.buffers.app.put(event) |
60
|
1 |
|
log.debug(f'Bootstrapping storehouse persistence' |
61
|
|
|
f'box for {self.namespace}.') |
62
|
|
|
|
63
|
1 |
|
def _get_or_create_a_box_from_list_of_boxes(self, _event, data, _error): |
64
|
|
|
"""Create a new persistence box or retrieve the stored box.""" |
65
|
1 |
|
if data: |
66
|
1 |
|
self.get_stored_box(data[0]) |
67
|
|
|
else: |
68
|
1 |
|
self.create_persistence_box() |
69
|
|
|
|
70
|
1 |
|
def get_stored_box(self, box_id): |
71
|
|
|
"""Get persistence box from storehouse.""" |
72
|
1 |
|
content = {'namespace': self.namespace, |
73
|
|
|
'callback': self._get_persistence_box_callback, |
74
|
|
|
'box_id': box_id, |
75
|
|
|
'data': {}} |
76
|
1 |
|
name = 'kytos.storehouse.retrieve' |
77
|
1 |
|
event = KytosEvent(name=name, content=content) |
78
|
1 |
|
self.controller.buffers.app.put(event) |
79
|
1 |
|
log.debug(f'Retrieve persistence box with {box_id}' |
80
|
|
|
f'from {self.namespace}.') |
81
|
|
|
|
82
|
1 |
|
def _get_persistence_box_callback(self, _event, data, error): |
83
|
|
|
"""Handle get_box method saving the box or logging with the error.""" |
84
|
|
|
if error: |
85
|
|
|
log.error(f'Persistence box not found.') |
86
|
|
|
|
87
|
|
|
self.box = data |
88
|
|
|
log.debug(f'Persistence box was loaded.') |
89
|
|
|
|
90
|
1 |
|
def save_status(self, status): |
91
|
|
|
"""Save the network administrative status using storehouse.""" |
92
|
1 |
|
self.box.data[status.get('id')] = status |
93
|
|
|
|
94
|
1 |
|
content = {'namespace': self.namespace, |
95
|
|
|
'box_id': self.box.box_id, |
96
|
|
|
'data': self.box.data, |
97
|
|
|
'callback': self._save_status_callback} |
98
|
|
|
|
99
|
1 |
|
event = KytosEvent(name='kytos.storehouse.update', content=content) |
100
|
1 |
|
self.controller.buffers.app.put(event) |
101
|
|
|
|
102
|
1 |
|
def _save_status_callback(self, _event, data, error): |
103
|
|
|
"""Display the saved network status in the log.""" |
104
|
|
|
if error: |
105
|
|
|
log.error(f'Can\'t update persistence box {self.namespace}.') |
106
|
|
|
|
107
|
|
|
log.info(f'Persistence box {data.box_id} in storehouse was updated.') |
108
|
|
|
|