Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame^] | 1 | |
| 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.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 17 | import pexpect,os |
| 18 | import time |
| 19 | import json |
| 20 | import collections |
| 21 | import sys |
| 22 | import robot |
| 23 | import os.path |
| 24 | from os.path import expanduser |
Kailash Khalasi | b6e87fc | 2017-04-18 15:08:19 -0700 | [diff] [blame] | 25 | import uuid |
Kailash Khalasi | 2adbad8 | 2017-05-15 14:53:40 -0700 | [diff] [blame] | 26 | import random |
| 27 | import re |
Kailash Khalasi | 86e231e | 2017-06-06 13:13:43 -0700 | [diff] [blame] | 28 | import yaml |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 29 | |
| 30 | class 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 44 | ''' |
| 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 Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 51 | def compare_dict(self, dict1, dict2): |
| 52 | print "input data", dict1 |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 53 | print "get data", dict2 |
| 54 | if dict1 == None or dict2 == None: |
| 55 | return False |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 56 | if type(dict1) is not dict or type(dict2) is not dict: |
| 57 | return False |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 58 | if dict1 == {}: |
| 59 | return True |
| 60 | return self.compare_dict_recursive(dict1, dict2) |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 61 | |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 62 | ''' |
| 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 68 | for key1,value1 in dict1.items(): |
You Wang | 507c456 | 2016-11-23 13:36:09 -0800 | [diff] [blame] | 69 | 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.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 80 | return True |
| 81 | |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 82 | ''' |
You Wang | 3964e84 | 2016-12-09 12:04:32 -0800 | [diff] [blame] | 83 | @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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 103 | @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.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 124 | 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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 133 | return key_value,found |
| 134 | ''' |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 135 | @method getDictFromListOfDict |
| 136 | return key_value,found |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 137 | @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 Vemuri | f58de92 | 2017-06-14 12:53:42 -0700 | [diff] [blame] | 150 | print "data", data |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 151 | 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 Vemuri | f58de92 | 2017-06-14 12:53:42 -0700 | [diff] [blame] | 158 | print "return_dict",return_dict |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 159 | 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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 184 | results = '' |
| 185 | found = False |
| 186 | input_keys = search_dict.keys() |
| 187 | for key in input_keys: |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 188 | print "key...", key |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 189 | if key == field: |
| 190 | results = search_dict[key] |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 191 | if not results: |
| 192 | found = True |
| 193 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 194 | 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.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 199 | if not search_dict[key]: |
| 200 | found = False |
You Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 201 | continue |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 202 | 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.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 207 | if results: |
| 208 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 209 | |
| 210 | return results |
| 211 | |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 212 | def setFieldValueInDict(self,input_dict,field,field_value): |
| 213 | input_dict[field]=field_value |
| 214 | return input_dict |
| 215 | |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame] | 216 | ''' |
| 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 Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 221 | ''' |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame] | 222 | |
| 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 Wang | f462ee9 | 2016-12-16 13:11:43 -0800 | [diff] [blame] | 237 | |
Kailash Khalasi | b6e87fc | 2017-04-18 15:08:19 -0700 | [diff] [blame] | 238 | def generate_uuid(self): |
| 239 | return uuid.uuid4() |
| 240 | |
Kailash Khalasi | 2adbad8 | 2017-05-15 14:53:40 -0700 | [diff] [blame] | 241 | 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 Khalasi | 86e231e | 2017-06-06 13:13:43 -0700 | [diff] [blame] | 250 | 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.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 264 | ''' |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 265 | #Test |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 266 | dict_list = { |
Kailash Khalasi | 2adbad8 | 2017-05-15 14:53:40 -0700 | [diff] [blame] | 267 | "humanReadableName": "cordSubscriber-17", |
| 268 | "id": 17, |
| 269 | "features": { |
| 270 | "uplink_speed": 1000000000, |
| 271 | "downlink_speed": 1000000000, |
| 272 | "status": "enabled" |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 273 | }, |
Kailash Khalasi | 2adbad8 | 2017-05-15 14:53:40 -0700 | [diff] [blame] | 274 | "identity": { |
| 275 | "account_num": "20", |
| 276 | "name": "My House" |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 277 | }, |
Kailash Khalasi | 2adbad8 | 2017-05-15 14:53:40 -0700 | [diff] [blame] | 278 | "related": {} |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 279 | } |
| 280 | input_dict = { |
| 281 | "s_tag" : "111", |
| 282 | "c_tag" : "222", |
| 283 | "subscriber" : "" |
| 284 | } |
| 285 | new_value = 3 |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 286 | test = utils() |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 287 | #data=test.jsonToList("Subscribers.json","SubscriberInfo") |
| 288 | #print test.jsonToList("Subscribers.json","SubscriberInfo") |
| 289 | #print "index 1...",test.listToDict(data,1) |
Kailash Khalasi | 2adbad8 | 2017-05-15 14:53:40 -0700 | [diff] [blame] | 290 | #result = test.getDictFromListOfDict(dict_list,"email",21) |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 291 | #result = test.getFieldValueFromDict(dict_list,"id") |
| 292 | #result = test.getDictFromListOfDict(dict_list,"account_num",21) |
Suchitra.Vemuri | 0c8024a | 2016-12-07 16:31:21 -0800 | [diff] [blame] | 293 | #result = test.setFieldValueInDict(input_dict,"subscriber",new_value) |
Kailash Khalasi | 2adbad8 | 2017-05-15 14:53:40 -0700 | [diff] [blame] | 294 | result = test.getAllFieldValues(list1,"instance_name") |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 295 | print "finalllllll result....", result |
| 296 | ''' |