blob: 6faec6e7c592c7bb924f5f12a349e57d17cafa74 [file] [log] [blame]
uwe ottrembka504ca3e2020-11-23 12:02:20 +01001# 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
16from __future__ import absolute_import
17from __future__ import print_function
18import inspect
19import 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
30def test(success):
31 if success is True:
32 return True
33 return False
34
35
36def 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
44def 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
66def 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
84def 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
90def 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
96def 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
103def decode(data):
104 decoded_data = data
105 print(unique(), str(decoded_data))