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 | ''' |
Suchitra Vemuri | 9008144 | 2017-12-20 16:10:15 -0800 | [diff] [blame] | 27 | def __init__(self, propertyFile="RestApiProperties.py"): |
| 28 | self.rp = readProperties(os.path.abspath(os.path.dirname(__file__))+"/../Properties/"+ propertyFile) |
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 | ''' |
Andy Bavier | 85ec6b9 | 2018-04-09 15:55:55 -0700 | [diff] [blame] | 39 | try: |
| 40 | rawValue = self.rp.getValueProperties(key) |
| 41 | value = rawValue.replace("'","") |
| 42 | except: |
| 43 | value = None |
| 44 | |
| 45 | # Allow override from environment |
| 46 | if key in os.environ: |
| 47 | value = os.environ[key] |
| 48 | |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 49 | return value |
| 50 | |
| 51 | def getURL(self, key): |
| 52 | ''' |
| 53 | Get REST API suffix from key and return the full URL |
| 54 | ''' |
| 55 | urlSuffix = self.getValueFromProperties(key) |
| 56 | url = "http://" + self.controllerIP + ":" + self.controllerPort + urlSuffix |
| 57 | return url |
| 58 | |
| 59 | def checkResult(self, resp, expectedStatus): |
| 60 | ''' |
| 61 | Check if the status code in resp equals to the expected number. |
| 62 | Return True or False based on the check result. |
| 63 | ''' |
| 64 | if resp.status_code == expectedStatus: |
| 65 | print "Test passed: " + str(resp.status_code) + ": " + resp.text |
| 66 | return True |
| 67 | else: |
| 68 | print "Test failed: " + str(resp.status_code) + ": " + resp.text |
| 69 | return False |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 70 | ''' |
| 71 | @method getAccountNum |
| 72 | @Returns AccountNumber for the subscriber |
| 73 | @params: jsonData is Dictionary |
| 74 | ''' |
| 75 | def getAccountNum(self, jsonData): |
| 76 | print type(str(jsonData['identity']['account_num'])) |
| 77 | return jsonData['identity']['account_num'] |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 78 | |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 79 | def getSubscriberId(self, jsonDataList, accountNum): |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 80 | ''' |
| 81 | Search in each json data in the given list to find and return the |
| 82 | subscriber id that corresponds to the given account number. |
| 83 | ''' |
| 84 | # Here we assume subscriber id starts from 1 |
| 85 | subscriberId = 0 |
| 86 | try: |
| 87 | for jsonData in jsonDataList: |
A R Karthick | 028edaf | 2017-11-06 16:34:57 -0800 | [diff] [blame] | 88 | if jsonData["service_specific_id"] == str(accountNum): |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 89 | subscriberId = jsonData["id"] |
| 90 | break |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 91 | return str(subscriberId) |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 92 | except KeyError: |
| 93 | print "Something wrong with the json data provided: ", jsonData |
| 94 | return -1 |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 95 | ''' |
| 96 | Retrieve the correct jsonDict from the List of json objects returned |
| 97 | from Get Reponse |
| 98 | Account Number is the one used to post "Data" |
| 99 | ''' |
| 100 | def getJsonDictOfAcctNum(self, getResponseList, AccountNum): |
| 101 | getJsonDict = {} |
| 102 | try: |
| 103 | for data in getResponseList: |
| 104 | if data['identity']['account_num'] == AccountNum: |
| 105 | getJsonDict = data |
| 106 | break |
| 107 | return getJsonDict |
| 108 | except KeyError: |
| 109 | print "Could not find the related account number in Get Resonse Data" |
| 110 | return -1 |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 111 | |
| 112 | def ApiPost(self, key, jsonData): |
| 113 | url = self.getURL(key) |
| 114 | data = json.dumps(jsonData) |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 115 | print "url, data..", url, data |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 116 | 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] | 117 | print "requests.codes.....",requests.codes.created |
| 118 | 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] | 119 | return passed |
| 120 | |
| 121 | def ApiGet(self, key, urlSuffix=""): |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 122 | url = self.getURL(key) + str(urlSuffix) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 123 | print "get url...",url |
| 124 | resp = requests.get(url, auth=(self.user, self.password)) |
| 125 | passed = self.checkResult(resp, requests.codes.ok) |
| 126 | if not passed: |
| 127 | return None |
| 128 | else: |
| 129 | return resp.json() |
| 130 | |
| 131 | def ApiChameleonGet(self, key, urlSuffix=""): |
| 132 | url = self.getURL(key) + "/" + str(urlSuffix) |
| 133 | print "get url...",url |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 134 | resp = requests.get(url, auth=(self.user, self.password)) |
| 135 | passed = self.checkResult(resp, requests.codes.ok) |
| 136 | if not passed: |
| 137 | return None |
| 138 | else: |
| 139 | return resp.json() |
| 140 | |
| 141 | def ApiPut(self, key, jsonData, urlSuffix=""): |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 142 | print "urlSuffix....",type(urlSuffix) |
| 143 | url = self.getURL(key) + str(urlSuffix) + "/" |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 144 | data = json.dumps(jsonData) |
| 145 | resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password)) |
| 146 | passed = self.checkResult(resp, requests.codes.ok) |
| 147 | return passed |
| 148 | |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 149 | def ApiChameleonPut(self, key, jsonData, urlSuffix=""): |
| 150 | print "urlSuffix....",type(urlSuffix) |
| 151 | url = self.getURL(key) + "/" + str(urlSuffix) |
| 152 | print "url", url |
| 153 | data = json.dumps(jsonData) |
| 154 | resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password)) |
| 155 | passed = self.checkResult(resp, requests.codes.ok) |
| 156 | return passed |
| 157 | |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 158 | def ApiDelete(self, key, urlSuffix=""): |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 159 | url = self.getURL(key) + str(urlSuffix) |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 160 | print "url",url |
You Wang | 2e97a01 | 2016-10-21 16:09:52 -0700 | [diff] [blame] | 161 | resp = requests.delete(url, auth=(self.user, self.password)) |
You Wang | 853c025 | 2017-05-17 13:28:31 -0700 | [diff] [blame] | 162 | passed = self.checkResult(resp, requests.codes.no_content) |
Suchitra Vemuri | f7410a9 | 2017-05-16 17:04:05 -0700 | [diff] [blame] | 163 | return passed |
| 164 | |
| 165 | def ApiChameleonDelete(self, key, urlSuffix=""): |
| 166 | url = self.getURL(key) + "/" + str(urlSuffix) |
| 167 | print "url",url |
| 168 | resp = requests.delete(url, auth=(self.user, self.password)) |
| 169 | #passed = self.checkResult(resp, requests.codes.no_content) |
| 170 | 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] | 171 | return passed |
| 172 | |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 173 | #test |
| 174 | ''' |
Suchitra Vemuri | 9008144 | 2017-12-20 16:10:15 -0800 | [diff] [blame] | 175 | test = restApi("MCORD_RestApiProperties.py") |
| 176 | print test.getURL("CORE_INSTANCES") |
A R Karthick | 028edaf | 2017-11-06 16:34:57 -0800 | [diff] [blame] | 177 | |
Suchitra Vemuri | daeb247 | 2017-08-15 11:58:21 -0700 | [diff] [blame] | 178 | ''' |