Passed
Push — master ( 6b9656...a01ca2 )
by Beraldo
06:14 queued 02:15
created

build.storehouse   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 68.33%

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 105
ccs 41
cts 60
cp 0.6833
rs 10
c 0
b 0
f 0
wmc 18

11 Methods

Rating   Name   Duplication   Size   Complexity  
A StoreHouse.create_box() 0 8 1
A StoreHouse.__new__() 0 9 2
A StoreHouse.__init__() 0 7 2
A StoreHouse.get_data() 0 6 2
A StoreHouse.get_stored_box() 0 10 1
A StoreHouse._get_or_create_a_box_from_list_of_boxes() 0 6 2
A StoreHouse._get_box_callback() 0 7 2
A StoreHouse._save_evc_callback() 0 6 2
A StoreHouse.list_stored_boxes() 0 9 1
A StoreHouse._create_box_callback() 0 7 2
A StoreHouse.save_evc() 0 11 1
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
        # pylint: disable=unused-argument
12
        """Make this class a Singleton."""
13 2
        instance = cls.__dict__.get("__instance__")
14 2
        if instance is not None:
15 2
            return instance
16 2
        cls.__instance__ = instance = object.__new__(cls)
17 2
        return instance
18
19 2
    def __init__(self, controller):
20
        """Create a storehouse instance."""
21 2
        self.controller = controller
22 2
        self.namespace = 'kytos.mef_eline.circuits'
23 2
        if 'box' not in self.__dict__:
24 2
            self.box = None
25 2
        self.list_stored_boxes()
26
27 2
    def get_data(self):
28
        """Return the box data."""
29 2
        if not self.box:
30
            return {}
31 2
        self.get_stored_box(self.box.box_id)
32 2
        return self.box.data
33
34 2
    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
        self.controller.buffers.app.put(event)
41
        log.info('Create box from storehouse.')
42
43 2
    def _create_box_callback(self, _event, data, error):
44
        """Execute the callback to handle create_box."""
45
        if error:
46
            log.error(f'Can\'t create box with namespace {self.namespace}')
47
48
        self.box = data
49
        log.info(f'Box {self.box.box_id} was created in {self.namespace}.')
50
51 2
    def list_stored_boxes(self):
52
        """List all boxes using the current namespace."""
53 2
        name = 'kytos.storehouse.list'
54 2
        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 2
        self.controller.buffers.app.put(event)
59 2
        log.debug(f'Bootstraping storehouse box for {self.namespace}.')
60
61 2
    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 2
        content = {'namespace': self.namespace,
71
                   'callback': self._get_box_callback,
72
                   'box_id': box_id,
73
                   'data': {}}
74 2
        name = 'kytos.storehouse.retrieve'
75 2
        event = KytosEvent(name=name, content=content)
76 2
        self.controller.buffers.app.put(event)
77 2
        log.debug(f'Retrieve box with {box_id} from {self.namespace}.')
78
79 2
    def _get_box_callback(self, _event, data, error):
80
        """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
        log.debug(f'Box {self.box.box_id} was load from storehouse.')
86
87 2
    def save_evc(self, evc):
88
        """Save a EVC using the storehouse."""
89 2
        self.box.data[evc.id] = evc.as_dict()
90
91 2
        content = {'namespace': self.namespace,
92
                   'box_id': self.box.box_id,
93
                   'data': self.box.data,
94
                   'callback': self._save_evc_callback}
95
96 2
        event = KytosEvent(name='kytos.storehouse.update', content=content)
97 2
        self.controller.buffers.app.put(event)
98
99 2
    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