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_stored_boxes() |
24
|
1 |
|
if 'box' not in self.__dict__: |
25
|
1 |
|
self.box = None |
26
|
|
|
|
27
|
1 |
|
def get_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_box(self): |
35
|
|
|
"""Create a persistence box to store administrative changes.""" |
36
|
1 |
|
content = {'namespace': self.namespace, |
37
|
|
|
'callback': self._create_box_callback, |
38
|
|
|
'data': {}} |
39
|
1 |
|
event = KytosEvent(name='kytos.storehouse.create', content=content) |
40
|
1 |
|
self.controller.buffers.app.put(event) |
41
|
|
|
|
42
|
1 |
|
def _create_box_callback(self, _event, data, error): |
43
|
|
|
"""Execute the callback to handle create_box.""" |
44
|
|
|
if error: |
45
|
|
|
log.error(f'Can\'t create persistence' |
46
|
|
|
f'box with namespace {self.namespace}') |
47
|
|
|
|
48
|
|
|
self.box = data |
49
|
|
|
|
50
|
1 |
|
def list_stored_boxes(self): |
51
|
|
|
"""List all persistence box stored in storehouse.""" |
52
|
1 |
|
name = 'kytos.storehouse.list' |
53
|
1 |
|
content = {'namespace': self.namespace, |
54
|
|
|
'callback': self._get_or_create_a_box_from_list_of_boxes} |
55
|
|
|
|
56
|
1 |
|
event = KytosEvent(name=name, content=content) |
57
|
1 |
|
self.controller.buffers.app.put(event) |
58
|
|
|
|
59
|
1 |
|
def _get_or_create_a_box_from_list_of_boxes(self, _event, data, _error): |
60
|
|
|
"""Create a persistence box or retrieve the stored box.""" |
61
|
1 |
|
if data: |
62
|
1 |
|
self.get_stored_box(data[0]) |
63
|
|
|
else: |
64
|
1 |
|
self.create_box() |
65
|
|
|
|
66
|
1 |
|
def get_stored_box(self, box_id): |
67
|
|
|
"""Get persistence box from storehouse.""" |
68
|
1 |
|
content = {'namespace': self.namespace, |
69
|
|
|
'callback': self._get_box_callback, |
70
|
|
|
'box_id': box_id, |
71
|
|
|
'data': {}} |
72
|
1 |
|
name = 'kytos.storehouse.retrieve' |
73
|
1 |
|
event = KytosEvent(name=name, content=content) |
74
|
1 |
|
self.controller.buffers.app.put(event) |
75
|
|
|
|
76
|
1 |
|
def _get_box_callback(self, _event, data, error): |
77
|
|
|
"""Handle get_box method saving the box or logging with the error.""" |
78
|
|
|
if error: |
79
|
|
|
log.error('Persistence box not found.') |
80
|
|
|
|
81
|
|
|
self.box = data |
82
|
|
|
|
83
|
1 |
|
def save_status(self, status): |
84
|
|
|
"""Save the network administrative status using storehouse.""" |
85
|
1 |
|
self.box.data[status.get('id')] = status |
86
|
|
|
|
87
|
1 |
|
content = {'namespace': self.namespace, |
88
|
|
|
'box_id': self.box.box_id, |
89
|
|
|
'data': self.box.data, |
90
|
|
|
'callback': self._save_status_callback} |
91
|
|
|
|
92
|
1 |
|
event = KytosEvent(name='kytos.storehouse.update', content=content) |
93
|
1 |
|
self.controller.buffers.app.put(event) |
94
|
|
|
|
95
|
1 |
|
def _save_status_callback(self, _event, data, error): |
96
|
|
|
"""Display the saved network status in the log.""" |
97
|
|
|
if error: |
98
|
|
|
log.error(f'Can\'t update persistence box {data.box_id}.') |
99
|
|
|
|
100
|
|
|
log.info('Network administrative status saved in ' |
101
|
|
|
f'{self.namespace}.{data.box_id}') |
102
|
|
|
|