Make pep8 more picky
[integration/test.git] / csit / libraries / CrudLibrary.py
1 import sys
2 import UtilLibrary
3 import SettingsLibrary
4 import time
5
6
7 __author__ = "Basheeruddin Ahmed"
8 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
9 __license__ = "New-style BSD"
10 __email__ = "syedbahm@cisco.com"
11
12
13 def initCar(hostname, port):
14     """Initiales the car shard"""
15     x = 0
16     strId = str(x)
17     payload = SettingsLibrary.add_car_init_payload_template.substitute(
18         id=strId, category="category" + strId, model="model" + strId,
19         manufacturer="manufacturer" + strId,
20         year=(2000 + x % 100))
21     print("Initialization payload=")
22     print(payload)
23     resp = UtilLibrary.post(SettingsLibrary.getAddCarInitUrl(hostname, port), "admin", "admin", payload)
24     print("the response of the POST to add car=")
25     print(resp)
26     return resp
27
28
29 def addCar(hostname, port, numberOfCars, *expected):
30     """Creates the specified number of cars based on Cars yang model using RESTCONF"""
31     for x in range(1, numberOfCars + 1):
32         strId = str(x)
33         payload = SettingsLibrary.add_car_payload_template.substitute(
34             id=strId, category="category" + strId, model="model" + strId,
35             manufacturer="manufacturer" + strId,
36             year=(2000 + x % 100))
37         print("payload formed after template substitution=")
38         print(payload)
39         # Send the POST request
40         resp = UtilLibrary.post(SettingsLibrary.getAddCarUrl(hostname, port), "admin", "admin", payload)
41
42         print("the response of the POST to add car=")
43         print(resp)
44         if expected and str(resp.status_code) not in expected:
45             raise RuntimeError('Add car failed for {}:{} with status {}'.
46                                format(hostname, port, resp.status_code))
47
48     return resp
49
50     # TBD: Detailed validation
51
52
53 def addPerson(hostname, port, numberOfPersons, *expected):
54     """Creates the specified number of persons based on People yang model using main RPC
55     <note>
56         To enable RPC a non-user input person entry is created with personId=user0
57     </note>
58     """
59     # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
60     if (numberOfPersons == 0):
61         strId = str(numberOfPersons)
62         payload = SettingsLibrary.add_person_payload_template.substitute(
63             personId="user" + strId, gender="unknown", age=0,
64             address=strId + "Way, Some Country, Some Zip  " + strId,
65             contactNo="some number" + strId)
66         # Send the POST request using RESTCONF
67         resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddPersonUrl(hostname, port), "admin", "admin", payload)
68         return resp
69
70     genderToggle = "Male"
71     for x in range(1, numberOfPersons + 1):
72         if(genderToggle == "Male"):
73             genderToggle = "Female"
74         else:
75             genderToggle = "Male"
76
77         strId = str(x)
78
79         payload = SettingsLibrary.add_person_rpc_payload_template.substitute(
80             personId="user" + strId, gender=genderToggle, age=(20 + x % 100),
81             address=strId + "Way, Some Country, Some Zip  " + str(x % 1000),
82             contactNo="some number" + strId)
83         # Send the POST request using RPC
84         resp = UtilLibrary.post(SettingsLibrary.getAddPersonRpcUrl(hostname, port), "admin", "admin", payload)
85
86         print("payload formed after template substitution=")
87         print(payload)
88         print("the response of the POST to add person=")
89         print(resp)
90         if expected and str(resp.status_code) not in expected:
91             raise RuntimeError('Add person failed for {}:{} with status {}'.
92                                format(hostname, port, resp.status_code))
93
94     return resp
95
96     # TBD: Detailed validation
97
98
99 def addCarPerson(hostname, port, numberOfCarPersons):
100     """This method is not exposed via commands as only getCarPersons is of interest
101
102     addCarPerson entry happens when buyCar is called
103     <note>
104         To enable RPC a non-user input car-person entry is created with personId=user0
105     </note>
106     """
107     # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
108     if (numberOfCarPersons == 0):
109         payload = SettingsLibrary.add_car_person_template.substitute(
110             Id=str(numberOfCarPersons), personId="user" + str(numberOfCarPersons))
111         # Send the POST request REST CONF
112         resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddCarPersonUrl(hostname, port), "admin", "admin", payload)
113
114         return resp
115
116     for x in range(1, numberOfCarPersons + 1):
117         strId = str(x)
118
119         payload = SettingsLibrary.add_car_person_template.substitute(Id=strId, personId="user" + strId)
120
121         # Send the POST request REST CONF
122         resp = UtilLibrary.post(SettingsLibrary.getAddCarPersonUrl(hostname, port), "admin", "admin", payload)
123
124         print("payload formed after template substitution=")
125         print(payload)
126
127         print("the response of the POST to add car_person=")
128         print(resp)
129
130     print("getting the car_persons for verification")
131     resp = getCarPersonMappings(hostname, port, 0)
132     # TBD detailed validation
133     return resp
134
135
136 def buyCar(hostname, port, numberOfCarBuyers, start=0):
137     """Invokes an RPC REST call that does a car purchase by a person id
138
139     <note>
140         It is expected that the Car and Person entries are already created
141         before invoking this method
142     </note>
143     """
144
145     print "Buying " + str(numberOfCarBuyers) + " Cars"
146     for x in range(start, start + numberOfCarBuyers):
147         strId = str(x + 1)
148
149         payload = SettingsLibrary.buy_car_rpc_template.substitute(personId="user" + strId, carId=strId)
150
151         # Send the POST request using RPC
152         resp = UtilLibrary.post(SettingsLibrary.getBuyCarRpcUrl(hostname, port), "admin", "admin", payload)
153
154         print(resp)
155         print(resp.text)
156
157         if (resp.status_code != 200):
158             raise RuntimeError('Buy car failed for {}:{} with status {}'.
159                                format(hostname, port, resp.status_code))
160
161
162 def getCars(hostname, port, ignore):
163     """Uses the GET on car:cars resource to get all cars in the store using RESTCONF"""
164     resp = UtilLibrary.get(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin")
165     resp.encoding = 'utf-8'
166     print(resp.text)
167     return resp
168
169
170 def getPersons(hostname, port, ignore):
171     """Uses the GET on people:people resource to get all persons in the store using RESTCONF
172
173     <note>
174         This also returns the dummy entry created for routed RPC
175         with personId being user0
176     </note>
177     """
178     resp = UtilLibrary.get(SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin")
179     resp.encoding = 'utf-8'
180     print(resp.text)
181     return resp
182
183
184 def getCarPersonMappings(hostname, port, ignore):
185     """Uses the GET on car-people:car-people resource
186
187     to get all car-persons entry in the store using RESTCONF
188     <note>
189         This also returns the dummy entry created for routed RPC
190         with personId being user0
191     </note>
192     """
193     resp = UtilLibrary.get(SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin")
194     resp.encoding = 'utf-8'
195     print (resp)
196
197     return resp
198
199
200 def deleteAllCars(hostname, port, ignore):
201     """delete all cars in the store using RESTCONF"""
202     UtilLibrary.delete(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin")
203     resp = getCars(hostname, port, ignore)
204     print("Cars in store after deletion:" + str(resp))
205
206
207 def deleteAllPersons(hostname, port, ignore):
208     """delete all persons in the store using RESTCONF"""
209     UtilLibrary.delete(SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin")
210     resp = getPersons(hostname, port, ignore)
211     print("Persons in store after deletion:" + str(resp))
212
213
214 def deleteAllCarsPersons(hostname, port, ignore):
215     """delete all car -poeple s in the store using RESTCONF"""
216     UtilLibrary.delete(SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin")
217     resp = getPersons(hostname, port, ignore)
218     print("Persons in store after deletion:" + str(resp))
219
220
221 def testlongevity(inputtime, port, *ips):
222     """Write longevity"""
223     max_time = int(inputtime)
224     start_time = time.time()  # remember when we started
225     while (time.time() - start_time) < max_time:
226         for ip in ips:
227             deleteAllCars(ip, port, 0)
228             resp = getCars(ip, port, 0)
229             if resp.status_code == 404:
230                 print("Pass: no cars found after deletion")
231             else:
232                 print("Fail: Cars are present after deletion")
233             deleteAllPersons(ip, port, 0)
234             resp = getPersons(ip, port, 0)
235             if resp.status_code == 404:
236                 print("Pass: no person found after deletion")
237             else:
238                 print("Fail: people are present after deletion")
239
240             addCar(ip, port, 100)
241             time.sleep(20)
242             resp = getCars(ip, port, 0)
243             if resp.status_code == 200:
244                 print("Pass: car data available after addition")
245                 if resp.content.find("manufacturer100") == -1:
246                     print("Fail: last car is not there")
247                 else:
248                     print("Pass: car data matches")
249             else:
250                 print("Fail: car addition failed")
251             addPerson(ip, port, 0)
252             addPerson(ip, port, 100)
253             time.sleep(20)
254             resp = getPersons(ip, port, 0)
255             if resp.status_code == 200:
256                 print("Pass: people data available after addition")
257                 if resp.content.find("user100") == -1:
258                     print("Fail: last person is not there")
259                 else:
260                     print("Pass: person data matches")
261             else:
262                 print("Fail: person addition failed")
263
264             addCarPerson(ip, port, 0)
265             buyCar(ip, port, 100)
266             time.sleep(20)
267             resp = getCarPersonMappings(ip, port, 0)
268             if resp.status_code == 200:
269                 print("Pass: car person data available after addition")
270                 if resp.content.find("user100") == -1:
271                     print("Fail: last car person is not there")
272                 else:
273                     print("Pass: car person data matches")
274             else:
275                 print("Fail: car person addition failed")
276             time.sleep(60)    # sleep before next host starts working
277
278
279 #
280 # Usage message shown to user
281 #
282
283 def options():
284
285     command = 'ac=Add Car\n\t\tap=Add Person \n\t\tbc=Buy Car\n\t\tgc=Get Cars\n\t\tgp=Get Persons\n\t\t' \
286               'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
287
288     param = '\n\t<param> is\n\t\t' \
289             'number of cars to be added if <command>=ac\n\t\t' \
290             'number of persons to be added if <command>=ap\n\t\t' \
291             'number of car buyers if <command>=bc\n\t\t'\
292             'pass 0 if <command>=gc or gp or gcp or dc or dp'\
293
294
295     usageString = 'usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address' \
296                   '\n\t<command> = any of the following commands \n\t\t'
297
298     usageString = usageString + command + param
299
300     print (usageString)
301
302
303 #
304 # entry point for command executions
305 #
306
307 def main():
308     if len(sys.argv) < 4:
309         options()
310         quit(0)
311     SettingsLibrary.hostname = sys.argv[1]
312     SettingsLibrary.port = '8181'
313     call = dict(ac=addCar, ap=addPerson, bc=buyCar,
314                 gc=getCars, gp=getPersons, gcp=getCarPersonMappings, dc=deleteAllCars, dp=deleteAllPersons)
315
316     # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
317     addPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
318
319     # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
320     addCarPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
321
322     call[sys.argv[2]](SettingsLibrary.hostname, SettingsLibrary.port, int(sys.argv[3]))
323
324 #
325 # main invoked
326 if __name__ == "__main__":
327     main()