Test Failed
Push — master ( 0d7af6...292dca )
by Beraldo
04:07 queued 01:46
created

build.storehouse   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 52.83%

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 94
ccs 28
cts 53
cp 0.5283
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
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.__init__() 0 6 1
A StoreHouse.get_data() 0 6 2
A StoreHouse.create_box() 0 8 1
A StoreHouse.save_evc() 0 11 1
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
    def __init__(self, controller):
10
        """Create a storehouse instance."""
11 1
        self.controller = controller
12 1
        self.namespace = 'kytos.mef_eline.circuits'
13 1
        self.box = None
14 1
        self.list_stored_boxes()
15
16 1
    def get_data(self):
17
        """Return the box data."""
18 1
        if not self.box:
19 1
            return {}
20
        self.get_stored_box(self.box.box_id)
21
        return self.box.data
22
23 1
    def create_box(self):
24
        """Create a new box."""
25
        content = {'namespace': self.namespace,
26
                   'callback': self._create_box_callback,
27
                   'data': {}}
28
        event = KytosEvent(name='kytos.storehouse.create', content=content)
29
        self.controller.buffers.app.put(event)
30
        log.info('Create box from storehouse.')
31
32 1
    def _create_box_callback(self, _event, data, error):
33
        """Execute the callback to handle create_box."""
34
        if error:
35
            log.error(f'Can\'t create box with namespace {self.namespace}')
36
37
        self.box = data
38
        log.info(f'Box {self.box.box_id} was created in {self.namespace}.')
39
40 1
    def list_stored_boxes(self):
41
        """List all boxes using the current namespace."""
42 1
        name = 'kytos.storehouse.list'
43 1
        content = {'namespace': self.namespace,
44
                   'callback': self._get_or_create_a_box_from_list_of_boxes}
45
46 1
        event = KytosEvent(name=name, content=content)
47 1
        self.controller.buffers.app.put(event)
48 1
        log.debug(f'Bootstraping storehouse box for {self.namespace}.')
49
50 1
    def _get_or_create_a_box_from_list_of_boxes(self, _event, data, _error):
51
        """Create a new box or retrieve the stored box."""
52
        if data:
53
            self.get_stored_box(data[0])
54
        else:
55
            self.create_box()
56
57 1
    def get_stored_box(self, box_id):
58
        """Get box from storehouse."""
59
        content = {'namespace': self.namespace,
60
                   'callback': self._get_box_callback,
61
                   'box_id': box_id,
62
                   'data': {}}
63
        name = 'kytos.storehouse.retrieve'
64
        event = KytosEvent(name=name, content=content)
65
        self.controller.buffers.app.put(event)
66
        log.debug(f'Retrieve box with {box_id} from {self.namespace}.')
67
68 1
    def _get_box_callback(self, _event, data, error):
69
        """Handle get_box method saving the box or logging with the error."""
70
        if error:
71
            log.error(f'Box {data.box_id} not found in {self.namespace}.')
72
73
        self.box = data
74
        log.debug(f'Box {self.box.box_id} was load from storehouse.')
75
76 1
    def save_evc(self, evc):
77
        """Save a EVC using the storehouse."""
78 1
        self.box.data[evc.id] = evc.as_dict()
79
80 1
        content = {'namespace': self.namespace,
81
                   'box_id': self.box.box_id,
82
                   'data': self.box.data,
83
                   'callback': self._save_evc_callback}
84
85 1
        event = KytosEvent(name='kytos.storehouse.update', content=content)
86 1
        self.controller.buffers.app.put(event)
87
88 1
    def _save_evc_callback(self, _event, data, error):
89
        """Display the save EVC result in the log."""
90
        if error:
91
            log.error(f'Can\'t update the {self.box.box_id}')
92
93
        log.info(f'Box {data.box_id} was updated.')
94