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 | |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 17 | #!/usr/bin/env python |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 18 | import requests, json, os, sys, time |
| 19 | #sys.path.append('common-utils') |
| 20 | sys.path.append(os.path.join(sys.path[0],'utils')) |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 21 | from readProperties import readProperties |
| 22 | |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 23 | class restApi(object): |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 24 | ''' |
| 25 | Functions for testing CORD API with POST, GET, PUT, DELETE method |
| 26 | ''' |
| 27 | def __init__(self): |
You Wang | d4dacd5 | 2017-02-16 10:58:53 -0800 | [diff] [blame] | 28 | self.rp = readProperties(os.path.abspath(os.path.dirname(__file__))+"/../Properties/RestApiProperties.py") |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 29 | self.controllerIP = self.getValueFromProperties("SERVER_IP") |
| 30 | self.controllerPort = self.getValueFromProperties("SERVER_PORT") |
| 31 | self.user = self.getValueFromProperties("USER") |
| 32 | self.password = self.getValueFromProperties("PASSWD") |
| 33 | self.jsonHeader = {'Content-Type': 'application/json'} |
| 34 | |
| 35 | def getValueFromProperties(self, key): |
| 36 | ''' |
| 37 | Get and return values from properties file |
| 38 | ''' |
| 39 | rawValue = self.rp.getValueProperties(key) |
| 40 | value = rawValue.replace("'","") |
| 41 | return value |
| 42 | |
| 43 | def getURL(self, key): |
| 44 | ''' |
| 45 | Get REST API suffix from key and return the full URL |
| 46 | ''' |
| 47 | urlSuffix = self.getValueFromProperties(key) |
| 48 | url = "http://" + self.controllerIP + ":" + self.controllerPort + urlSuffix |
| 49 | return url |
| 50 | |
| 51 | def checkResult(self, resp, expectedStatus): |
| 52 | ''' |
| 53 | Check if the status code in resp equals to the expected number. |
| 54 | Return True or False based on the check result. |
| 55 | ''' |
| 56 | if resp.status_code == expectedStatus: |
| 57 | print "Test passed: " + str(resp.status_code) + ": " + resp.text |
| 58 | return True |
| 59 | else: |
| 60 | print "Test failed: " + str(resp.status_code) + ": " + resp.text |
| 61 | return False |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 62 | ''' |
| 63 | @method getAccountNum |
| 64 | @Returns AccountNumber for the subscriber |
| 65 | @params: jsonData is Dictionary |
| 66 | ''' |
| 67 | def getAccountNum(self, jsonData): |
| 68 | print type(str(jsonData['identity']['account_num'])) |
| 69 | return jsonData['identity']['account_num'] |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 70 | |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 71 | def getSubscriberId(self, jsonDataList, accountNum): |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 72 | ''' |
| 73 | Search in each json data in the given list to find and return the |
| 74 | subscriber id that corresponds to the given account number. |
| 75 | ''' |
| 76 | # Here we assume subscriber id starts from 1 |
| 77 | subscriberId = 0 |
| 78 | try: |
| 79 | for jsonData in jsonDataList: |
A R Karthick | 028edaf | 2017-11-06 16:34:57 -0800 | [diff] [blame] | 80 | if jsonData["service_specific_id"] == str(accountNum): |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 81 | subscriberId = jsonData["id"] |
| 82 | break |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 83 | return str(subscriberId) |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 84 | except KeyError: |
| 85 | print "Something wrong with the json data provided: ", jsonData |
| 86 | return -1 |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 87 | ''' |
| 88 | Retrieve the correct jsonDict from the List of json objects returned |
| 89 | from Get Reponse |
| 90 | Account Number is the one used to post "Data" |
| 91 | ''' |
| 92 | def getJsonDictOfAcctNum(self, getResponseList, AccountNum): |
| 93 | getJsonDict = {} |
| 94 | try: |
| 95 | for data in getResponseList: |
| 96 | if data['identity']['account_num'] == AccountNum: |
| 97 | getJsonDict = data |
| 98 | break |
| 99 | return getJsonDict |
| 100 | except KeyError: |
| 101 | print "Could not find the related account number in Get Resonse Data" |
| 102 | return -1 |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 103 | |
| 104 | def ApiPost(self, key, jsonData): |
| 105 | url = self.getURL(key) |
| 106 | data = json.dumps(jsonData) |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 107 | print "url, data..", url, data |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 108 | resp = requests.post(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password)) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 109 | print "requests.codes.....",requests.codes.created |
| 110 | passed = self.checkResult(resp, requests.codes.created) or self.checkResult(resp, requests.codes.ok) |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 111 | return passed |
| 112 | |
| 113 | def ApiGet(self, key, urlSuffix=""): |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 114 | url = self.getURL(key) + str(urlSuffix) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 115 | print "get url...",url |
| 116 | resp = requests.get(url, auth=(self.user, self.password)) |
| 117 | passed = self.checkResult(resp, requests.codes.ok) |
| 118 | if not passed: |
| 119 | return None |
| 120 | else: |
| 121 | return resp.json() |
| 122 | |
| 123 | def ApiChameleonGet(self, key, urlSuffix=""): |
| 124 | url = self.getURL(key) + "/" + str(urlSuffix) |
| 125 | print "get url...",url |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 126 | resp = requests.get(url, auth=(self.user, self.password)) |
| 127 | passed = self.checkResult(resp, requests.codes.ok) |
| 128 | if not passed: |
| 129 | return None |
| 130 | else: |
| 131 | return resp.json() |
| 132 | |
| 133 | def ApiPut(self, key, jsonData, urlSuffix=""): |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 134 | print "urlSuffix....",type(urlSuffix) |
| 135 | url = self.getURL(key) + str(urlSuffix) + "/" |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 136 | data = json.dumps(jsonData) |
| 137 | resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password)) |
| 138 | passed = self.checkResult(resp, requests.codes.ok) |
| 139 | return passed |
| 140 | |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 141 | def ApiChameleonPut(self, key, jsonData, urlSuffix=""): |
| 142 | print "urlSuffix....",type(urlSuffix) |
| 143 | url = self.getURL(key) + "/" + str(urlSuffix) |
| 144 | print "url", url |
| 145 | data = json.dumps(jsonData) |
| 146 | resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password)) |
| 147 | passed = self.checkResult(resp, requests.codes.ok) |
| 148 | return passed |
| 149 | |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 150 | def ApiDelete(self, key, urlSuffix=""): |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 151 | url = self.getURL(key) + str(urlSuffix) |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 152 | print "url",url |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 153 | resp = requests.delete(url, auth=(self.user, self.password)) |
You Wang | 853c025 | 2017-05-17 13:28:31 -0700 | [diff] [blame] | 154 | passed = self.checkResult(resp, requests.codes.no_content) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 155 | return passed |
| 156 | |
| 157 | def ApiChameleonDelete(self, key, urlSuffix=""): |
| 158 | url = self.getURL(key) + "/" + str(urlSuffix) |
| 159 | print "url",url |
| 160 | resp = requests.delete(url, auth=(self.user, self.password)) |
| 161 | #passed = self.checkResult(resp, requests.codes.no_content) |
| 162 | passed = self.checkResult(resp, requests.codes.created) or self.checkResult(resp, requests.codes.ok) |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 163 | return passed |
| 164 | |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 165 | #test |
| 166 | ''' |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 167 | if __name__ == '__main__': |
| 168 | test = RestApi() |
| 169 | key = "TENANT_SUBSCRIBER" |
| 170 | account_num = 5 |
| 171 | result = test.ApiPost(key, {"identity":{"account_num":str(account_num)}}) |
| 172 | time.sleep(5) |
| 173 | result = test.ApiGet(key) |
| 174 | subId = test.getSubscriberIdFromAccountNum(result, account_num) |
| 175 | urlSuffix = str(subId) + "/" |
| 176 | time.sleep(5) |
| 177 | result = test.ApiPut(key, {"identity":{"name":"My House 2"}}, urlSuffix) |
| 178 | time.sleep(5) |
| 179 | result = test.ApiDelete(key, urlSuffix) |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 180 | ''' |
Suchitra Vemuri | daeb247 | 2017-08-15 11:58:21 -0700 | [diff] [blame] | 181 | ''' |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 182 | test = restApi() |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 183 | #key = "UTILS_SYNCHRONIZER" |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 184 | #key = "CORE_USERS" |
| 185 | #key2 = "UTILS_LOGIN" |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 186 | #key = "TENANT_SUBSCRIBER" |
| 187 | #jsonGetData = test.ApiGet(key) |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 188 | #jsonResponse = test.ApiPost(key,{"identity":{"name":"My House 22"}}) |
| 189 | #jsonResponse = test.ApiPost(key,{"firstname":"Test002","lastname":"User002","email":"test002@onlab.us","password":"TestUser002","site": "http://localhost:8000/api/core/sites/1/"}) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 190 | key = "VOLT_TENANT" |
Suchitra Vemuri | daeb247 | 2017-08-15 11:58:21 -0700 | [diff] [blame] | 191 | key2 = "VOLT_SUBSCRIBER" |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 192 | #jsonResponse = test.ApiDelete(key,204) |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 193 | #jsonResponse = test.ApiPut(key,{"firstname":"Test002","lastname":"User002","email":"test002update@onlab.us","password":"TestUser002","site": "http://localhost:8000/api/core/sites/1/"},14) |
| 194 | #jsonResponse = test.ApiPost(key2,{"username":"test002update@onlab.us","password":"TestUser002"}) |
| 195 | #jsonResponse = test.ApiPost(key2,{"username":"padmin@vicci.org","password":"letmein"}) |
| 196 | #jsonResponse = test.ApiPut(key,{"username":"testuser","password":"TestUser001"},"9") |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 197 | #key = "CORE_INSTANCES" |
Suchitra Vemuri | daeb247 | 2017-08-15 11:58:21 -0700 | [diff] [blame] | 198 | key1 = "CH_CORE_SERVICELINK" |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 199 | #key2 = "CORE_SLICES" |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 200 | #input_dict = { "s_tag" : "111", "c_tag" : "222", "subscriber" : 23} |
Suchitra.Vemuri | 75dffd4 | 2016-12-20 15:35:25 -0800 | [diff] [blame] | 201 | input_dict = { |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 202 | "s_tag" : 117, |
Suchitra Vemuri | f58de92 | 2017-06-14 12:53:42 -0700 | [diff] [blame] | 203 | "c_tag" : 227, |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 204 | } |
Suchitra Vemuri | daeb247 | 2017-08-15 11:58:21 -0700 | [diff] [blame] | 205 | input_dict3 = { |
| 206 | "subscriber_service_instance_id" : 5 |
| 207 | } |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 208 | #input_dict1 = { "name" : "mysite_Test1", "site" : 1 , "creator" : 1} |
| 209 | input_dict2 = { |
A R Karthick | 028edaf | 2017-11-06 16:34:57 -0800 | [diff] [blame] | 210 | |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 211 | "cdn_enable": "true", |
| 212 | "uplink_speed": 1000000000, |
| 213 | "downlink_speed": 1000000000, |
| 214 | "enable_uverse": "true", |
| 215 | "status": "enabled", |
| 216 | "service_specific_id": "100", |
| 217 | "name": "My House" |
| 218 | } |
| 219 | #jsonResponse = test.ApiPost(key,input_dict) |
| 220 | #jsonResponse = test.ApiChameleonPut(key,input_dict,12) |
Suchitra Vemuri | daeb247 | 2017-08-15 11:58:21 -0700 | [diff] [blame] | 221 | #jsonGetData = test.ApiGet(key1) |
| 222 | #jsonResponse = test.ApiChameleonPut(key1,input_dict3,6) |
| 223 | #jsonGetData = test.ApiGet(key1) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 224 | #print "=========" |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 225 | #print jsonGetData |
Suchitra.Vemuri | 75dffd4 | 2016-12-20 15:35:25 -0800 | [diff] [blame] | 226 | #jsonEdit = test.ApiPut(key,{"c_tag" : "666","s_tag" : "123"},"30") |
Suchitra Vemuri | daeb247 | 2017-08-15 11:58:21 -0700 | [diff] [blame] | 227 | jsonO = test.ApiChameleonDelete(key2,"50") |
| 228 | jsonO = test.ApiChameleonDelete(key,"51") |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 229 | #jsonResponse = test.ApiPut(key,{"identity":{"name":"My House 22"}},"71") |
Suchitra Vemuri | f58de92 | 2017-06-14 12:53:42 -0700 | [diff] [blame] | 230 | #jsonResponse = test.ApiPost(key,input_dict) |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 231 | #jsonResponse = test.ApiPut(key,{"name":"test1-changed"},"9") |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 232 | print "=========" |
Suchitra Vemuri | daeb247 | 2017-08-15 11:58:21 -0700 | [diff] [blame] | 233 | ''' |