7 __author__ = "Basheeruddin Ahmed"
8 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
9 __license__ = "New-style BSD"
10 __email__ = "syedbahm@cisco.com"
13 def initCar(hostname, port):
14 """Initiales the car shard"""
17 payload = SettingsLibrary.add_car_init_payload_template.substitute(
19 category="category" + strId,
20 model="model" + strId,
21 manufacturer="manufacturer" + strId,
22 year=(2000 + x % 100),
24 print("Initialization payload=")
26 resp = UtilLibrary.post(
27 SettingsLibrary.getAddCarInitUrl(hostname, port), "admin", "admin", payload
29 print("the response of the POST to add car=")
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):
38 payload = SettingsLibrary.add_car_payload_template.substitute(
40 category="category" + strId,
41 model="model" + strId,
42 manufacturer="manufacturer" + strId,
43 year=(2000 + x % 100),
45 print("payload formed after template substitution=")
47 # Send the POST request
48 resp = UtilLibrary.post(
49 SettingsLibrary.getAddCarUrl(hostname, port), "admin", "admin", payload
52 print("the response of the POST to add car=")
54 if expected and str(resp.status_code) not in expected:
56 "Add car failed for {}:{} with status {}".format(
57 hostname, port, resp.status_code
63 # TBD: Detailed validation
66 def addPerson(hostname, port, numberOfPersons, *expected):
67 """Creates the specified number of persons based on People yang model using main RPC
69 To enable RPC a non-user input person entry is created with personId=user0
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,
79 address=strId + "Way, Some Country, Some Zip " + strId,
80 contactNo="some number" + strId,
82 # Send the POST request using RESTCONF
83 resp = UtilLibrary.nonprintpost(
84 SettingsLibrary.getAddPersonUrl(hostname, port), "admin", "admin", payload
89 for x in range(1, numberOfPersons + 1):
90 if genderToggle == "Male":
91 genderToggle = "Female"
97 payload = SettingsLibrary.add_person_rpc_payload_template.substitute(
98 personId="user" + strId,
101 address=strId + "Way, Some Country, Some Zip " + str(x % 1000),
102 contactNo="some number" + strId,
104 # Send the POST request using RPC
105 resp = UtilLibrary.post(
106 SettingsLibrary.getAddPersonRpcUrl(hostname, port),
112 print("payload formed after template substitution=")
114 print("the response of the POST to add person=")
116 if expected and str(resp.status_code) not in expected:
118 "Add person failed for {}:{} with status {}".format(
119 hostname, port, resp.status_code
125 # TBD: Detailed validation
128 def addCarPerson(hostname, port, numberOfCarPersons):
129 """This method is not exposed via commands as only getCarPersons is of interest
131 addCarPerson entry happens when buyCar is called
133 To enable RPC a non-user input car-person entry is created with personId=user0
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)
141 # Send the POST request REST CONF
142 resp = UtilLibrary.nonprintpost(
143 SettingsLibrary.getAddCarPersonUrl(hostname, port),
151 for x in range(1, numberOfCarPersons + 1):
154 payload = SettingsLibrary.add_car_person_template.substitute(
155 Id=strId, personId="user" + strId
158 # Send the POST request REST CONF
159 resp = UtilLibrary.post(
160 SettingsLibrary.getAddCarPersonUrl(hostname, port),
166 print("payload formed after template substitution=")
169 print("the response of the POST to add car_person=")
172 print("getting the car_persons for verification")
173 resp = getCarPersonMappings(hostname, port, 0)
174 # TBD detailed validation
178 def buyCar(hostname, port, numberOfCarBuyers, start=0):
179 """Invokes an RPC REST call that does a car purchase by a person id
182 It is expected that the Car and Person entries are already created
183 before invoking this method
187 print("Buying " + str(numberOfCarBuyers) + " Cars")
188 for x in range(start, start + numberOfCarBuyers):
191 payload = SettingsLibrary.buy_car_rpc_template.substitute(
192 personId="user" + strId, carId=strId
195 # Send the POST request using RPC
196 resp = UtilLibrary.post(
197 SettingsLibrary.getBuyCarRpcUrl(hostname, port), "admin", "admin", payload
203 if resp.status_code != 200:
205 "Buy car failed for {}:{} with status {}".format(
206 hostname, port, resp.status_code
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"
216 resp.encoding = "utf-8"
221 def getPersons(hostname, port, ignore):
222 """Uses the GET on people:people resource to get all persons in the store using RESTCONF
225 This also returns the dummy entry created for routed RPC
226 with personId being user0
229 resp = UtilLibrary.get(
230 SettingsLibrary.getPersonsUrl(hostname, port) + "?content=config",
234 resp.encoding = "utf-8"
239 def getCarPersonMappings(hostname, port, ignore):
240 """Uses the GET on car-people:car-people resource
242 to get all car-persons entry in the store using RESTCONF
244 This also returns the dummy entry created for routed RPC
245 with personId being user0
248 resp = UtilLibrary.get(
249 SettingsLibrary.getCarPersonUrl(hostname, port) + "?content=config",
253 resp.encoding = "utf-8"
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))
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))
273 def deleteAllCarsPersons(hostname, port, ignore):
274 """delete all car -poeple s in the store using RESTCONF"""
276 SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin"
278 resp = getPersons(hostname, port, ignore)
279 print("Persons in store after deletion:" + str(resp))
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:
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")
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")
299 print("Fail: people are present after deletion")
301 addCar(ip, port, 100)
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")
309 print("Pass: car data matches")
311 print("Fail: car addition failed")
312 addPerson(ip, port, 0)
313 addPerson(ip, port, 100)
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")
321 print("Pass: person data matches")
323 print("Fail: person addition failed")
325 addCarPerson(ip, port, 0)
326 buyCar(ip, port, 100)
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")
334 print("Pass: car person data matches")
336 print("Fail: car person addition failed")
337 time.sleep(60) # sleep before next host starts working
341 # Usage message shown to user
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)"
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"
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"
365 usageString = usageString + command + param
371 # entry point for command executions
376 if len(sys.argv) < 4:
379 SettingsLibrary.hostname = sys.argv[1]
380 SettingsLibrary.port = "8181"
387 gcp=getCarPersonMappings,
392 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
393 addPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
395 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
396 addCarPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
398 call[sys.argv[2]](SettingsLibrary.hostname, SettingsLibrary.port, int(sys.argv[3]))
403 if __name__ == "__main__":