1 __author__ = "Basheeruddin Ahmed"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "syedbahm@cisco.com"
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):
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=")
21 # Send the POST request
22 resp = UtilLibrary.post(SettingsLibrary.getAddCarUrl(hostname, port), "admin", "admin", payload)
24 print("the response of the POST to add car=")
27 time.sleep(5) # Let the add finish
30 # TBD: Detailed validation
33 def addPerson(hostname, port, numberOfPersons):
34 """Creates the specified number of persons based on People yang model using main RPC
36 To enable RPC a non-user input person entry is created with personId=user0
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)
51 for x in range(1, numberOfPersons+1):
52 if(genderToggle == "Male"):
53 genderToggle = "Female"
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)
66 print("payload formed after template substitution=")
68 print("the response of the POST to add person=")
73 # TBD: Detailed validation
76 def addCarPerson(hostname, port, numberOfCarPersons):
77 """This method is not exposed via commands as only getCarPersons is of interest
79 addCarPerson entry happens when buyCar is called
81 To enable RPC a non-user input car-person entry is created with personId=user0
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)
93 for x in range(1, numberOfCarPersons+1):
96 payload = SettingsLibrary.add_car_person_template.substitute(Id=strId, personId="user" + strId)
98 # Send the POST request REST CONF
99 resp = UtilLibrary.post(SettingsLibrary.getAddCarPersonUrl(hostname, port), "admin", "admin", payload)
101 print("payload formed after template substitution=")
104 print("the response of the POST to add car_person=")
107 print("getting the car_persons for verification")
108 resp = getCarPersonMappings(hostname, port, 0)
110 # TBD detailed validation
113 def buyCar(hostname, port, numberOfCarBuyers, start=0):
114 """Invokes an RPC REST call that does a car purchase by a person id
117 It is expected that the Car and Person entries are already created
118 before invoking this method
121 for x in range(start, start+numberOfCarBuyers):
124 payload = SettingsLibrary.buy_car_rpc_template.substitute(personId="user" + strId, carId=strId)
126 # Send the POST request using RPC
127 resp = UtilLibrary.post(SettingsLibrary.getBuyCarRpcUrl(hostname, port), "admin", "admin", payload)
132 if (resp.status_code != 204):
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'
146 def getPersons(hostname, port, ignore):
147 """Uses the GET on people:people resource to get all persons in the store using RESTCONF
150 This also returns the dummy entry created for routed RPC
151 with personId being user0
154 resp = UtilLibrary.get(SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin")
155 resp.encoding = 'utf-8'
160 def getCarPersonMappings(hostname, port, ignore):
161 """Uses the GET on car-people:car-people resource
163 to get all car-persons entry in the store using RESTCONF
165 This also returns the dummy entry created for routed RPC
166 with personId being user0
169 resp = UtilLibrary.get(SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin")
170 resp.encoding = 'utf-8'
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))
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))
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))
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:
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")
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")
214 print("Fail: people are present after deletion")
216 addCar(ip, port, 100)
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")
224 print("Pass: car data matches")
226 print("Fail: car addition failed")
227 addPerson(ip, port, 0)
228 addPerson(ip, port, 100)
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")
236 print("Pass: person data matches")
238 print("Fail: person addition failed")
240 addCarPerson(ip, port, 0)
241 buyCar(ip, port, 100)
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")
249 print("Pass: car person data matches")
251 print("Fail: car person addition failed")
252 time.sleep(60) # sleep before next host starts working
256 # Usage message shown to user
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)'
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'\
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'
274 usageString = usageString + command + param
280 # entry point for command executions
284 if len(sys.argv) < 4:
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)
292 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
293 addPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
295 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
296 addCarPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
298 call[sys.argv[2]](SettingsLibrary.hostname, SettingsLibrary.port, int(sys.argv[3]))
302 if __name__ == "__main__":