blob: ba2de9102ac2f9fd648ddd5b897b2d889e2dd04b [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
121 def ApiGet(self, key, urlSuffix=""):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700122 url = self.getURL(key) + str(urlSuffix)
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700123 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 Wang2e97a012016-10-21 16:09:52 -0700134 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.Vemuri32e03c22016-11-03 11:57:53 -0700142 print "urlSuffix....",type(urlSuffix)
143 url = self.getURL(key) + str(urlSuffix) + "/"
You Wang2e97a012016-10-21 16:09:52 -0700144 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 Vemurif7410a92017-05-16 17:04:05 -0700149 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 Wang2e97a012016-10-21 16:09:52 -0700158 def ApiDelete(self, key, urlSuffix=""):
Suchitra.Vemuri32e03c22016-11-03 11:57:53 -0700159 url = self.getURL(key) + str(urlSuffix)
Suchitra.Vemuri8be18802016-11-16 16:59:54 -0800160 print "url",url
You Wang2e97a012016-10-21 16:09:52 -0700161 resp = requests.delete(url, auth=(self.user, self.password))
You Wang853c0252017-05-17 13:28:31 -0700162 passed = self.checkResult(resp, requests.codes.no_content)
Suchitra Vemurif7410a92017-05-16 17:04:05 -0700163 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 Wang2e97a012016-10-21 16:09:52 -0700171 return passed
172
Suchitra.Vemuri85220062016-10-25 10:44:11 -0700173#test
174'''
Suchitra Vemuri90081442017-12-20 16:10:15 -0800175test = restApi("MCORD_RestApiProperties.py")
176print test.getURL("CORE_INSTANCES")
A R Karthick028edaf2017-11-06 16:34:57 -0800177
Suchitra Vemuridaeb2472017-08-15 11:58:21 -0700178'''