blob: 832bee2a93dd62ec45c0eb4c4fe88b6044412c85 [file] [log] [blame]
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -07001import pexpect,os
2import time
3import json
4import collections
5import sys
6import robot
7import os.path
8from os.path import expanduser
Kailash Khalasib6e87fc2017-04-18 15:08:19 -07009import uuid
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -070010
11class utils(object):
12
13 @staticmethod
14 def listToDict(alist, intListIndex):
15 dictInfo = alist[int(intListIndex)]
16 return dictInfo
17
18 @staticmethod
19 def jsonToList(strFile, strListName):
20 data = json.loads(open(strFile).read())
21 #print "data...",data
22 dataList = data[strListName]
23 return dataList
24
Suchitra.Vemuri85220062016-10-25 10:44:11 -070025 '''
26 @method compare_dict
27 @Description: validates if contents of dict1 exists in dict2
28 @params: dict1 = input_data entered through api
29 dict2 = retrieved data from GET method
30 returns True if contents of dict1 exists in dict2
31 '''
You Wang507c4562016-11-23 13:36:09 -080032 def compare_dict(self, dict1, dict2):
33 print "input data", dict1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070034 print "get data", dict2
35 if dict1 == None or dict2 == None:
36 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070037 if type(dict1) is not dict or type(dict2) is not dict:
38 return False
You Wang507c4562016-11-23 13:36:09 -080039 if dict1 == {}:
40 return True
41 return self.compare_dict_recursive(dict1, dict2)
Suchitra.Vemuri85220062016-10-25 10:44:11 -070042
You Wang507c4562016-11-23 13:36:09 -080043 '''
44 @method compare_dict_recursive
45 @Description: recursive function to validate if dict1 is a subset of dict2
46 returns True if contents of dict1 exists in dict2
47 '''
48 def compare_dict_recursive(self, dict1, dict2):
Suchitra.Vemuri85220062016-10-25 10:44:11 -070049 for key1,value1 in dict1.items():
You Wang507c4562016-11-23 13:36:09 -080050 if key1 not in dict2.keys():
51 print "Missing key", key1, "in dict2"
52 return False
53 value2 = dict2[key1]
54 if type(value1) is dict and type(value2) is dict:
55 if not self.compare_dict_recursive(value1, value2):
56 return False
57 else:
58 if value2 != value1:
59 print "Values of key", key1, "in two dicts are not equal"
60 return False
Suchitra.Vemuri85220062016-10-25 10:44:11 -070061 return True
62
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070063 '''
You Wang3964e842016-12-09 12:04:32 -080064 @method compare_list_of_dicts
65 @Description: validates if contents of dicts in list1 exists in dicts of list2
66 returns True if for each dict in list1, there's a dict in list2 that contains its content
67 '''
68 def compare_list_of_dicts(self, list1, list2):
69 for dict1 in list1:
70 if dict1 == {}:
71 continue
72 key = dict1.keys()[0]
73 value = dict1[key]
74 dict2 = self.getDictFromListOfDict(list2, key, value)
75 if dict2 == {}:
76 print "Comparison failed: no dictionaries found in list2 with key", key, "and value", value
77 return False
78 if self.compare_dict(dict1, dict2) == False:
79 print "Comparison failed: dictionary", dict1, "is not a subset of dictionary", dict2
80 return False
81 return True
82
83 '''
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -070084 @method search_dictionary
85 @Description: Searches for a key in the provided nested dictionary
86 @params: input_dict = dictionary to be searched
87 search_key = name of the key to be searched for
88 returns two values: search_key value and status of the search.
89 True if found (False when not found)
90
91 '''
92 def search_dictionary(self,input_dict, search_key):
93 input_keys = input_dict.keys()
94 key_value = ''
95 found = False
96 for key in input_keys:
97 if key == search_key:
98 key_value = input_dict[key]
99 found = True
100 break
101 elif type(input_dict[key]) == dict:
102 key_value, found = self.search_dictionary(input_dict[key],search_key)
103 if found == True:
104 break
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800105 elif type(input_dict[key]) == list:
106 if not input_dict[key]:
107 found = False
108 break
109 for item in input_dict[key]:
110 if isinstance(item, dict):
111 key_value, found = self.search_dictionary(item, search_key)
112 if found == True:
113 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700114 return key_value,found
115 '''
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800116 @method getDictFromListOfDict
117 return key_value,found
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700118 @Description: Searches for the dictionary in the provided list of dictionaries
119 that matches the value of the key provided
120 @params : List of dictionaries(getResponse Data from the URL),
121 SearchKey - Key that needs to be searched for (ex: account_num)
122 searchKeyValue - Value of the searchKey (ex: 21)
123 @Returns: Dictionary returned when match found for searchKey with the corresponding
124 searchKeyValue provided
125 '''
126
127 def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue):
128 return_dict = {}
129 result = ''
130 for data in getJsonDataList:
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700131 print "data..",data
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700132 return_dict = {}
133 found = False
134 input_keys = data.keys()
135 for key in input_keys:
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700136 print "key in input_keys...",key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700137 if key == searchKey and str(data[key]) == str(searchKeyValue):
138 found = True
139 return_dict = data
140 break
141 elif type(data[key]) == dict:
142 result, found = self.search_dictionary(data[key],searchKey)
143 if found == True and str(result) == str(searchKeyValue):
144 return_dict = data
145 break
146 elif type(data[key]) == list:
147 for item in data[key]:
148 if isinstance(item, dict):
149 result, found = self.search_dictionary(data[key], searchKey)
150 if found == True and str(result) == str(searchKeyValue):
151 return_dict = data
152 break
153 if return_dict:
154 break
155 return return_dict
156
157
158 '''
159 @method getFieldValueFromDict
160 @params : search_dict - Dictionary to be searched
161 field - Key to be searched for (ex: account_num)
162 @Returns: Returns the value of the Key that was provided
163 '''
164 def getFieldValueFromDict(self,search_dict, field):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700165 results = ''
166 found = False
167 input_keys = search_dict.keys()
168 for key in input_keys:
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800169 print "key...", key
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700170 if key == field:
171 results = search_dict[key]
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800172 if not results:
173 found = True
174 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700175 elif type(search_dict[key]) == dict:
176 results, found = self.search_dictionary(search_dict[key],field)
177 if found == True:
178 break
179 elif type(search_dict[key]) == list:
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800180 if not search_dict[key]:
181 found = False
You Wangf462ee92016-12-16 13:11:43 -0800182 continue
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700183 for item in search_dict[key]:
184 if isinstance(item, dict):
185 results, found = self.search_dictionary(item, field)
186 if found == True:
187 break
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800188 if results:
189 break
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700190
191 return results
192
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800193 def setFieldValueInDict(self,input_dict,field,field_value):
194 input_dict[field]=field_value
195 return input_dict
196
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800197 '''
198 @method getAllFieldValues
199 @params : getJsonDataDictList - List of dictionaries to be searched
200 fieldName - Key to be searched for (ex: instance_id)
201 @Returns: Returns the unique value of the Key that was provided
You Wangf462ee92016-12-16 13:11:43 -0800202 '''
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800203
204 def getAllFieldValues(self, getJsonDataDictList, fieldName):
205 value_list = []
206 uniqValue = ''
207 uniq_list = []
208 for data in getJsonDataDictList:
209 fieldValue = ''
210 fieldValue = self.getFieldValueFromDict(data, fieldName)
211 value_list.append(fieldValue)
212 uniq_list = sorted(set(value_list))
213 if len(uniq_list) == 1:
214 uniqValue = uniq_list[0]
215 else:
216 print "list of values found for ", fieldName, ";", uniq_list
217 return fieldValue
You Wangf462ee92016-12-16 13:11:43 -0800218
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700219 def generate_uuid(self):
220 return uuid.uuid4()
221
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700222'''
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700223#Test
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800224dict_list = {
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700225[
226 {
227 "humanReadableName": "mysite_vsg-1",
228 "validators": {
229 "policed": [],
230 "creator": [],
231 "ip": [],
232 "image": [
233 "notBlank"
234 ],
235 "backend_register": [
236 "notBlank"
237 ],
238 "flavor": [
239 "notBlank"
240 ],
241 "backend_status": [
242 "notBlank"
243 ],
244 "id": [],
245 "instance_name": [],
246 "slice": [
247 "notBlank"
248 ],
249 "backend_need_delete": [],
250 "enacted": [],
251 "no_sync": [],
252 "node": [
253 "notBlank"
254 ],
255 "userData": [],
256 "updated": [
257 "notBlank"
258 ],
259 "parent": [],
260 "deleted": [],
261 "lazy_blocked": [],
262 "deployment": [
263 "notBlank"
264 ],
265 "backend_need_reap": [],
266 "instance_uuid": [],
267 "numberCores": [
268 "notBlank"
269 ],
270 "name": [
271 "notBlank"
272 ],
273 "created": [],
274 "write_protect": [],
275 "isolation": [
276 "notBlank"
277 ],
278 "no_policy": [],
279 "instance_id": [],
280 "volumes": []
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800281 },
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700282 "id": 1,
283 "created": "2017-03-13T22:23:48.805109Z",
284 "updated": "2017-03-13T22:38:06.084074Z",
285 "enacted": "2017-03-13T22:38:43.894253Z",
286 "policed": "2017-03-13T22:38:08.086489Z",
287 "backend_register": "{\"next_run\": 0, \"last_success\": 1489444729.019414, \"exponent\": 0}",
288 "backend_status": "1 - OK",
289 "instance_id": "instance-00000001",
290 "instance_uuid": "a46d716f-e82c-4088-a042-72c3a97ed3ff",
291 "name": "mysite_vsg",
292 "instance_name": "mysite_vsg-1",
293 "ip": "10.1.0.17",
294 "image": "http://ms1333.utah.cloudlab.us:8080/api/core/images/1/",
295 "creator": "http://ms1333.utah.cloudlab.us:8080/api/core/users/1/",
296 "slice": "http://ms1333.utah.cloudlab.us:8080/api/core/slices/2/",
297 "deployment": "http://ms1333.utah.cloudlab.us:8080/api/core/deployments/1/",
298 "node": "http://ms1333.utah.cloudlab.us:8080/api/core/nodes/1/",
299 "numberCores": 0,
300 "flavor": "http://ms1333.utah.cloudlab.us:8080/api/core/flavors/1/",
301 "isolation": "vm",
302 "volumes": "/etc/dnsmasq.d,/etc/ufw",
303 "networks": [
304 "http://ms1333.utah.cloudlab.us:8080/api/core/networks/1/",
305 "http://ms1333.utah.cloudlab.us:8080/api/core/networks/2/"
306 ]
307 },
308 {
309 "humanReadableName": "mysite_exampleservice-2",
310 "validators": {
311 "policed": [],
312 "creator": [],
313 "ip": [],
314 "image": [
315 "notBlank"
316 ],
317 "backend_register": [
318 "notBlank"
319 ],
320 "flavor": [
321 "notBlank"
322 ],
323 "backend_status": [
324 "notBlank"
325 ],
326 "id": [],
327 "instance_name": [],
328 "slice": [
329 "notBlank"
330 ],
331 "backend_need_delete": [],
332 "enacted": [],
333 "no_sync": [],
334 "node": [
335 "notBlank"
336 ],
337 "userData": [],
338 "updated": [
339 "notBlank"
340 ],
341 "parent": [],
342 "deleted": [],
343 "lazy_blocked": [],
344 "deployment": [
345 "notBlank"
346 ],
347 "backend_need_reap": [],
348 "instance_uuid": [],
349 "numberCores": [
350 "notBlank"
351 ],
352 "name": [
353 "notBlank"
354 ],
355 "created": [],
356 "write_protect": [],
357 "isolation": [
358 "notBlank"
359 ],
360 "no_policy": [],
361 "instance_id": [],
362 "volumes": []
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800363 },
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700364 "id": 2,
365 "created": "2017-03-13T22:38:03.872267Z",
366 "updated": "2017-03-13T22:38:06.047153Z",
367 "enacted": "2017-03-13T22:39:07.002800Z",
368 "policed": "2017-03-13T22:38:07.895147Z",
369 "backend_register": "{\"next_run\": 0, \"last_success\": 1489444774.726988, \"exponent\": 0}",
370 "backend_status": "1 - OK",
371 "instance_id": "instance-00000002",
372 "instance_uuid": "cb219739-0d11-48a2-9f19-1e2aba1f004e",
373 "name": "mysite_exampleservice",
374 "instance_name": "mysite_exampleservice-2",
375 "ip": "10.1.0.17",
376 "image": "http://ms1333.utah.cloudlab.us:8080/api/core/images/3/",
377 "creator": "http://ms1333.utah.cloudlab.us:8080/api/core/users/1/",
378 "slice": "http://ms1333.utah.cloudlab.us:8080/api/core/slices/4/",
379 "deployment": "http://ms1333.utah.cloudlab.us:8080/api/core/deployments/1/",
380 "node": "http://ms1333.utah.cloudlab.us:8080/api/core/nodes/1/",
381 "numberCores": 0,
382 "flavor": "http://ms1333.utah.cloudlab.us:8080/api/core/flavors/1/",
383 "isolation": "vm",
384 "networks": [
385 "http://ms1333.utah.cloudlab.us:8080/api/core/networks/3/",
386 "http://ms1333.utah.cloudlab.us:8080/api/core/networks/1/",
387 "http://ms1333.utah.cloudlab.us:8080/api/core/networks/4/"
388 ]
389 },
390 {
391 "humanReadableName": "mysite_vsg-3",
392 "validators": {
393 "policed": [],
394 "creator": [],
395 "ip": [],
396 "image": [
397 "notBlank"
398 ],
399 "backend_register": [
400 "notBlank"
401 ],
402 "flavor": [
403 "notBlank"
404 ],
405 "backend_status": [
406 "notBlank"
407 ],
408 "id": [],
409 "instance_name": [],
410 "slice": [
411 "notBlank"
412 ],
413 "backend_need_delete": [],
414 "enacted": [],
415 "no_sync": [],
416 "node": [
417 "notBlank"
418 ],
419 "userData": [],
420 "updated": [
421 "notBlank"
422 ],
423 "parent": [],
424 "deleted": [],
425 "lazy_blocked": [],
426 "deployment": [
427 "notBlank"
428 ],
429 "backend_need_reap": [],
430 "instance_uuid": [],
431 "numberCores": [
432 "notBlank"
433 ],
434 "name": [
435 "notBlank"
436 ],
437 "created": [],
438 "write_protect": [],
439 "isolation": [
440 "notBlank"
441 ],
442 "no_policy": [],
443 "instance_id": [],
444 "volumes": []
445 },
446 "id": 3,
447 "created": "2017-03-17T23:15:13.556863Z",
448 "updated": "2017-03-17T23:15:13.555271Z",
449 "enacted": "2017-03-17T23:15:24.376854Z",
450 "policed": "2017-03-17T23:15:14.991037Z",
451 "backend_register": "{\"next_run\": 0, \"last_success\": 1489792538.996003, \"exponent\": 0}",
452 "backend_status": "1 - OK",
453 "instance_id": "instance-00000003",
454 "instance_uuid": "ec5ece6d-bebe-4165-98c5-3a026a41c63c",
455 "name": "mysite_vsg",
456 "instance_name": "mysite_vsg-3",
457 "ip": "10.1.0.17",
458 "image": "http://ms1333.utah.cloudlab.us:8080/api/core/images/1/",
459 "creator": "http://ms1333.utah.cloudlab.us:8080/api/core/users/1/",
460 "slice": "http://ms1333.utah.cloudlab.us:8080/api/core/slices/2/",
461 "deployment": "http://ms1333.utah.cloudlab.us:8080/api/core/deployments/1/",
462 "node": "http://ms1333.utah.cloudlab.us:8080/api/core/nodes/1/",
463 "numberCores": 0,
464 "flavor": "http://ms1333.utah.cloudlab.us:8080/api/core/flavors/1/",
465 "isolation": "vm",
466 "volumes": "/etc/dnsmasq.d,/etc/ufw"
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800467 }
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700468 ]
469 }
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800470input_dict = {
471 "s_tag" : "111",
472 "c_tag" : "222",
473 "subscriber" : ""
474 }
475new_value = 3
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700476test = utils()
Suchitra.Vemurifdb220a2016-10-19 14:09:53 -0700477#data=test.jsonToList("Subscribers.json","SubscriberInfo")
478#print test.jsonToList("Subscribers.json","SubscriberInfo")
479#print "index 1...",test.listToDict(data,1)
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700480result = test.getDictFromListOfDict(dict_list,"instance_name","mysite_vsg-3")
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800481#result = test.getFieldValueFromDict(dict_list,"id")
482#result = test.getDictFromListOfDict(dict_list,"account_num",21)
Suchitra.Vemuri0c8024a2016-12-07 16:31:21 -0800483#result = test.setFieldValueInDict(input_dict,"subscriber",new_value)
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700484#result = test.getAllFieldValues(list1,"instance_name")
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700485print "finalllllll result....", result
486'''