blob: 2f7f26dbcb16e82346c92b353c3a155584be4c5c [file] [log] [blame]
Chip Boling67b674a2019-02-08 11:42: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#
16from unittest import main, TestCase
17
18from pyvoltha.adapters.extensions.omci.omci_entities import *
19from pyvoltha.adapters.extensions.omci.database.mib_db_dict import *
20from pyvoltha.adapters.extensions.omci.database.mib_db_api import MODIFIED_KEY, CREATED_KEY,\
21 DEVICE_ID_KEY, MDS_KEY, LAST_SYNC_KEY
22from mock.mock_adapter_agent import MockAdapterAgent, MockDevice
23from nose.tools import raises, assert_raises
24import time
25
26_DEVICE_ID = 'br-549'
27
28
29class TestOmciMibDbDict(TestCase):
30
31 def setUp(self):
32 self.adapter_agent = MockAdapterAgent()
33 self.adapter_agent.add_device(MockDevice(_DEVICE_ID)) # For Entity class lookups
34 self.db = MibDbVolatileDict(self.adapter_agent)
35
36 def tearDown(self):
37 self.db.stop()
38
39 def test_start_stop(self):
40 # Simple start stop
41 self.assertFalse(self.db.active)
42 self.db.start()
43 self.assertTrue(self.db.active)
44 self.db.stop()
45 self.assertFalse(self.db.active)
46
47 # Start after start still okay
48 self.db.start()
49 self.db.start()
50 self.assertTrue(self.db.active)
51
52 self.db.stop()
53 self.db.stop()
54 self.assertFalse(self.db.active)
55
56 @raises(DatabaseStateError)
57 def test_bad_state_add(self):
58 self.db.add(_DEVICE_ID)
59
60 @raises(DatabaseStateError)
61 def test_bad_state_remove(self):
62 self.db.remove(_DEVICE_ID)
63
64 @raises(DatabaseStateError)
65 def test_bad_state_query_1(self):
66 self.db.query(_DEVICE_ID, 0)
67
68 @raises(DatabaseStateError)
69 def test_bad_state_query_2(self):
70 self.db.query(_DEVICE_ID, 0, 0)
71
72 @raises(DatabaseStateError)
73 def test_bad_state_query_3(self):
74 self.db.query(_DEVICE_ID, 0, 0, 'test')
75
76 @raises(DatabaseStateError)
77 def test_bad_state_set(self):
78 self.db.set(_DEVICE_ID, 0, 0, {'test': 123})
79
80 @raises(DatabaseStateError)
81 def test_bad_state_delete(self):
82 self.db.delete(_DEVICE_ID, 0, 0)
83
84 @raises(KeyError)
85 def test_no_device_query(self):
86 self.db.start()
87 self.db.query(_DEVICE_ID)
88
89 def test_no_device_last_sync(self):
90 self.db.start()
91 # Returns None, not a KeyError
92 value = self.db.get_last_sync(_DEVICE_ID)
93 self.assertIsNone(value)
94
95 def test_no_device_mds(self):
96 self.db.start()
97 # Returns None, not a KeyError
98 value = self.db.get_mib_data_sync(_DEVICE_ID)
99 self.assertIsNone(value)
100
101 @raises(KeyError)
102 def test_no_device_save_last_sync(self):
103 self.db.start()
104 self.db.save_last_sync(_DEVICE_ID, datetime.utcnow())
105
106 @raises(KeyError)
107 def test_no_device_save_mds(self):
108 self.db.start()
109 self.db.save_mib_data_sync(_DEVICE_ID, 123)
110
111 def test_param_types(self):
112 self.db.start()
113 assert_raises(TypeError, self.db.add, 123)
114 assert_raises(TypeError, self.db.remove, 123)
115 assert_raises(TypeError, self.db.query, 123)
116
117 assert_raises(TypeError, self.db.get_mib_data_sync, 123)
118 assert_raises(TypeError, self.db.save_mib_data_sync, 123, 0)
119 assert_raises(TypeError, self.db.save_mib_data_sync, _DEVICE_ID, 'zero')
120
121 assert_raises(TypeError, self.db.get_last_sync, 123)
122 assert_raises(TypeError, self.db.save_last_sync, 123, datetime.utcnow())
123 assert_raises(TypeError, self.db.save_last_sync, _DEVICE_ID, 'bad-date')
124
125 assert_raises(TypeError, self.db.set, 123, 0, 0, {'test': 0})
126 assert_raises(TypeError, self.db.set, None, 0, 0, {'test': 0})
127 assert_raises(ValueError, self.db.set, _DEVICE_ID, None, 0, {'test': 0})
128 assert_raises(ValueError, self.db.set, _DEVICE_ID, 0, None, {'test': 0})
129 assert_raises(TypeError, self.db.set, _DEVICE_ID, 0, 0, None)
130 assert_raises(TypeError, self.db.set, _DEVICE_ID, 0, 0, 'not-a-dict')
131
132 assert_raises(ValueError, self.db.set, _DEVICE_ID, -1, 0, {'test': 0})
133 assert_raises(ValueError, self.db.set, _DEVICE_ID, 0x10000, 0, {'test': 0})
134 assert_raises(ValueError, self.db.set, _DEVICE_ID, 0, -1, {'test': 0})
135 assert_raises(ValueError, self.db.set, _DEVICE_ID, 0, 0x10000, {'test': 0})
136
137 assert_raises(TypeError, self.db.delete, 123, 0, 0)
138 assert_raises(ValueError, self.db.delete, _DEVICE_ID, -1, 0)
139 assert_raises(ValueError, self.db.delete, _DEVICE_ID, 0x10000, 0)
140 assert_raises(ValueError, self.db.delete, _DEVICE_ID, 0, -1)
141 assert_raises(ValueError, self.db.delete, _DEVICE_ID, 0, 0x10000)
142
143 def test_add_remove_device(self):
144 self.db.start()
145
146 # Remove of non-existent device is not an error
147 assert_raises(KeyError, self.db.query, _DEVICE_ID)
148 self.db.remove(_DEVICE_ID)
149
150 start_time = datetime.utcnow()
151 self.db.add(_DEVICE_ID)
152 dev_data = self.db.query(_DEVICE_ID)
153 end_time = datetime.utcnow()
154
155 self.assertEqual(dev_data[DEVICE_ID_KEY], _DEVICE_ID)
156 self.assertEquals(dev_data[MDS_KEY], 0)
157 self.assertIsNone(dev_data[LAST_SYNC_KEY])
158 self.assertEqual(dev_data[VERSION_KEY], MibDbVolatileDict.CURRENT_VERSION)
159
160 # Remove it
161 self.db.remove(_DEVICE_ID)
162 assert_raises(KeyError, self.db.query, _DEVICE_ID)
163
164 # Remove of non-existant dev okay
165 self.db.remove(_DEVICE_ID +'abcd')
166
167 # Overwrite tests
168 self.db.add(_DEVICE_ID)
169 assert_raises(KeyError, self.db.add, _DEVICE_ID)
170 self.db.add(_DEVICE_ID, overwrite=True) # This is okay
171
172 def test_mib_data_sync(self):
173 self.db.start()
174 self.db.add(_DEVICE_ID)
175 self.assertEquals(self.db.get_mib_data_sync(_DEVICE_ID), 0)
176
177 self.db.save_mib_data_sync(_DEVICE_ID, 100)
178 self.assertEqual(self.db.get_mib_data_sync(_DEVICE_ID), 100)
179
180 assert_raises(ValueError, self.db.save_mib_data_sync, _DEVICE_ID, -1)
181 assert_raises(ValueError, self.db.save_mib_data_sync, _DEVICE_ID, 256)
182
183 def test_last_sync(self):
184 self.db.start()
185 self.assertIsNone(self.db.get_last_sync(_DEVICE_ID))
186
187 self.db.add(_DEVICE_ID)
188 self.assertIsNone(self.db.get_last_sync(_DEVICE_ID))
189
190 now = datetime.utcnow()
191
192 self.db.save_last_sync(_DEVICE_ID, now)
193 self.assertEqual(self.db.get_last_sync(_DEVICE_ID), now)
194
195 assert_raises(TypeError, self.db.save_last_sync, _DEVICE_ID, 'hello')
196
197 def test_set_and_query(self):
198 self.db.start()
199 self.db.add(_DEVICE_ID) # Base device DB created here
200 time.sleep(0.1)
201
202 class_id = OntG.class_id
203 inst_id = 0
204 attributes = {'vendor_id': 'ABCD'}
205
206 start_time = datetime.utcnow()
207 set_occurred = self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
208 self.assertTrue(set_occurred)
209 end_time = datetime.utcnow()
210
211 dev_data = self.db.query(_DEVICE_ID)
212 self.assertEqual(dev_data[DEVICE_ID_KEY], _DEVICE_ID)
213
214 dev_classes = [v for k, v in dev_data.items() if isinstance(k, int)]
215
216 self.assertEqual(len(dev_classes), 1)
217 class_data = dev_classes[0]
218
219 self.assertEqual(class_data[CLASS_ID_KEY], class_id)
220
221 class_insts = [v for k, v in class_data.items() if isinstance(k, int)]
222
223 self.assertEqual(len(class_insts), 1)
224 inst_data = class_insts[0]
225
226 self.assertEqual(inst_data[INSTANCE_ID_KEY], inst_id)
227 self.assertGreaterEqual(inst_data[MODIFIED_KEY], start_time)
228 self.assertLessEqual(inst_data[MODIFIED_KEY], end_time)
229 self.assertLessEqual(inst_data[CREATED_KEY], inst_data[MODIFIED_KEY])
230
231 inst_attributes = inst_data[ATTRIBUTES_KEY]
232 self.assertEqual(len(inst_attributes), 1)
233
234 self.assertTrue('vendor_id' in inst_attributes)
235 self.assertEqual(inst_attributes['vendor_id'], attributes['vendor_id'])
236
237 ########################################
238 # Query with device and class. Should be same as from full device query
239 cls_2_data = self.db.query(_DEVICE_ID, class_id)
240
241 self.assertEqual(class_data[CLASS_ID_KEY], cls_2_data[CLASS_ID_KEY])
242
243 cl2_insts = {k:v for k, v in cls_2_data.items() if isinstance(k, int)}
244 self.assertEqual(len(cl2_insts), len(class_insts))
245
246 # Bad class id query
247 cls_no_data = self.db.query(_DEVICE_ID, class_id + 1)
248 self.assertTrue(isinstance(cls_no_data, dict))
249 self.assertEqual(len(cls_no_data), 0)
250
251 ########################################
252 # Query with device, class, instance
253 inst_2_data = self.db.query(_DEVICE_ID, class_id, inst_id)
254
255 self.assertEqual(inst_data[INSTANCE_ID_KEY], inst_2_data[INSTANCE_ID_KEY])
256 self.assertEqual(inst_data[MODIFIED_KEY], inst_2_data[MODIFIED_KEY])
257 self.assertEqual(inst_data[CREATED_KEY], inst_2_data[CREATED_KEY])
258
259 inst2_attr = inst_2_data[ATTRIBUTES_KEY]
260 self.assertEqual(len(inst2_attr), len(inst_attributes))
261
262 # Bad instance id query
263 inst_no_data = self.db.query(_DEVICE_ID, class_id, inst_id + 100)
264 self.assertTrue(isinstance(inst_no_data, dict))
265 self.assertEqual(len(inst_no_data), 0)
266
267 ########################################
268 # Attribute queries
269 attr_2_data = self.db.query(_DEVICE_ID, class_id, inst_id, 'vendor_id')
270 self.assertEqual(attr_2_data['vendor_id'], attributes['vendor_id'])
271
272 attr_3_data = self.db.query(_DEVICE_ID, class_id, inst_id, ['vendor_id'])
273 self.assertEqual(attr_3_data['vendor_id'], attributes['vendor_id'])
274
275 attr_4_data = self.db.query(_DEVICE_ID, class_id, inst_id, {'vendor_id'})
276 self.assertEqual(attr_4_data['vendor_id'], attributes['vendor_id'])
277
278 attr_no_data = self.db.query(_DEVICE_ID, class_id, inst_id, 'no_such_thing')
279 self.assertTrue(isinstance(attr_no_data, dict))
280 self.assertEqual(len(attr_no_data), 0)
281
282 # Set to same value does not change modified data. The modified is
283 # at the instance level
284
285 class_id = OntG.class_id
286 inst_id = 0
287 attributes = {'vendor_id': 'ABCD'}
288 set_occurred = self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
289 self.assertFalse(set_occurred)
290
291 inst_3_data = self.db.query(_DEVICE_ID, class_id, inst_id)
292 self.assertEqual(inst_data[MODIFIED_KEY], inst_3_data[MODIFIED_KEY])
293 self.assertEqual(inst_data[CREATED_KEY], inst_3_data[CREATED_KEY])
294
295 # But set to new value does
296 time.sleep(0.1)
297 attributes = {'vendor_id': 'WXYZ'}
298 set_occurred = self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
299 self.assertTrue(set_occurred)
300
301 inst_4_data = self.db.query(_DEVICE_ID, class_id, inst_id)
302 self.assertLess(inst_3_data[MODIFIED_KEY], inst_4_data[MODIFIED_KEY])
303 self.assertEqual(inst_3_data[CREATED_KEY], inst_4_data[CREATED_KEY])
304
305 def test_delete_instances(self):
306 self.db.start()
307 self.db.add(_DEVICE_ID)
308 create_time = datetime.utcnow()
309
310 class_id = GalEthernetProfile.class_id
311 inst_id_1 = 0x100
312 inst_id_2 = 0x200
313 attributes = {'max_gem_payload_size': 1500}
314
315 self.db.set(_DEVICE_ID, class_id, inst_id_1, attributes)
316 self.db.set(_DEVICE_ID, class_id, inst_id_2, attributes)
317 set_time = datetime.utcnow()
318 time.sleep(0.1)
319
320 dev_data = self.db.query(_DEVICE_ID)
321 cls_data = self.db.query(_DEVICE_ID, class_id)
322 inst_data = {k: v for k, v in cls_data.items() if isinstance(k, int)}
323 self.assertEqual(len(inst_data), 2)
324
325 self.assertLessEqual(dev_data[CREATED_KEY], create_time)
326 self.assertLessEqual(self.db.created, create_time)
327
328 # Delete one instance
329 time.sleep(0.1)
330 del_time = datetime.utcnow()
331 result = self.db.delete(_DEVICE_ID, class_id, inst_id_1)
332 self.assertTrue(result) # True returned if a del actually happened
333
334 dev_data = self.db.query(_DEVICE_ID)
335 cls_data = self.db.query(_DEVICE_ID, class_id)
336 inst_data = {k: v for k, v in cls_data.items() if isinstance(k, int)}
337 self.assertEqual(len(inst_data), 1)
338
339 self.assertLessEqual(dev_data[CREATED_KEY], create_time)
340 self.assertLessEqual(self.db.created, create_time)
341
342 # Delete remaining instance
343 time.sleep(0.1)
344 result = self.db.delete(_DEVICE_ID, class_id, inst_id_2)
345 self.assertTrue(result) # True returned if a del actually happened
346
347 dev_data = self.db.query(_DEVICE_ID)
348 cls_data = {k: v for k, v in dev_data.items() if isinstance(k, int)}
349 self.assertEqual(len(cls_data), 0)
350 self.assertLessEqual(dev_data[CREATED_KEY], create_time)
351
352 # Delete returns false if not instance
353 self.assertFalse(self.db.delete(_DEVICE_ID, class_id, inst_id_1))
354 self.assertFalse(self.db.delete(_DEVICE_ID, class_id, inst_id_2))
355
356 def test_on_mib_reset_listener(self):
357 self.db.start()
358 self.db.add(_DEVICE_ID)
359 time.sleep(0.1)
360
361 class_id = OntG.class_id
362 inst_id = 0
363 attributes = {'vendor_id': 'ABCD'}
364
365 set_time = datetime.utcnow()
366 self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
367
368 time.sleep(0.1)
369 self.db.on_mib_reset(_DEVICE_ID)
370
371 dev_data = self.db.query(_DEVICE_ID)
372 self.assertEqual(dev_data[DEVICE_ID_KEY], _DEVICE_ID)
373 self.assertLessEqual(dev_data[CREATED_KEY], set_time)
374 self.assertLessEqual(self.db.created, set_time)
375
376 self.assertFalse(any(isinstance(cls, int) for cls in dev_data.iterkeys()))
377
378 def test_str_field_serialization(self):
379 self.db.start()
380 self.db.add(_DEVICE_ID)
381
382 class_id = OltG.class_id
383 inst_id = 0
384 attributes = {
385 'olt_vendor_id': 'ABCD', # StrFixedLenField(4)
386 }
387 self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
388 data = self.db.query(_DEVICE_ID, class_id, inst_id, attributes.keys())
389 self.assertTrue(all(isinstance(data[k], basestring) for k in attributes.keys()))
390 self.assertTrue(all(data[k] == attributes[k] for k in attributes.keys()))
391
392 def test_mac_address_ip_field_serialization(self):
393 self.db.start()
394 self.db.add(_DEVICE_ID)
395
396 class_id = IpHostConfigData.class_id
397 inst_id = 0
398 attributes = {
399 'mac_address': '00:01:02:03:04:05', # MACField
400 'ip_address': '1.2.3.4', # IPField
401 }
402 self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
403 data = self.db.query(_DEVICE_ID, class_id, inst_id, attributes.keys())
404 self.assertTrue(all(isinstance(data[k], basestring) for k in attributes.keys()))
405 self.assertTrue(all(data[k] == attributes[k] for k in attributes.keys()))
406
407 def test_byte_and_short_field_serialization(self):
408 self.db.start()
409 self.db.add(_DEVICE_ID)
410
411 class_id = UniG.class_id
412 inst_id = 0
413 attributes = {
414 'administrative_state': int(1), # ByteField
415 'non_omci_management_identifier': int(12345) # IPField
416 }
417 self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
418 data = self.db.query(_DEVICE_ID, class_id, inst_id, attributes.keys())
419 self.assertTrue(all(isinstance(data[k], type(attributes[k])) for k in attributes.keys()))
420 self.assertTrue(all(data[k] == attributes[k] for k in attributes.keys()))
421
422 def test_int_field_serialization(self):
423 self.db.start()
424 self.db.add(_DEVICE_ID)
425
426 class_id = PriorityQueueG.class_id
427 inst_id = 0
428 attributes = {
429 'related_port': int(1234567) # IntField
430 }
431 self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
432 data = self.db.query(_DEVICE_ID, class_id, inst_id, attributes.keys())
433 self.assertTrue(all(isinstance(data[k], type(attributes[k])) for k in attributes.keys()))
434 self.assertTrue(all(data[k] == attributes[k] for k in attributes.keys()))
435
436 def test_long_field_serialization(self):
437 self.db.start()
438 self.db.add(_DEVICE_ID)
439
440 class_id = PriorityQueueG.class_id
441 inst_id = 0
442 attributes = {
443 'packet_drop_queue_thresholds': int(0x1234) # LongField
444 }
445 self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
446 data = self.db.query(_DEVICE_ID, class_id, inst_id, attributes.keys())
447 self.assertTrue(all(isinstance(data[k], type(attributes[k])) for k in attributes.keys()))
448 self.assertTrue(all(data[k] == attributes[k] for k in attributes.keys()))
449
450 def test_bit_field_serialization(self):
451 self.db.start()
452 self.db.add(_DEVICE_ID)
453
454 class_id = OntG.class_id
455 inst_id = 0
456 attributes = {
457 'extended_tc_layer_options': long(0x1234), # BitField(16)
458 }
459 self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
460 data = self.db.query(_DEVICE_ID, class_id, inst_id, attributes.keys())
461 self.assertTrue(all(isinstance(data[k], type(attributes[k])) for k in attributes.keys()))
462 self.assertTrue(all(data[k] == attributes[k] for k in attributes.keys()))
463
464 def test_list_field_serialization(self):
465 self.db.start()
466 self.db.add(_DEVICE_ID)
467
468 class_id = VlanTaggingFilterData.class_id
469 inst_id = 0
470 vlan_filter_list = [0] * 12
471 vlan_filter_list[0] = 0x1234
472
473 attributes = {
474 'vlan_filter_list': vlan_filter_list, # FieldListField
475 'forward_operation': 0,
476 'number_of_entries': 1
477 }
478 self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
479 data = self.db.query(_DEVICE_ID, class_id, inst_id, attributes.keys())
480 self.assertTrue(all(isinstance(data[k], type(attributes[k])) for k in attributes.keys()))
481 self.assertTrue(all(data[k] == attributes[k] for k in attributes.keys()))
482
483 def test_complex_json_serialization(self):
484 self.db.start()
485 self.db.add(_DEVICE_ID)
486
487 class_id = ExtendedVlanTaggingOperationConfigurationData.class_id
488 inst_id = 0x202
489 table_data = VlanTaggingOperation(
490 filter_outer_priority=15,
491 filter_inner_priority=8,
492 filter_inner_vid=1024,
493 filter_inner_tpid_de=5,
494 filter_ether_type=0,
495 treatment_tags_to_remove=1,
496 pad3=2,
497 treatment_outer_priority=15,
498 treatment_inner_priority=8,
499 treatment_inner_vid=1024,
500 treatment_inner_tpid_de=4
501 )
502 attributes = dict(
503 received_frame_vlan_tagging_operation_table=table_data
504 )
505 self.db.set(_DEVICE_ID, class_id, inst_id, attributes)
506
507 data = self.db.query(_DEVICE_ID, class_id, inst_id, attributes.keys())
508 table_as_dict = json.loads(table_data.to_json())
509
510 self.assertTrue(all(isinstance(data['received_frame_vlan_tagging_operation_table'][0].fields[k],
511 type(attributes['received_frame_vlan_tagging_operation_table'].fields[k]))
512 for k in attributes['received_frame_vlan_tagging_operation_table'].fields.keys()))
513 self.assertTrue(all(data['received_frame_vlan_tagging_operation_table'][0].fields[k] ==
514 attributes['received_frame_vlan_tagging_operation_table'].fields[k]
515 for k in attributes['received_frame_vlan_tagging_operation_table'].fields.keys()))
516 self.assertTrue(all(data['received_frame_vlan_tagging_operation_table'][0].fields[k] == table_as_dict[k]
517 for k in table_as_dict.keys()))
518
519
520if __name__ == '__main__':
521 main()