blob: cbd157c22682de6d44f1e8df9be6154bcbc895eb [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 '''
Suchitra Vemuri90081442017-12-20 16:10:15 -080027 def __init__(self, propertyFile="RestApiProperties.py"):
28 self.rp = readProperties(os.path.abspath(os.path.dirname(__file__))+"/../Properties/"+ propertyFile)
You Wang2e97a012016-10-21 16:09:52 -070029 self.controllerIP = self.getValueFromProperties("SERVER_IP")
30 self.controllerPort = self.getValueFromProperties("SERVER_PORT")
Kailash Khalasi68f9f632018-04-11 08:09:19 -070031 self.user = self.getValueFromProperties("XOS_USER")
32 self.password = self.getValueFromProperties("XOS_PASSWD")
You Wang2e97a012016-10-21 16:09:52 -070033 self.jsonHeader = {'Content-Type': 'application/json'}
34
35 def getValueFromProperties(self, key):
36 '''
37 Get and return values from properties file
38 '''
Andy Bavier85ec6b92018-04-09 15:55:55 -070039 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 Wang2e97a012016-10-21 16:09:52 -070049 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.Vemuri85220062016-10-25 10:44:11 -070070 '''
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 Wang2e97a012016-10-21 16:09:52 -070078
Suchitra.Vemuri85220062016-10-25 10:44:11 -070079 def getSubscriberId(self, jsonDataList, accountNum):
You Wang2e97a012016-10-21 16:09:52 -070080 '''
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 Karthick028edaf2017-11-06 16:34:57 -080088 if jsonData["service_specific_id"] == str(accountNum):
You Wang2e97a012016-10-21 16:09:52 -070089 subscriberId = jsonData["id"]
90 break
Suchitra.Vemuri85220062016-10-25 10:44:11 -070091 return str(subscriberId)
You Wang2e97a012016-10-21 16:09:52 -070092 except KeyError:
93 print "Something wrong with the json data provided: ", jsonData
94 return -1
Suchitra.Vemuri85220062016-10-25 10:44:11 -070095 '''
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 Wang2e97a012016-10-21 16:09:52 -0700111
112 def ApiPost(self, key, jsonData):
113 url = self.getURL(key)
114 data = json.dumps(jsonData)
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700115 print "url, data..", url, data
You Wang2e97a012016-10-21 16:09:52 -0700116 resp = requests.post(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password))
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700117 print "requests.codes.....",requests.codes.created
118 passed = self.checkResult(resp, requests.codes.created) or self.checkResult(resp, requests.codes.ok)
You Wang2e97a012016-10-21 16:09:52 -0700119 return passed
120
Suchitra Vemuri7dbf03c2018-04-25 23:06:23 -0700121 def ApiPostReturnJson(self, key, jsonData):
122 url = self.getURL(key)
123 data = json.dumps(jsonData)
124 print "url, data..", url, data
125 resp = requests.post(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password))
126 print "requests.codes.....",requests.codes.created
127 print "posted data...", resp.json()
128 passed = self.checkResult(resp, requests.codes.created) or self.checkResult(resp, requests.codes.ok)
129 return passed, resp.json()
130
You Wang2e97a012016-10-21 16:09:52 -0700131 def ApiGet(self, key, urlSuffix=""):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700132 url = self.getURL(key) + str(urlSuffix)
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700133 print "get url...",url
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 ApiChameleonGet(self, key, urlSuffix=""):
142 url = self.getURL(key) + "/" + str(urlSuffix)
143 print "get url...",url
You Wang2e97a012016-10-21 16:09:52 -0700144 resp = requests.get(url, auth=(self.user, self.password))
145 passed = self.checkResult(resp, requests.codes.ok)
146 if not passed:
147 return None
148 else:
149 return resp.json()
150
151 def ApiPut(self, key, jsonData, urlSuffix=""):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700152 print "urlSuffix....",type(urlSuffix)
153 url = self.getURL(key) + str(urlSuffix) + "/"
You Wang2e97a012016-10-21 16:09:52 -0700154 data = json.dumps(jsonData)
155 resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password))
156 passed = self.checkResult(resp, requests.codes.ok)
157 return passed
158
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700159 def ApiChameleonPut(self, key, jsonData, urlSuffix=""):
160 print "urlSuffix....",type(urlSuffix)
161 url = self.getURL(key) + "/" + str(urlSuffix)
162 print "url", url
163 data = json.dumps(jsonData)
164 resp = requests.put(url, data=data, headers=self.jsonHeader, auth=(self.user, self.password))
165 passed = self.checkResult(resp, requests.codes.ok)
166 return passed
167
You Wang2e97a012016-10-21 16:09:52 -0700168 def ApiDelete(self, key, urlSuffix=""):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700169 url = self.getURL(key) + str(urlSuffix)
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800170 print "url",url
You Wang2e97a012016-10-21 16:09:52 -0700171 resp = requests.delete(url, auth=(self.user, self.password))
You Wang853c0252017-05-17 13:28:31 -0700172 passed = self.checkResult(resp, requests.codes.no_content)
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700173 return passed
174
175 def ApiChameleonDelete(self, key, urlSuffix=""):
176 url = self.getURL(key) + "/" + str(urlSuffix)
177 print "url",url
178 resp = requests.delete(url, auth=(self.user, self.password))
179 #passed = self.checkResult(resp, requests.codes.no_content)
180 passed = self.checkResult(resp, requests.codes.created) or self.checkResult(resp, requests.codes.ok)
You Wang2e97a012016-10-21 16:09:52 -0700181 return passed
182
Suchitra.Vemuri85220062016-10-25 10:44:11 -0700183#test
Suchitra Vemuri7dbf03c2018-04-25 23:06:23 -0700184#'''
Suchitra Vemuri90081442017-12-20 16:10:15 -0800185test = restApi("MCORD_RestApiProperties.py")
186print test.getURL("CORE_INSTANCES")
A R Karthick028edaf2017-11-06 16:34:57 -0800187
Suchitra Vemuri7dbf03c2018-04-25 23:06:23 -0700188test = restApi()
189voltdevice = {
190 "name" : "volt-10",
191 "device_type" : "asf_olt",
192 "host" : "172.17.0.1",
193 "port" : 50060,
194 "switch_port" : "5",
195 "outer_tpid" : "0x8100",
196 "volt_service_id" : 7
197 }
198#result = test.ApiPost("VOLT_DEVICE",voltdevice)
199dele = test.ApiChameleonDelete("VOLT_DEVICE",13)
200dele = test.ApiChameleonDelete("VOLT_DEVICE",14)
201dele = test.ApiChameleonDelete("VOLT_DEVICE",15)
202#'''