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