Completed
Push — master ( 0ff074...4feb48 )
by Humberto
02:38 queued 11s
created

build.storehouse   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 81.67%

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 123
ccs 49
cts 60
cp 0.8167
rs 10
c 0
b 0
f 0
wmc 18

11 Methods

Rating   Name   Duplication   Size   Complexity  
A StoreHouse.__init__() 0 7 2
A StoreHouse._save_status_callback() 0 6 2
A StoreHouse._get_box_callback() 0 7 2
A StoreHouse.list_stored_boxes() 0 9 1
A StoreHouse._create_box_callback() 0 7 2
A StoreHouse.__new__() 0 9 2
A StoreHouse.get_data() 0 6 2
A StoreHouse.get_stored_box() 0 10 1
A StoreHouse.create_box() 0 8 1
A StoreHouse.save_status() 0 11 1
A StoreHouse._get_or_create_a_box_from_list_of_boxes() 0 6 2
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 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
    # @listen('kytos/core.shutdown')
28
    # def delete_data_store(self):
29
    #     """delete status stored"""
30
    #     content = {'namespace': self.namespace,
31
    #                'box_id' : self.box.box_id,
32
    #                'callback': self._delete_box_callback,
33
    #                'data': {}}
34
    #     event = KytosEvent(name='kytos.storehouse.delete', content=content)
35
    #     self.controller.buffers.app.put(event)
36
    #     log.info('Delete Status box from storehouse.')
37
38
    # def _delete_box_callback(self, _event, data, error):
39
    #     """Execute the callback to handle delete_box."""
40
    #     if error:
41
    #         log.error(f'Can\'t delete box with namespace {self.namespace}')
42
    #
43
    #     log.info('Status box has been deleted.')
44
45 1
    def get_data(self):
46
        """Return the box data."""
47 1
        if not self.box:
48 1
            return {}
49 1
        self.get_stored_box(self.box.box_id)
50 1
        return self.box.data
51
52 1
    def create_box(self):
53
        """Create a new box."""
54 1
        content = {'namespace': self.namespace,
55
                   'callback': self._create_box_callback,
56
                   'data': {}}
57 1
        event = KytosEvent(name='kytos.storehouse.create', content=content)
58 1
        self.controller.buffers.app.put(event)
59 1
        log.info('Create box from storehouse.')
60
61 1
    def _create_box_callback(self, _event, data, error):
62
        """Execute the callback to handle create_box."""
63
        if error:
64
            log.error(f'Can\'t create box with namespace {self.namespace}')
65
66
        self.box = data
67
        log.info(f'Box {self.box.box_id} was created in {self.namespace}.')
68
69 1
    def list_stored_boxes(self):
70
        """List all boxes using the current namespace."""
71 1
        name = 'kytos.storehouse.list'
72 1
        content = {'namespace': self.namespace,
73
                   'callback': self._get_or_create_a_box_from_list_of_boxes}
74
75 1
        event = KytosEvent(name=name, content=content)
76 1
        self.controller.buffers.app.put(event)
77 1
        log.debug(f'Bootstrapping storehouse box for {self.namespace}.')
78
79 1
    def _get_or_create_a_box_from_list_of_boxes(self, _event, data, _error):
80
        """Create a new box or retrieve the stored box."""
81 1
        if data:
82 1
            self.get_stored_box(data[0])
83
        else:
84 1
            self.create_box()
85
86 1
    def get_stored_box(self, box_id):
87
        """Get box from storehouse."""
88 1
        content = {'namespace': self.namespace,
89
                   'callback': self._get_box_callback,
90
                   'box_id': box_id,
91
                   'data': {}}
92 1
        name = 'kytos.storehouse.retrieve'
93 1
        event = KytosEvent(name=name, content=content)
94 1
        self.controller.buffers.app.put(event)
95 1
        log.debug(f'Retrieve box with {box_id} from {self.namespace}.')
96
97 1
    def _get_box_callback(self, _event, data, error):
98
        """Handle get_box method saving the box or logging with the error."""
99
        if error:
100
            log.error(f'Box {data.box_id} not found in {self.namespace}.')
101
102
        self.box = data
103
        log.debug(f'Box {self.box.box_id} was loaded from storehouse.')
104
105 1
    def save_status(self, status):
106
        """Save the network administrative status using storehouse."""
107 1
        self.box.data[status.get('id')] = status
108
109 1
        content = {'namespace': self.namespace,
110
                   'box_id': self.box.box_id,
111
                   'data': self.box.data,
112
                   'callback': self._save_status_callback}
113
114 1
        event = KytosEvent(name='kytos.storehouse.update', content=content)
115 1
        self.controller.buffers.app.put(event)
116
117 1
    def _save_status_callback(self, _event, data, error):
118
        """Display the saved network status in the log."""
119
        if error:
120
            log.error(f'Can\'t update box {self.box.box_id}')
121
122
        log.info(f'Box {data.box_id} was updated.')
123