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 | ''' |
| 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 | ''' |
Suchitra Vemuri | 9008144 | 2017-12-20 16:10:15 -0800 | [diff] [blame] | 167 | test = restApi("MCORD_RestApiProperties.py") |
| 168 | print test.getURL("CORE_INSTANCES") |
A R Karthick | 028edaf | 2017-11-06 16:34:57 -0800 | [diff] [blame] | 169 | |
Suchitra Vemuri | daeb247 | 2017-08-15 11:58:21 -0700 | [diff] [blame] | 170 | ''' |