uwe ottrembka | 504ca3e | 2020-11-23 12:02:20 +0100 | [diff] [blame] | 1 | # Copyright 2020 Open Networking Foundation |
| 2 | # delivered by ADTRAN, Inc. |
| 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 | from __future__ import absolute_import |
| 17 | from __future__ import print_function |
| 18 | import inspect |
| 19 | import os |
| 20 | |
| 21 | # global definition of keys (find in given 'inventory_data') |
| 22 | _NAME = 'name' |
| 23 | _CHILDREN = 'children' |
| 24 | _SENSOR_DATA = 'sensor_data' |
| 25 | _ROOT = 'root' |
| 26 | _INVENTORY = 'inventory' |
| 27 | _UUID = 'uuid' |
| 28 | |
| 29 | |
| 30 | def test(success): |
| 31 | if success is True: |
| 32 | return True |
| 33 | return False |
| 34 | |
| 35 | |
| 36 | def unique(): |
| 37 | """Returns the current filename and line number in our program.""" |
| 38 | trace = str(os.path.basename(__file__) + |
| 39 | "[" + str(inspect.currentframe().f_back.f_lineno) + "]:") |
| 40 | return trace |
| 41 | |
| 42 | |
| 43 | # check if given paramter exist in inventory data, search recursive |
| 44 | def check_in_inventory_Component_data(inventory_data, name, element, value): |
| 45 | print(unique(), str(inventory_data), str(name), str(element), str(value)) |
| 46 | if inventory_data.get(_NAME) == name and inventory_data.get(element) == value: |
| 47 | return True |
| 48 | |
| 49 | for child in inventory_data[_CHILDREN]: |
| 50 | print(unique(), str(child)) |
| 51 | if child.get(_NAME) == name and child.get(element) == value: |
| 52 | return True |
| 53 | if _SENSOR_DATA in child: |
| 54 | for sensor_data in child[_SENSOR_DATA]: |
| 55 | print(unique(), str(sensor_data)) |
| 56 | if sensor_data.get(element) == value: |
| 57 | return True |
| 58 | if _CHILDREN in child: |
| 59 | result = check_in_inventory_Component_data(child, name, element, value) |
| 60 | if result is True: |
| 61 | return result |
| 62 | return False |
| 63 | |
| 64 | |
| 65 | # get uuid out of inventory data, search recursive |
| 66 | def get_uuid_from_inventory_Component_data(inventory_data, searchFor): |
| 67 | print(unique(), str(inventory_data), ', ', str(searchFor)) |
| 68 | if inventory_data.get(_NAME) == searchFor: |
| 69 | return inventory_data.get(_UUID) |
| 70 | for child in inventory_data[_CHILDREN]: |
| 71 | print(unique(), str(child)) |
| 72 | result = None |
| 73 | if child.get(_NAME) == searchFor: |
| 74 | print(unique(), str(child[_NAME])) |
| 75 | result = child.get(_UUID) |
| 76 | print(unique(), child.keys()) |
| 77 | if result is None and _CHILDREN in child: |
| 78 | result = get_uuid_from_inventory_Component_data(child, searchFor) |
| 79 | if result is not None: |
| 80 | return result |
| 81 | return None |
| 82 | |
| 83 | |
| 84 | def get_uuid_from_Inventory_Element(inventory, searchFor): |
| 85 | for children in inventory[_INVENTORY][_ROOT][_CHILDREN]: |
| 86 | return get_uuid_from_inventory_Component_data(children, searchFor) |
| 87 | return None |
| 88 | |
| 89 | |
| 90 | def check_Inventory_Element(inventory, name, element, value): |
| 91 | for childrens in inventory[_INVENTORY][_ROOT][_CHILDREN]: |
| 92 | return check_in_inventory_Component_data(childrens, name, element, value) |
| 93 | return False |
| 94 | |
| 95 | |
| 96 | def getWord(line, number): |
| 97 | line_in_list = line.split() |
| 98 | if len(line_in_list) >= number-1: |
| 99 | return line_in_list[number-1] |
| 100 | return "" |
| 101 | |
| 102 | |
| 103 | def decode(data): |
| 104 | decoded_data = data |
| 105 | print(unique(), str(decoded_data)) |