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