blob: 9da6a98a4d2634d3ae77370b7d2987e7cc38caff [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
TorstenThiemeff9c9142021-04-08 07:21:34 +000020import operator
uwe ottrembka504ca3e2020-11-23 12:02:20 +010021
22# global definition of keys (find in given 'inventory_data')
23_NAME = 'name'
24_CHILDREN = 'children'
25_SENSOR_DATA = 'sensor_data'
26_ROOT = 'root'
27_INVENTORY = 'inventory'
28_UUID = 'uuid'
29
30
31def test(success):
32 if success is True:
33 return True
34 return False
35
36
37def unique():
38 """Returns the current filename and line number in our program."""
39 trace = str(os.path.basename(__file__) +
40 "[" + str(inspect.currentframe().f_back.f_lineno) + "]:")
41 return trace
42
43
44# check if given paramter exist in inventory data, search recursive
45def check_in_inventory_Component_data(inventory_data, name, element, value):
46 print(unique(), str(inventory_data), str(name), str(element), str(value))
47 if inventory_data.get(_NAME) == name and inventory_data.get(element) == value:
48 return True
49
50 for child in inventory_data[_CHILDREN]:
51 print(unique(), str(child))
52 if child.get(_NAME) == name and child.get(element) == value:
53 return True
54 if _SENSOR_DATA in child:
55 for sensor_data in child[_SENSOR_DATA]:
56 print(unique(), str(sensor_data))
57 if sensor_data.get(element) == value:
58 return True
59 if _CHILDREN in child:
60 result = check_in_inventory_Component_data(child, name, element, value)
61 if result is True:
62 return result
63 return False
64
65
66# get uuid out of inventory data, search recursive
67def get_uuid_from_inventory_Component_data(inventory_data, searchFor):
68 print(unique(), str(inventory_data), ', ', str(searchFor))
69 if inventory_data.get(_NAME) == searchFor:
70 return inventory_data.get(_UUID)
71 for child in inventory_data[_CHILDREN]:
72 print(unique(), str(child))
73 result = None
74 if child.get(_NAME) == searchFor:
75 print(unique(), str(child[_NAME]))
76 result = child.get(_UUID)
77 print(unique(), child.keys())
78 if result is None and _CHILDREN in child:
79 result = get_uuid_from_inventory_Component_data(child, searchFor)
80 if result is not None:
81 return result
82 return None
83
84
85def get_uuid_from_Inventory_Element(inventory, searchFor):
86 for children in inventory[_INVENTORY][_ROOT][_CHILDREN]:
87 return get_uuid_from_inventory_Component_data(children, searchFor)
88 return None
89
90
91def check_Inventory_Element(inventory, name, element, value):
92 for childrens in inventory[_INVENTORY][_ROOT][_CHILDREN]:
93 return check_in_inventory_Component_data(childrens, name, element, value)
94 return False
95
96
97def getWord(line, number):
98 line_in_list = line.split()
99 if len(line_in_list) >= number-1:
100 return line_in_list[number-1]
101 return ""
102
103
104def decode(data):
105 decoded_data = data
106 print(unique(), str(decoded_data))
TorstenThiemeff9c9142021-04-08 07:21:34 +0000107
108
109# Compares two values using a given operator. The values are converted to float first so that
110# numbers as strings are also accepted. Returns True or False.
111# operator: ==, !=, <, <=, >, >=
112# Example:
113# | ${result} | Compare | 100 | > | 5 | # True |
114def compare(value1, op, value2):
115 ops = {"==": operator.eq,
116 "!=": operator.ne,
117 "<": operator.lt,
118 "<=": operator.le,
119 ">": operator.gt,
120 ">=": operator.ge}
121 return ops[op](float(value1), float(value2))
122
123
124# Validates two values using a given operator.
125# The values are converted to float first so that numbers as strings are also accepted.
126# Second value has to be a list in case of operator is 'in' or 'range'
127# Returns True or False.
128# operator: in, range, ==, !=, <, <=, >, >=
129# Example:
130# | ${result} | validate | 100 | > | 5 | # True |
131# | ${result} | validate | 11 | in | ['11','264','329'] | # True |
132# | ${result} | validate | 1 | range | ['0','1'] | # True |
133def validate(value1, op, value2):
134 if op == "in":
135 return (float(value1) in [float(i) for i in value2])
136 if op == "range":
137 return ((compare(value1, ">=", value2[0])) and (compare(value1, "<=", value2[1])))
138 return compare(value1, op, value2)