Reformat '* On Session' keywords (aaa)
[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(
214         SettingsLibrary.getCarsUrl(hostname, port) + "?content=config", "admin", "admin"
215     )
216     resp.encoding = "utf-8"
217     print(resp.text)
218     return resp
219
220
221 def getPersons(hostname, port, ignore):
222     """Uses the GET on people:people resource to get all persons in the store using RESTCONF
223
224     <note>
225         This also returns the dummy entry created for routed RPC
226         with personId being user0
227     </note>
228     """
229     resp = UtilLibrary.get(
230         SettingsLibrary.getPersonsUrl(hostname, port) + "?content=config",
231         "admin",
232         "admin",
233     )
234     resp.encoding = "utf-8"
235     print(resp.text)
236     return resp
237
238
239 def getCarPersonMappings(hostname, port, ignore):
240     """Uses the GET on car-people:car-people resource
241
242     to get all car-persons entry in the store using RESTCONF
243     <note>
244         This also returns the dummy entry created for routed RPC
245         with personId being user0
246     </note>
247     """
248     resp = UtilLibrary.get(
249         SettingsLibrary.getCarPersonUrl(hostname, port) + "?content=config",
250         "admin",
251         "admin",
252     )
253     resp.encoding = "utf-8"
254     print(resp)
255
256     return resp
257
258
259 def deleteAllCars(hostname, port, ignore):
260     """delete all cars in the store using RESTCONF"""
261     UtilLibrary.delete(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin")
262     resp = getCars(hostname, port, ignore)
263     print("Cars in store after deletion:" + str(resp))
264
265
266 def deleteAllPersons(hostname, port, ignore):
267     """delete all persons in the store using RESTCONF"""
268     UtilLibrary.delete(SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin")
269     resp = getPersons(hostname, port, ignore)
270     print("Persons in store after deletion:" + str(resp))
271
272
273 def deleteAllCarsPersons(hostname, port, ignore):
274     """delete all car -poeple s in the store using RESTCONF"""
275     UtilLibrary.delete(
276         SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin"
277     )
278     resp = getPersons(hostname, port, ignore)
279     print("Persons in store after deletion:" + str(resp))
280
281
282 def testlongevity(inputtime, port, *ips):
283     """Write longevity"""
284     max_time = int(inputtime)
285     start_time = time.time()  # remember when we started
286     while (time.time() - start_time) < max_time:
287         for ip in ips:
288             deleteAllCars(ip, port, 0)
289             resp = getCars(ip, port, 0)
290             if resp.status_code == 404:
291                 print("Pass: no cars found after deletion")
292             else:
293                 print("Fail: Cars are present after deletion")
294             deleteAllPersons(ip, port, 0)
295             resp = getPersons(ip, port, 0)
296             if resp.status_code == 404:
297                 print("Pass: no person found after deletion")
298             else:
299                 print("Fail: people are present after deletion")
300
301             addCar(ip, port, 100)
302             time.sleep(20)
303             resp = getCars(ip, port, 0)
304             if resp.status_code == 200:
305                 print("Pass: car data available after addition")
306                 if resp.text.find("manufacturer100") == -1:
307                     print("Fail: last car is not there")
308                 else:
309                     print("Pass: car data matches")
310             else:
311                 print("Fail: car addition failed")
312             addPerson(ip, port, 0)
313             addPerson(ip, port, 100)
314             time.sleep(20)
315             resp = getPersons(ip, port, 0)
316             if resp.status_code == 200:
317                 print("Pass: people data available after addition")
318                 if resp.text.find("user100") == -1:
319                     print("Fail: last person is not there")
320                 else:
321                     print("Pass: person data matches")
322             else:
323                 print("Fail: person addition failed")
324
325             addCarPerson(ip, port, 0)
326             buyCar(ip, port, 100)
327             time.sleep(20)
328             resp = getCarPersonMappings(ip, port, 0)
329             if resp.status_code == 200:
330                 print("Pass: car person data available after addition")
331                 if resp.text.find("user100") == -1:
332                     print("Fail: last car person is not there")
333                 else:
334                     print("Pass: car person data matches")
335             else:
336                 print("Fail: car person addition failed")
337             time.sleep(60)  # sleep before next host starts working
338
339
340 #
341 # Usage message shown to user
342 #
343
344
345 def options():
346
347     command = (
348         "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"
349         "gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)"
350     )
351
352     param = (
353         "\n\t<param> is\n\t\t"
354         "number of cars to be added if <command>=ac\n\t\t"
355         "number of persons to be added if <command>=ap\n\t\t"
356         "number of car buyers if <command>=bc\n\t\t"
357         "pass 0 if <command>=gc or gp or gcp or dc or dp"
358     )
359
360     usageString = (
361         "usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address"
362         "\n\t<command> = any of the following commands \n\t\t"
363     )
364
365     usageString = usageString + command + param
366
367     print(usageString)
368
369
370 #
371 # entry point for command executions
372 #
373
374
375 def main():
376     if len(sys.argv) < 4:
377         options()
378         quit(0)
379     SettingsLibrary.hostname = sys.argv[1]
380     SettingsLibrary.port = "8181"
381     call = dict(
382         ac=addCar,
383         ap=addPerson,
384         bc=buyCar,
385         gc=getCars,
386         gp=getPersons,
387         gcp=getCarPersonMappings,
388         dc=deleteAllCars,
389         dp=deleteAllPersons,
390     )
391
392     # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
393     addPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
394
395     # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
396     addCarPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
397
398     call[sys.argv[2]](SettingsLibrary.hostname, SettingsLibrary.port, int(sys.argv[3]))
399
400
401 #
402 # main invoked
403 if __name__ == "__main__":
404     main()