blob: c5586fe42fdf7a00c74350f06772814660b99a8c [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
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
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070017import pexpect,os
18import time
19import json
20import collections
21import sys
22import robot
23import os.path
24from os.path import expanduser
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070025import uuid
Kailash Khalasi2adbad82017-05-15 14:53:40 -070026import random
27import re
Kailash Khalasi86e231e2017-06-06 13:13:43 -070028import yaml
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070029
30class utils(object):
31
32 @staticmethod
33 def listToDict(alist, intListIndex):
34 dictInfo = alist[int(intListIndex)]
35 return dictInfo
36
37 @staticmethod
38 def jsonToList(strFile, strListName):
39 data = json.loads(open(strFile).read())
40 #print "data...",data
41 dataList = data[strListName]
42 return dataList
43
Suchitra.Vemuri85220062016-10-25 10:44:11 -070044 '''
45 @method compare_dict
46 @Description: validates if contents of dict1 exists in dict2
47 @params: dict1 = input_data entered through api
48 dict2 = retrieved data from GET method
49 returns True if contents of dict1 exists in dict2
50 '''
You Wang507c4562016-11-23 13:36:09 -080051 def compare_dict(self, dict1, dict2):
52 print "input data", dict1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070053 print "get data", dict2
54 if dict1 == None or dict2 == None:
55 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070056 if type(dict1) is not dict or type(dict2) is not dict:
57 return False
You Wang507c4562016-11-23 13:36:09 -080058 if dict1 == {}:
59 return True
60 return self.compare_dict_recursive(dict1, dict2)
Suchitra.Vemuri85220062016-10-25 10:44:11 -070061
You Wang507c4562016-11-23 13:36:09 -080062 '''
63 @method compare_dict_recursive
64 @Description: recursive function to validate if dict1 is a subset of dict2
65 returns True if contents of dict1 exists in dict2
66 '''
67 def compare_dict_recursive(self, dict1, dict2):
Suchitra.Vemuri85220062016-10-25 10:44:11 -070068 for key1,value1 in dict1.items():
You Wang507c4562016-11-23 13:36:09 -080069 if key1 not in dict2.keys():
70 print "Missing key", key1, "in dict2"
71 return False
72 value2 = dict2[key1]
73 if type(value1) is dict and type(value2) is dict:
74 if not self.compare_dict_recursive(value1, value2):
75 return False
76 else:
77 if value2 != value1:
78 print "Values of key", key1, "in two dicts are not equal"
79 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070080 return True
81
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070082 '''
You Wang3964e842016-12-09 12:04:32 -080083 @method compare_list_of_dicts
84 @Description: validates if contents of dicts in list1 exists in dicts of list2
85 returns True if for each dict in list1, there's a dict in list2 that contains its content
86 '''
87 def compare_list_of_dicts(self, list1, list2):
88 for dict1 in list1:
89 if dict1 == {}:
90 continue
91 key = dict1.keys()[0]
92 value = dict1[key]
93 dict2 = self.getDictFromListOfDict(list2, key, value)
94 if dict2 == {}:
95 print "Comparison failed: no dictionaries found in list2 with key", key, "and value", value
96 return False
97 if self.compare_dict(dict1, dict2) == False:
98 print "Comparison failed: dictionary", dict1, "is not a subset of dictionary", dict2
99 return False
100 return True
101
102 '''
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700103 @method search_dictionary
104 @Description: Searches for a key in the provided nested dictionary
105 @params: input_dict = dictionary to be searched
106 search_key = name of the key to be searched for
107 returns two values: search_key value and status of the search.
108 True if found (False when not found)
109
110 '''
111 def search_dictionary(self,input_dict, search_key):
112 input_keys = input_dict.keys()
113 key_value = ''
114 found = False
115 for key in input_keys:
116 if key == search_key:
117 key_value = input_dict[key]
118 found = True
119 break
120 elif type(input_dict[key]) == dict:
121 key_value, found = self.search_dictionary(input_dict[key],search_key)
122 if found == True:
123 break
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800124 elif type(input_dict[key]) == list:
125 if not input_dict[key]:
126 found = False
127 break
128 for item in input_dict[key]:
129 if isinstance(item, dict):
130 key_value, found = self.search_dictionary(item, search_key)
131 if found == True:
132 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700133 return key_value,found
134 '''
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800135 @method getDictFromListOfDict
136 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700137 @Description: Searches for the dictionary in the provided list of dictionaries
138 that matches the value of the key provided
139 @params : List of dictionaries(getResponse Data from the URL),
140 SearchKey - Key that needs to be searched for (ex: account_num)
141 searchKeyValue - Value of the searchKey (ex: 21)
142 @Returns: Dictionary returned when match found for searchKey with the corresponding
143 searchKeyValue provided
144 '''
145
146 def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue):
147 return_dict = {}
148 result = ''
149 for data in getJsonDataList:
Suchitra Vemurif58de922017-06-14 12:53:42 -0700150 print "data", data
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700151 return_dict = {}
152 found = False
153 input_keys = data.keys()
154 for key in input_keys:
155 if key == searchKey and str(data[key]) == str(searchKeyValue):
156 found = True
157 return_dict = data
Suchitra Vemurif58de922017-06-14 12:53:42 -0700158 print "return_dict",return_dict
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700159 break
160 elif type(data[key]) == dict:
161 result, found = self.search_dictionary(data[key],searchKey)
162 if found == True and str(result) == str(searchKeyValue):
163 return_dict = data
164 break
165 elif type(data[key]) == list:
166 for item in data[key]:
167 if isinstance(item, dict):
168 result, found = self.search_dictionary(data[key], searchKey)
169 if found == True and str(result) == str(searchKeyValue):
170 return_dict = data
171 break
172 if return_dict:
173 break
174 return return_dict
175
176
177 '''
178 @method getFieldValueFromDict
179 @params : search_dict - Dictionary to be searched
180 field - Key to be searched for (ex: account_num)
181 @Returns: Returns the value of the Key that was provided
182 '''
183 def getFieldValueFromDict(self,search_dict, field):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700184 results = ''
185 found = False
186 input_keys = search_dict.keys()
187 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800188 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700189 if key == field:
190 results = search_dict[key]
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800191 if not results:
192 found = True
193 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700194 elif type(search_dict[key]) == dict:
195 results, found = self.search_dictionary(search_dict[key],field)
196 if found == True:
197 break
198 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800199 if not search_dict[key]:
200 found = False
You Wangf462ee92016-12-16 13:11:43 -0800201 continue
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700202 for item in search_dict[key]:
203 if isinstance(item, dict):
204 results, found = self.search_dictionary(item, field)
205 if found == True:
206 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800207 if results:
208 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700209
210 return results
211
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800212 def setFieldValueInDict(self,input_dict,field,field_value):
213 input_dict[field]=field_value
214 return input_dict
215
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800216 '''
217 @method getAllFieldValues
218 @params : getJsonDataDictList - List of dictionaries to be searched
219 fieldName - Key to be searched for (ex: instance_id)
220 @Returns: Returns the unique value of the Key that was provided
You Wangf462ee92016-12-16 13:11:43 -0800221 '''
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800222
223 def getAllFieldValues(self, getJsonDataDictList, fieldName):
224 value_list = []
225 uniqValue = ''
226 uniq_list = []
227 for data in getJsonDataDictList:
228 fieldValue = ''
229 fieldValue = self.getFieldValueFromDict(data, fieldName)
230 value_list.append(fieldValue)
231 uniq_list = sorted(set(value_list))
232 if len(uniq_list) == 1:
233 uniqValue = uniq_list[0]
234 else:
235 print "list of values found for ", fieldName, ";", uniq_list
236 return fieldValue
You Wangf462ee92016-12-16 13:11:43 -0800237
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700238 def generate_uuid(self):
239 return uuid.uuid4()
240
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700241 def generate_random_number_from_blacklist(self, blacklist, min=100, max=500, typeTag=False):
242 num = None
243 while num in blacklist or num is None:
244 num = random.randrange(int(min), int(max))
245 if typeTag:
246 return num
247 else:
248 return str(num)
249
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700250 def get_dynamic_resources(self, inputfile, resource):
251 resourceNames = []
252 names = {}
253 dnames = []
254 with open(inputfile, 'r') as f:
255 contents = yaml.load(f)
256 resources = contents[resource]
257 for i in resources:
258 resourceNames.append(i["name"])
259 for i in resourceNames:
260 names['name']=i
261 dnames.append(names.copy())
262 return dnames
263
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700264'''
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700265#Test
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800266dict_list = {
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700267 "humanReadableName": "cordSubscriber-17",
268 "id": 17,
269 "features": {
270 "uplink_speed": 1000000000,
271 "downlink_speed": 1000000000,
272 "status": "enabled"
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800273 },
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700274 "identity": {
275 "account_num": "20",
276 "name": "My House"
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800277 },
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700278 "related": {}
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800279 }
280input_dict = {
281 "s_tag" : "111",
282 "c_tag" : "222",
283 "subscriber" : ""
284 }
285new_value = 3
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700286test = utils()
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700287#data=test.jsonToList("Subscribers.json","SubscriberInfo")
288#print test.jsonToList("Subscribers.json","SubscriberInfo")
289#print "index 1...",test.listToDict(data,1)
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700290#result = test.getDictFromListOfDict(dict_list,"email",21)
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800291#result = test.getFieldValueFromDict(dict_list,"id")
292#result = test.getDictFromListOfDict(dict_list,"account_num",21)
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800293#result = test.setFieldValueInDict(input_dict,"subscriber",new_value)
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700294result = test.getAllFieldValues(list1,"instance_name")
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700295print "finalllllll result....", result
296'''