1 __author__ = "Basheeruddin Ahmed"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "syedbahm@cisco.com"
11 def initCar(hostname, port):
12 """Initiales the car shard"""
15 payload = SettingsLibrary.add_car_init_payload_template.substitute(
16 id=strId, category="category" + strId, model="model" + strId,
17 manufacturer="manufacturer" + strId,
18 year=(2000 + x % 100))
19 print("Initialization payload=")
21 resp = UtilLibrary.post(SettingsLibrary.getAddCarInitUrl(hostname, port), "admin", "admin", payload)
22 print("the response of the POST to add car=")
27 def addCar(hostname, port, numberOfCars, *expected):
28 """Creates the specified number of cars based on Cars yang model using RESTCONF"""
29 for x in range(1, numberOfCars + 1):
31 payload = SettingsLibrary.add_car_payload_template.substitute(
32 id=strId, category="category" + strId, model="model" + strId,
33 manufacturer="manufacturer" + strId,
34 year=(2000 + x % 100))
35 print("payload formed after template substitution=")
37 # Send the POST request
38 resp = UtilLibrary.post(SettingsLibrary.getAddCarUrl(hostname, port), "admin", "admin", payload)
40 print("the response of the POST to add car=")
42 if expected and str(resp.status_code) not in expected:
43 raise RuntimeError('Add car failed for {}:{} with status {}'.
44 format(hostname, port, resp.status_code))
48 # TBD: Detailed validation
51 def addPerson(hostname, port, numberOfPersons, *expected):
52 """Creates the specified number of persons based on People yang model using main RPC
54 To enable RPC a non-user input person entry is created with personId=user0
57 # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
58 if (numberOfPersons == 0):
59 strId = str(numberOfPersons)
60 payload = SettingsLibrary.add_person_payload_template.substitute(
61 personId="user" + strId, gender="unknown", age=0,
62 address=strId + "Way, Some Country, Some Zip " + strId,
63 contactNo="some number" + strId)
64 # Send the POST request using RESTCONF
65 resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddPersonUrl(hostname, port), "admin", "admin", payload)
69 for x in range(1, numberOfPersons+1):
70 if(genderToggle == "Male"):
71 genderToggle = "Female"
77 payload = SettingsLibrary.add_person_rpc_payload_template.substitute(
78 personId="user" + strId, gender=genderToggle, age=(20 + x % 100),
79 address=strId + "Way, Some Country, Some Zip " + str(x % 1000),
80 contactNo= "some number" + strId)
81 # Send the POST request using RPC
82 resp = UtilLibrary.post(SettingsLibrary.getAddPersonRpcUrl(hostname, port), "admin", "admin", payload)
84 print("payload formed after template substitution=")
86 print("the response of the POST to add person=")
88 if expected and str(resp.status_code) not in expected:
89 raise RuntimeError('Add person failed for {}:{} with status {}'.
90 format(hostname, port, resp.status_code))
94 # TBD: Detailed validation
97 def addCarPerson(hostname, port, numberOfCarPersons):
98 """This method is not exposed via commands as only getCarPersons is of interest
100 addCarPerson entry happens when buyCar is called
102 To enable RPC a non-user input car-person entry is created with personId=user0
105 # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
106 if (numberOfCarPersons == 0):
107 payload = SettingsLibrary.add_car_person_template.substitute(
108 Id=str(numberOfCarPersons), personId="user" + str(numberOfCarPersons))
109 # Send the POST request REST CONF
110 resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddCarPersonUrl(hostname, port), "admin", "admin", payload)
114 for x in range(1, numberOfCarPersons+1):
117 payload = SettingsLibrary.add_car_person_template.substitute(Id=strId, personId="user" + strId)
119 # Send the POST request REST CONF
120 resp = UtilLibrary.post(SettingsLibrary.getAddCarPersonUrl(hostname, port), "admin", "admin", payload)
122 print("payload formed after template substitution=")
125 print("the response of the POST to add car_person=")
128 print("getting the car_persons for verification")
129 resp = getCarPersonMappings(hostname, port, 0)
130 # TBD detailed validation
134 def buyCar(hostname, port, numberOfCarBuyers, start=0):
135 """Invokes an RPC REST call that does a car purchase by a person id
138 It is expected that the Car and Person entries are already created
139 before invoking this method
143 print "Buying " + str(numberOfCarBuyers) + " Cars"
144 for x in range(start, start+numberOfCarBuyers):
147 payload = SettingsLibrary.buy_car_rpc_template.substitute(personId="user" + strId, carId=strId)
149 # Send the POST request using RPC
150 resp = UtilLibrary.post(SettingsLibrary.getBuyCarRpcUrl(hostname, port), "admin", "admin", payload)
155 if (resp.status_code != 200):
156 raise RuntimeError('Buy car failed for {}:{} with status {}'.
157 format(hostname, port, resp.status_code))
160 def getCars(hostname, port, ignore):
161 """Uses the GET on car:cars resource to get all cars in the store using RESTCONF"""
162 resp = UtilLibrary.get(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin")
163 resp.encoding = 'utf-8'
168 def getPersons(hostname, port, ignore):
169 """Uses the GET on people:people resource to get all persons in the store using RESTCONF
172 This also returns the dummy entry created for routed RPC
173 with personId being user0
176 resp = UtilLibrary.get(SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin")
177 resp.encoding = 'utf-8'
182 def getCarPersonMappings(hostname, port, ignore):
183 """Uses the GET on car-people:car-people resource
185 to get all car-persons entry in the store using RESTCONF
187 This also returns the dummy entry created for routed RPC
188 with personId being user0
191 resp = UtilLibrary.get(SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin")
192 resp.encoding = 'utf-8'
198 def deleteAllCars(hostname, port, ignore):
199 """delete all cars in the store using RESTCONF"""
200 UtilLibrary.delete(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin")
201 resp = getCars(hostname, port, ignore)
202 print("Cars in store after deletion:" + str(resp))
205 def deleteAllPersons(hostname, port, ignore):
206 """delete all persons in the store using RESTCONF"""
207 UtilLibrary.delete(SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin")
208 resp = getPersons(hostname, port, ignore)
209 print("Persons in store after deletion:" + str(resp))
212 def deleteAllCarsPersons(hostname, port, ignore):
213 """delete all car -poeple s in the store using RESTCONF"""
214 UtilLibrary.delete(SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin")
215 resp = getPersons(hostname, port, ignore)
216 print("Persons in store after deletion:" + str(resp))
219 def testlongevity(inputtime, port, *ips):
220 """Write longevity"""
221 max_time = int(inputtime)
222 start_time = time.time() # remember when we started
223 while (time.time() - start_time) < max_time:
225 deleteAllCars(ip, port, 0)
226 resp = getCars(ip, port, 0)
227 if resp.status_code == 404:
228 print("Pass: no cars found after deletion")
230 print("Fail: Cars are present after deletion")
231 deleteAllPersons(ip, port, 0)
232 resp = getPersons(ip, port, 0)
233 if resp.status_code == 404:
234 print("Pass: no person found after deletion")
236 print("Fail: people are present after deletion")
238 addCar(ip, port, 100)
240 resp = getCars(ip, port, 0)
241 if resp.status_code == 200:
242 print("Pass: car data available after addition")
243 if resp.content.find("manufacturer100") == -1:
244 print("Fail: last car is not there")
246 print("Pass: car data matches")
248 print("Fail: car addition failed")
249 addPerson(ip, port, 0)
250 addPerson(ip, port, 100)
252 resp = getPersons(ip, port, 0)
253 if resp.status_code == 200:
254 print("Pass: people data available after addition")
255 if resp.content.find("user100") == -1:
256 print("Fail: last person is not there")
258 print("Pass: person data matches")
260 print("Fail: person addition failed")
262 addCarPerson(ip, port, 0)
263 buyCar(ip, port, 100)
265 resp = getCarPersonMappings(ip, port, 0)
266 if resp.status_code == 200:
267 print("Pass: car person data available after addition")
268 if resp.content.find("user100") == -1:
269 print("Fail: last car person is not there")
271 print("Pass: car person data matches")
273 print("Fail: car person addition failed")
274 time.sleep(60) # sleep before next host starts working
278 # Usage message shown to user
283 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' \
284 'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
286 param = '\n\t<param> is\n\t\t' \
287 'number of cars to be added if <command>=ac\n\t\t' \
288 'number of persons to be added if <command>=ap\n\t\t' \
289 'number of car buyers if <command>=bc\n\t\t'\
290 'pass 0 if <command>=gc or gp or gcp or dc or dp'\
293 usageString = 'usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address' \
294 '\n\t<command> = any of the following commands \n\t\t'
296 usageString = usageString + command + param
302 # entry point for command executions
306 if len(sys.argv) < 4:
309 SettingsLibrary.hostname = sys.argv[1]
310 SettingsLibrary.port = '8181'
311 call = dict(ac=addCar, ap=addPerson, bc=buyCar,
312 gc=getCars, gp=getPersons, gcp=getCarPersonMappings, dc=deleteAllCars, dp=deleteAllPersons)
314 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
315 addPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
317 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
318 addCarPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
320 call[sys.argv[2]](SettingsLibrary.hostname, SettingsLibrary.port, int(sys.argv[3]))
324 if __name__ == "__main__":