blob: c93f1219e448369ed2c929337f0a8d1128177ca4 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
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 Wang2e97a012016-10-21 16:09:52 -070017#!/usr/bin/env python
Suchitra.Vemuri85220062016-10-25 10:44:11 -070018import requests, json, os, sys, time
19#sys.path.append('common-utils')
20sys.path.append(os.path.join(sys.path[0],'utils'))
You Wang2e97a012016-10-21 16:09:52 -070021from readProperties import readProperties
22
Suchitra.Vemuri85220062016-10-25 10:44:11 -070023class restApi(object):
You Wang2e97a012016-10-21 16:09:52 -070024 '''
25 Functions for testing CORD API with POST, GET, PUT, DELETE method
26 '''
27 def __init__(self):
You Wangd4dacd52017-02-16 10:58:53 -080028 self.rp = readProperties(os.path.abspath(os.path.dirname(__file__))+"/../Properties/RestApiProperties.py")
You Wang2e97a012016-10-21 16:09:52 -070029 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.Vemuri85220062016-10-25 10:44:11 -070062 '''
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 Wang2e97a012016-10-21 16:09:52 -070070
Suchitra.Vemuri85220062016-10-25 10:44:11 -070071 def getSubscriberId(self, jsonDataList, accountNum):
You Wang2e97a012016-10-21 16:09:52 -070072 '''
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:
80 if jsonData["identity"]["account_num"] == str(accountNum):
81 subscriberId = jsonData["id"]
82 break
Suchitra.Vemuri85220062016-10-25 10:44:11 -070083 return str(subscriberId)
You Wang2e97a012016-10-21 16:09:52 -070084 except KeyError:
85 print "Something wrong with the json data provided: ", jsonData
86 return -1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070087 '''
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 Wang2e97a012016-10-21 16:09:52 -0700103
104 def ApiPost(self, key, jsonData):
105 url = self.getURL(key)
106 data = json.dumps(jsonData)
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700107 print "url, data..", url, data
You Wang2e97a012016-10-21 16:09:52 -0700108 resp = requests.post(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password))
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700109 print "requests.codes.....",requests.codes.created
110 passed = self.checkResult(resp, requests.codes.created) or self.checkResult(resp, requests.codes.ok)
You Wang2e97a012016-10-21 16:09:52 -0700111 return passed
112
113 def ApiGet(self, key, urlSuffix=""):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700114 url = self.getURL(key) + str(urlSuffix)
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700115 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 Wang2e97a012016-10-21 16:09:52 -0700126 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.Vemuri32e03c22016-11-03 11:57:53 -0700134 print "urlSuffix....",type(urlSuffix)
135 url = self.getURL(key) + str(urlSuffix) + "/"
You Wang2e97a012016-10-21 16:09:52 -0700136 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 Vemurif7410a92017-05-16 17:04:05 -0700141 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 Wang2e97a012016-10-21 16:09:52 -0700150 def ApiDelete(self, key, urlSuffix=""):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700151 url = self.getURL(key) + str(urlSuffix)
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800152 print "url",url
You Wang2e97a012016-10-21 16:09:52 -0700153 resp = requests.delete(url, auth=(self.user, self.password))
You Wang853c0252017-05-17 13:28:31 -0700154 passed = self.checkResult(resp, requests.codes.no_content)
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700155 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 Wang2e97a012016-10-21 16:09:52 -0700163 return passed
164
Suchitra.Vemuri85220062016-10-25 10:44:11 -0700165#test
166'''
You Wang2e97a012016-10-21 16:09:52 -0700167if __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.Vemuri85220062016-10-25 10:44:11 -0700180'''
Suchitra Vemurif58de922017-06-14 12:53:42 -0700181#'''
Suchitra.Vemuri85220062016-10-25 10:44:11 -0700182test = restApi()
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800183#key = "UTILS_SYNCHRONIZER"
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800184#key = "CORE_USERS"
185#key2 = "UTILS_LOGIN"
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800186#key = "TENANT_SUBSCRIBER"
187#jsonGetData = test.ApiGet(key)
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800188#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 Vemurif7410a92017-05-16 17:04:05 -0700190key = "VOLT_TENANT"
Suchitra Vemurif58de922017-06-14 12:53:42 -0700191#key = "VOLT_SUBSCRIBER"
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700192#jsonResponse = test.ApiDelete(key,204)
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800193#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 Vemurif7410a92017-05-16 17:04:05 -0700197#key = "CORE_INSTANCES"
198#key1 = "CORE_SANITY_SLICES"
199#key2 = "CORE_SLICES"
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800200#input_dict = { "s_tag" : "111", "c_tag" : "222", "subscriber" : 23}
Suchitra.Vemuri75dffd42016-12-20 15:35:25 -0800201input_dict = {
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700202 "s_tag" : 117,
Suchitra Vemurif58de922017-06-14 12:53:42 -0700203 "c_tag" : 227,
204 "subscriber_root_id" : "16"
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700205 }
206
207#input_dict1 = { "name" : "mysite_Test1", "site" : 1 , "creator" : 1}
208input_dict2 = {
209
210 "cdn_enable": "true",
211 "uplink_speed": 1000000000,
212 "downlink_speed": 1000000000,
213 "enable_uverse": "true",
214 "status": "enabled",
215 "service_specific_id": "100",
216 "name": "My House"
217 }
218#jsonResponse = test.ApiPost(key,input_dict)
219#jsonResponse = test.ApiChameleonPut(key,input_dict,12)
Suchitra Vemurif58de922017-06-14 12:53:42 -0700220jsonGetData = test.ApiGet(key)
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700221#print "========="
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800222#print jsonGetData
Suchitra.Vemuri75dffd42016-12-20 15:35:25 -0800223#jsonEdit = test.ApiPut(key,{"c_tag" : "666","s_tag" : "123"},"30")
Suchitra Vemurif58de922017-06-14 12:53:42 -0700224#jsonO = test.ApiChameleonDelete(key,"56")
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700225#jsonResponse = test.ApiPut(key,{"identity":{"name":"My House 22"}},"71")
Suchitra Vemurif58de922017-06-14 12:53:42 -0700226#jsonResponse = test.ApiPost(key,input_dict)
Suchitra.Vemurid2035342016-11-22 17:44:40 -0800227#jsonResponse = test.ApiPut(key,{"name":"test1-changed"},"9")
Suchitra.Vemuri85220062016-10-25 10:44:11 -0700228print "========="
Suchitra Vemurif58de922017-06-14 12:53:42 -0700229#'''