Test Failed
Pull Request — master (#127)
by Antonio
05:24
created

build.storehouse.StoreHouse.__new__()   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
"""Module to handle the storehouse."""
2 2
from kytos.core import log
3 2
from kytos.core.events import KytosEvent
4
5
6 2
class StoreHouse:
7
    """Class to handle storehouse."""
8
9 2
    @classmethod
10
    def __new__(cls, *args, **kwargs):
11 2
        """Make this class a Singleton"""
12 2
13 2
        it = cls.__dict__.get("__it__")
14 2
        if it is not None:
15
            return it
16 2
        cls.__it__ = it = object.__new__(cls)
17
        return it
18 2
19 2
    def __init__(self, controller):
20
        """Create a storehouse instance."""
21
        self.controller = controller
22
        self.namespace = 'kytos.mef_eline.circuits'
23 2
        if 'box' not in self.__dict__:
24
            self.box = None
25
        self.list_stored_boxes()
26
27
    def get_data(self):
28
        """Return the box data."""
29
        if not self.box:
30
            return {}
31
        self.get_stored_box(self.box.box_id)
32 2
        return self.box.data
33
34
    def create_box(self):
35
        """Create a new box."""
36
        content = {'namespace': self.namespace,
37
                   'callback': self._create_box_callback,
38
                   'data': {}}
39
        event = KytosEvent(name='kytos.storehouse.create', content=content)
40 2
        self.controller.buffers.app.put(event)
41
        log.info('Create box from storehouse.')
42 2
43 2
    def _create_box_callback(self, _event, data, error):
44
        """Execute the callback to handle create_box."""
45
        if error:
46 2
            log.error(f'Can\'t create box with namespace {self.namespace}')
47 2
48 2
        self.box = data
49
        log.info(f'Box {self.box.box_id} was created in {self.namespace}.')
50 2
51
    def list_stored_boxes(self):
52
        """List all boxes using the current namespace."""
53
        name = 'kytos.storehouse.list'
54
        content = {'namespace': self.namespace,
55
                   'callback': self._get_or_create_a_box_from_list_of_boxes}
56
57 2
        event = KytosEvent(name=name, content=content)
58
        self.controller.buffers.app.put(event)
59
        log.debug(f'Bootstraping storehouse box for {self.namespace}.')
60
61
    def _get_or_create_a_box_from_list_of_boxes(self, _event, data, _error):
62
        """Create a new box or retrieve the stored box."""
63
        if data:
64
            self.get_stored_box(data[0])
65
        else:
66
            self.create_box()
67
68 2
    def get_stored_box(self, box_id):
69
        """Get box from storehouse."""
70
        content = {'namespace': self.namespace,
71
                   'callback': self._get_box_callback,
72
                   'box_id': box_id,
73
                   'data': {}}
74
        name = 'kytos.storehouse.retrieve'
75
        event = KytosEvent(name=name, content=content)
76 2
        self.controller.buffers.app.put(event)
77
        log.debug(f'Retrieve box with {box_id} from {self.namespace}.')
78 2
79
    def _get_box_callback(self, _event, data, error):
80 2
        """Handle get_box method saving the box or logging with the error."""
81
        if error:
82
            log.error(f'Box {data.box_id} not found in {self.namespace}.')
83
84
        self.box = data
85 2
        log.debug(f'Box {self.box.box_id} was load from storehouse.')
86 2
87
    def save_evc(self, evc):
88 2
        """Save a EVC using the storehouse."""
89
        self.box.data[evc.id] = evc.as_dict()
90
91
        content = {'namespace': self.namespace,
92
                   'box_id': self.box.box_id,
93
                   'data': self.box.data,
94
                   'callback': self._save_evc_callback}
95
96
        event = KytosEvent(name='kytos.storehouse.update', content=content)
97
        self.controller.buffers.app.put(event)
98
99
    def _save_evc_callback(self, _event, data, error):
100
        """Display the save EVC result in the log."""
101
        if error:
102
            log.error(f'Can\'t update the {self.box.box_id}')
103
104
        log.info(f'Box {data.box_id} was updated.')
105