| 1 |  |  | """Module to handle the storehouse.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 | 1 |  | import time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 | 1 |  | from kytos.core import log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 | 1 |  | from kytos.core.events import KytosEvent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 | 1 |  | from napps.kytos.flow_manager import settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 | 1 |  | DEFAULT_WAIT_PERSISTENCE_BOX_TIMER = 0.1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 | 1 |  | class StoreHouse: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     """Class to handle storehouse.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 1 |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     def __new__(cls, *args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         # pylint: disable=unused-argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         """Make this class a Singleton.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 1 |  |         instance = cls.__dict__.get("__instance__") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 1 |  |         if instance is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 1 |  |             return instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |         cls.__instance__ = instance = object.__new__(cls) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 1 |  |         return instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 1 |  |     def __init__(self, controller): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         """Create a storehouse client instance.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |         self.controller = controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 1 |  |         self.namespace = 'kytos.flow.persistence' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |         self.wait_persistence_box = getattr(settings, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                                             'WAIT_PERSISTENCE_BOX_TIMER', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                                             DEFAULT_WAIT_PERSISTENCE_BOX_TIMER) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         if 'box' not in self.__dict__: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |             self.box = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |         self.list_stored_boxes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |     def get_data(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         """Return the persistence box data.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         # Wait retrieve or create box in storehouse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |         while not self.box: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             time.sleep(self.wait_persistence_box) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |         return self.box.data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |     def create_box(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         """Create a persistence box to store administrative changes.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         content = {'namespace': self.namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                    'callback': self._create_box_callback, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                    'data': {}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         event = KytosEvent(name='kytos.storehouse.create', content=content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         self.controller.buffers.app.put(event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |     def _create_box_callback(self, _event, data, error): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         """Execute the callback to handle create_box.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if error: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             log.error(f'Can\'t create persistence' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                       f'box with namespace {self.namespace}') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         self.box = data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |     def list_stored_boxes(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         """List all persistence box stored in storehouse.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         name = 'kytos.storehouse.list' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         content = {'namespace': self.namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                    'callback': self._get_or_create_a_box_from_list_of_boxes} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |         event = KytosEvent(name=name, content=content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         self.controller.buffers.app.put(event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |     def _get_or_create_a_box_from_list_of_boxes(self, _event, data, _error): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         """Create a persistence box or retrieve the stored box.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |         if data: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |             self.get_stored_box(data[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |             self.create_box() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |     def get_stored_box(self, box_id): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         """Get persistence box from storehouse.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         content = {'namespace': self.namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                    'callback': self._get_box_callback, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                    'box_id': box_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                    'data': {}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         name = 'kytos.storehouse.retrieve' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |         event = KytosEvent(name=name, content=content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         self.controller.buffers.app.put(event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |     def _get_box_callback(self, _event, data, error): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         """Handle get_box method saving the box or logging with the error.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if error: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             log.error('Persistence box not found.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         self.box = data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |     def save_flow(self, flows): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         """Save flows in storehouse.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         self.box.data[flows['id']] = flows | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         content = {'namespace': self.namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                    'box_id': self.box.box_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                    'data': self.box.data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                    'callback': self._save_flow_callback} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |         event = KytosEvent(name='kytos.storehouse.update', content=content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |         self.controller.buffers.app.put(event) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 103 | 1 |  |     def _save_flow_callback(self, _event, data, error): | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         """Display stored flow.""" | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         if error: | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |             log.error(f'Can\'t update persistence box {data.box_id}.') | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |         log.info(f'Flow saved in {self.namespace}.{data.box_id}') | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |