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(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin")
214 resp.encoding = "utf-8"
219 def getPersons(hostname, port, ignore):
220 """Uses the GET on people:people resource to get all persons in the store using RESTCONF
223 This also returns the dummy entry created for routed RPC
224 with personId being user0
227 resp = UtilLibrary.get(
228 SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin"
230 resp.encoding = "utf-8"
235 def getCarPersonMappings(hostname, port, ignore):
236 """Uses the GET on car-people:car-people resource
238 to get all car-persons entry in the store using RESTCONF
240 This also returns the dummy entry created for routed RPC
241 with personId being user0
244 resp = UtilLibrary.get(
245 SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin"
247 resp.encoding = "utf-8"
253 def deleteAllCars(hostname, port, ignore):
254 """delete all cars in the store using RESTCONF"""
255 UtilLibrary.delete(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin")
256 resp = getCars(hostname, port, ignore)
257 print("Cars in store after deletion:" + str(resp))
260 def deleteAllPersons(hostname, port, ignore):
261 """delete all persons in the store using RESTCONF"""
262 UtilLibrary.delete(SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin")
263 resp = getPersons(hostname, port, ignore)
264 print("Persons in store after deletion:" + str(resp))
267 def deleteAllCarsPersons(hostname, port, ignore):
268 """delete all car -poeple s in the store using RESTCONF"""
270 SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin"
272 resp = getPersons(hostname, port, ignore)
273 print("Persons in store after deletion:" + str(resp))
276 def testlongevity(inputtime, port, *ips):
277 """Write longevity"""
278 max_time = int(inputtime)
279 start_time = time.time() # remember when we started
280 while (time.time() - start_time) < max_time:
282 deleteAllCars(ip, port, 0)
283 resp = getCars(ip, port, 0)
284 if resp.status_code == 404:
285 print("Pass: no cars found after deletion")
287 print("Fail: Cars are present after deletion")
288 deleteAllPersons(ip, port, 0)
289 resp = getPersons(ip, port, 0)
290 if resp.status_code == 404:
291 print("Pass: no person found after deletion")
293 print("Fail: people are present after deletion")
295 addCar(ip, port, 100)
297 resp = getCars(ip, port, 0)
298 if resp.status_code == 200:
299 print("Pass: car data available after addition")
300 if resp.text.find("manufacturer100") == -1:
301 print("Fail: last car is not there")
303 print("Pass: car data matches")
305 print("Fail: car addition failed")
306 addPerson(ip, port, 0)
307 addPerson(ip, port, 100)
309 resp = getPersons(ip, port, 0)
310 if resp.status_code == 200:
311 print("Pass: people data available after addition")
312 if resp.text.find("user100") == -1:
313 print("Fail: last person is not there")
315 print("Pass: person data matches")
317 print("Fail: person addition failed")
319 addCarPerson(ip, port, 0)
320 buyCar(ip, port, 100)
322 resp = getCarPersonMappings(ip, port, 0)
323 if resp.status_code == 200:
324 print("Pass: car person data available after addition")
325 if resp.text.find("user100") == -1:
326 print("Fail: last car person is not there")
328 print("Pass: car person data matches")
330 print("Fail: car person addition failed")
331 time.sleep(60) # sleep before next host starts working
335 # Usage message shown to user
342 "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"
343 "gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)"
347 "\n\t<param> is\n\t\t"
348 "number of cars to be added if <command>=ac\n\t\t"
349 "number of persons to be added if <command>=ap\n\t\t"
350 "number of car buyers if <command>=bc\n\t\t"
351 "pass 0 if <command>=gc or gp or gcp or dc or dp"
355 "usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address"
356 "\n\t<command> = any of the following commands \n\t\t"
359 usageString = usageString + command + param
365 # entry point for command executions
370 if len(sys.argv) < 4:
373 SettingsLibrary.hostname = sys.argv[1]
374 SettingsLibrary.port = "8181"
381 gcp=getCarPersonMappings,
386 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
387 addPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
389 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
390 addCarPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
392 call[sys.argv[2]](SettingsLibrary.hostname, SettingsLibrary.port, int(sys.argv[3]))
397 if __name__ == "__main__":