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