| 1 |  |  | """Module to handle the storehouse.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from kytos.core import log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from kytos.core.events import KytosEvent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class StoreHouse: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     """Class to handle storehouse.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     def __new__(cls, *args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         # pylint: disable=unused-argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         """Make this class a Singleton.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         instance = cls.__dict__.get("__instance__") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         if instance is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |             return instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         cls.__instance__ = instance = object.__new__(cls) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         return instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     def __init__(self, controller): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         """Create a storehouse instance.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         self.controller = controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         self.namespace = 'kytos.topology.status' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         self.list_stored_boxes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         if 'box' not in self.__dict__: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             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 |  |  |     def get_data(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         """Return the box data.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if not self.box: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             return {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         self.get_stored_box(self.box.box_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return self.box.data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     def create_box(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         """Create a new box.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         content = {'namespace': self.namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                    'callback': self._create_box_callback, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                    'data': {}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         event = KytosEvent(name='kytos.storehouse.create', content=content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         self.controller.buffers.app.put(event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         log.info('Create box from storehouse.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     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 |  |  |     def list_stored_boxes(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         """List all boxes using the current namespace.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         name = 'kytos.storehouse.list' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         content = {'namespace': self.namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                    'callback': self._get_or_create_a_box_from_list_of_boxes} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         event = KytosEvent(name=name, content=content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         self.controller.buffers.app.put(event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         log.debug(f'Bootstrapping storehouse box for {self.namespace}.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     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 |  |  |         if data: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             self.get_stored_box(data[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             self.create_box() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     def get_stored_box(self, box_id): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         """Get box from storehouse.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         content = {'namespace': self.namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                    'callback': self._get_box_callback, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                    'box_id': box_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                    'data': {}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         name = 'kytos.storehouse.retrieve' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         event = KytosEvent(name=name, content=content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         self.controller.buffers.app.put(event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         log.debug(f'Retrieve box with {box_id} from {self.namespace}.') | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     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 |  |  |     def save_status(self, status): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         """Save a status network using the storehouse.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         self.box.data[status.get('id')] = status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         content = {'namespace': self.namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                    'box_id': self.box.box_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                    'data': self.box.data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                    'callback': self._save_status_callback} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         event = KytosEvent(name='kytos.storehouse.update', content=content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         self.controller.buffers.app.put(event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     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 |  |  |  |