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