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