blob: eb933232f7ada71eba79a88e97aa53da7b6eeb5e [file] [log] [blame]
Chip Boling32aab302019-01-23 10:50:18 -06001#
2# Copyright 2017 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18OpenOMCI MIB Database API
19"""
20
21import structlog
22from datetime import datetime
23
24CREATED_KEY = 'created'
25MODIFIED_KEY = 'modified'
26MDS_KEY = 'mib_data_sync'
27LAST_SYNC_KEY = 'last_mib_sync'
28VERSION_KEY = 'version'
29DEVICE_ID_KEY = 'device_id'
30CLASS_ID_KEY = 'class_id'
31INSTANCE_ID_KEY = 'instance_id'
32ATTRIBUTES_KEY = 'attributes'
33ME_KEY = 'managed_entities'
34MSG_TYPE_KEY = 'message_types'
35
36
37class DatabaseStateError(Exception):
38 def __init__(self, *args):
39 Exception.__init__(self, *args)
40
41
42class MibDbApi(object):
43 """
44 MIB Database API Base Class
45
46 Derive the ME MIB Database implementation from this API. For an example
47 implementation, look at the mib_db_dict.py implementation
48 """
49 def __init__(self, omci_agent):
50 """
51 Class initializer
52 :param omci_agent: (OpenOMCIAgent) OpenOMCI Agent
53 """
54 self.log = structlog.get_logger()
55 self._omci_agent = omci_agent
56 self._started = False
57
58 now = datetime.utcnow()
59 self._created = now
60 self._modified = now
61
62 def start(self):
63 """
64 Start up/restore the database. For in-memory, will be a nop. For external
65 DB, may need to create the DB and fetch create/modified values
66 """
67 if not self._started:
68 self._started = True
69 # For a derived class that is a persistent DB, Restore DB (connect,
70 # get created/modified times, ....) or something along those lines.
71 # Minimal restore could just be getting ONU device IDs' so they are cached
72 # locally. Maximum restore would be a full in-memory version of database
73 # for fast 'GET' request support.
74 # Remember to restore the '_created' and '_modified' times (above) as well
75 # from the database
76
77 def stop(self):
78 """
79 Start up the database. For in-memory, will be a nop. For external
80 DB, may need to create the DB and fetch create/modified values
81 """
82 if self._started:
83 self._started = False
84
85 @property
86 def active(self):
87 """
88 Is the database active
89 :return: (bool) True if active
90 """
91 return self._started
92
93 @property
94 def created(self):
95 """
96 Date (UTC) that the database was created
97 :return: (datetime) creation date
98 """
99 return self._created
100
101 @property
102 def modified(self):
103 """
104 Date (UTC) that the database last added or removed a device
105 or updated a device's ME information
106 :return: (datetime) last modification date
107 """
108 return self._modified
109
110 def add(self, device_id, overwrite=False):
111 """
112 Add a new ONU to database
113
114 :param device_id: (str) Device ID of ONU to add
115 :param overwrite: (bool) Overwrite existing entry if found.
116
117 :raises KeyError: If device already exists and 'overwrite' is False
118 """
119 raise NotImplementedError('Implement this in your derive class')
120
121 def remove(self, device_id):
122 """
123 Remove an ONU from the database
124
125 :param device_id: (str) Device ID of ONU to remove from database
126 """
127 raise NotImplementedError('Implement this in your derive class')
128
129 def set(self, device_id, class_id, entity_id, attributes):
130 """
131 Set/Create a database value. This should only be called by the MIB synchronizer
132 and its related tasks
133
134 :param device_id: (str) ONU Device ID
135 :param class_id: (int) ME Class ID
136 :param entity_id: (int) ME Entity ID
137 :param attributes: (dict) Attribute dictionary
138
139 :returns: (bool) True if the value was saved to the database. False if the
140 value was identical to the current instance
141
142 :raises KeyError: If device does not exist
143 :raises DatabaseStateError: If the database is not enabled
144 """
145 raise NotImplementedError('Implement this in your derive class')
146
147 def delete(self, device_id, class_id, entity_id):
148 """
149 Delete an entity from the database if it exists
150
151 :param device_id: (str) ONU Device ID
152 :param class_id: (int) ME Class ID
153 :param entity_id: (int) ME Entity ID
154
155 :returns: (bool) True if the instance was found and deleted. False
156 if it did not exist.
157
158 :raises KeyError: If device does not exist
159 :raises DatabaseStateError: If the database is not enabled
160 """
161 raise NotImplementedError('Implement this in your derive class')
162
163 def query(self, device_id, class_id=None, instance_id=None, attributes=None):
164 """
165 Get database information.
166
167 This method can be used to request information from the database to the detailed
168 level requested
169
170 :param device_id: (str) ONU Device ID
171 :param class_id: (int) Managed Entity class ID
172 :param instance_id: (int) Managed Entity instance
173 :param attributes: (list/set or str) Managed Entity instance's attributes
174
175 :return: (dict) The value(s) requested. If class/inst/attribute is
176 not found, an empty dictionary is returned
177 :raises KeyError: If the requested device does not exist
178 :raises DatabaseStateError: If the database is not enabled
179 """
180 raise NotImplementedError('Implement this in your derive class')
181
182 def on_mib_reset(self, device_id):
183 """
184 Reset/clear the database for a specific Device
185
186 :param device_id: (str) ONU Device ID
187 :raises DatabaseStateError: If the database is not enabled
188 """
189 # Your derived class should clear out all MIB data and update the
190 # modified stats appropriately
191 raise NotImplementedError('Implement this in your derive class')
192
193 def save_mib_data_sync(self, device_id, value):
194 """
195 Save the MIB Data Sync to the database in an easy location to access
196
197 :param device_id: (str) ONU Device ID
198 :param value: (int) Value to save
199 """
200 raise NotImplementedError('Implement this in your derive class')
201
202 def get_mib_data_sync(self, device_id):
203 """
204 Get the MIB Data Sync value last saved to the database for a device
205
206 :param device_id: (str) ONU Device ID
207 :return: (int) The Value or None if not found
208 """
209 raise NotImplementedError('Implement this in your derive class')
210
211 def save_last_sync(self, device_id, value):
212 """
213 Save the Last Sync time to the database in an easy location to access
214
215 :param device_id: (str) ONU Device ID
216 :param value: (DateTime) Value to save
217 """
218 raise NotImplementedError('Implement this in your derive class')
219
220 def get_last_sync(self, device_id):
221 """
222 Get the Last SYnc Time saved to the database for a device
223
224 :param device_id: (str) ONU Device ID
225 :return: (int) The Value or None if not found
226 """
227 raise NotImplementedError('Implement this in your derive class')
228
229 def update_supported_managed_entities(self, device_id, managed_entities):
230 """
231 Update the supported OMCI Managed Entities for this device
232
233 :param device_id: (str) ONU Device ID
234 :param managed_entities: (set) Managed Entity class IDs
235 """
236 raise NotImplementedError('Implement this in your derive class')
237
238 def update_supported_message_types(self, device_id, msg_types):
239 """
240 Update the supported OMCI Managed Entities for this device
241
242 :param device_id: (str) ONU Device ID
243 :param msg_types: (set) Message Type values (ints)
244 """
245 raise NotImplementedError('Implement this in your derive class')