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