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(
18 id=strId, category="category" + strId, model="model" + strId,
19 manufacturer="manufacturer" + strId,
20 year=(2000 + x % 100))
21 print("Initialization payload=")
23 resp = UtilLibrary.post(SettingsLibrary.getAddCarInitUrl(hostname, port), "admin", "admin", payload)
24 print("the response of the POST to add car=")
29 def addCar(hostname, port, numberOfCars, *expected):
30 """Creates the specified number of cars based on Cars yang model using RESTCONF"""
31 for x in range(1, numberOfCars + 1):
33 payload = SettingsLibrary.add_car_payload_template.substitute(
34 id=strId, category="category" + strId, model="model" + strId,
35 manufacturer="manufacturer" + strId,
36 year=(2000 + x % 100))
37 print("payload formed after template substitution=")
39 # Send the POST request
40 resp = UtilLibrary.post(SettingsLibrary.getAddCarUrl(hostname, port), "admin", "admin", payload)
42 print("the response of the POST to add car=")
44 if expected and str(resp.status_code) not in expected:
45 raise RuntimeError('Add car failed for {}:{} with status {}'.
46 format(hostname, port, resp.status_code))
50 # TBD: Detailed validation
53 def addPerson(hostname, port, numberOfPersons, *expected):
54 """Creates the specified number of persons based on People yang model using main RPC
56 To enable RPC a non-user input person entry is created with personId=user0
59 # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
60 if (numberOfPersons == 0):
61 strId = str(numberOfPersons)
62 payload = SettingsLibrary.add_person_payload_template.substitute(
63 personId="user" + strId, gender="unknown", age=0,
64 address=strId + "Way, Some Country, Some Zip " + strId,
65 contactNo="some number" + strId)
66 # Send the POST request using RESTCONF
67 resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddPersonUrl(hostname, port), "admin", "admin", payload)
71 for x in range(1, numberOfPersons + 1):
72 if(genderToggle == "Male"):
73 genderToggle = "Female"
79 payload = SettingsLibrary.add_person_rpc_payload_template.substitute(
80 personId="user" + strId, gender=genderToggle, age=(20 + x % 100),
81 address=strId + "Way, Some Country, Some Zip " + str(x % 1000),
82 contactNo="some number" + strId)
83 # Send the POST request using RPC
84 resp = UtilLibrary.post(SettingsLibrary.getAddPersonRpcUrl(hostname, port), "admin", "admin", payload)
86 print("payload formed after template substitution=")
88 print("the response of the POST to add person=")
90 if expected and str(resp.status_code) not in expected:
91 raise RuntimeError('Add person failed for {}:{} with status {}'.
92 format(hostname, port, resp.status_code))
96 # TBD: Detailed validation
99 def addCarPerson(hostname, port, numberOfCarPersons):
100 """This method is not exposed via commands as only getCarPersons is of interest
102 addCarPerson entry happens when buyCar is called
104 To enable RPC a non-user input car-person entry is created with personId=user0
107 # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
108 if (numberOfCarPersons == 0):
109 payload = SettingsLibrary.add_car_person_template.substitute(
110 Id=str(numberOfCarPersons), personId="user" + str(numberOfCarPersons))
111 # Send the POST request REST CONF
112 resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddCarPersonUrl(hostname, port), "admin", "admin", payload)
116 for x in range(1, numberOfCarPersons + 1):
119 payload = SettingsLibrary.add_car_person_template.substitute(Id=strId, personId="user" + strId)
121 # Send the POST request REST CONF
122 resp = UtilLibrary.post(SettingsLibrary.getAddCarPersonUrl(hostname, port), "admin", "admin", payload)
124 print("payload formed after template substitution=")
127 print("the response of the POST to add car_person=")
130 print("getting the car_persons for verification")
131 resp = getCarPersonMappings(hostname, port, 0)
132 # TBD detailed validation
136 def buyCar(hostname, port, numberOfCarBuyers, start=0):
137 """Invokes an RPC REST call that does a car purchase by a person id
140 It is expected that the Car and Person entries are already created
141 before invoking this method
145 print "Buying " + str(numberOfCarBuyers) + " Cars"
146 for x in range(start, start + numberOfCarBuyers):
149 payload = SettingsLibrary.buy_car_rpc_template.substitute(personId="user" + strId, carId=strId)
151 # Send the POST request using RPC
152 resp = UtilLibrary.post(SettingsLibrary.getBuyCarRpcUrl(hostname, port), "admin", "admin", payload)
157 if (resp.status_code != 200):
158 raise RuntimeError('Buy car failed for {}:{} with status {}'.
159 format(hostname, port, resp.status_code))
162 def getCars(hostname, port, ignore):
163 """Uses the GET on car:cars resource to get all cars in the store using RESTCONF"""
164 resp = UtilLibrary.get(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin")
165 resp.encoding = 'utf-8'
170 def getPersons(hostname, port, ignore):
171 """Uses the GET on people:people resource to get all persons in the store using RESTCONF
174 This also returns the dummy entry created for routed RPC
175 with personId being user0
178 resp = UtilLibrary.get(SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin")
179 resp.encoding = 'utf-8'
184 def getCarPersonMappings(hostname, port, ignore):
185 """Uses the GET on car-people:car-people resource
187 to get all car-persons entry in the store using RESTCONF
189 This also returns the dummy entry created for routed RPC
190 with personId being user0
193 resp = UtilLibrary.get(SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin")
194 resp.encoding = 'utf-8'
200 def deleteAllCars(hostname, port, ignore):
201 """delete all cars in the store using RESTCONF"""
202 UtilLibrary.delete(SettingsLibrary.getCarsUrl(hostname, port), "admin", "admin")
203 resp = getCars(hostname, port, ignore)
204 print("Cars in store after deletion:" + str(resp))
207 def deleteAllPersons(hostname, port, ignore):
208 """delete all persons in the store using RESTCONF"""
209 UtilLibrary.delete(SettingsLibrary.getPersonsUrl(hostname, port), "admin", "admin")
210 resp = getPersons(hostname, port, ignore)
211 print("Persons in store after deletion:" + str(resp))
214 def deleteAllCarsPersons(hostname, port, ignore):
215 """delete all car -poeple s in the store using RESTCONF"""
216 UtilLibrary.delete(SettingsLibrary.getCarPersonUrl(hostname, port), "admin", "admin")
217 resp = getPersons(hostname, port, ignore)
218 print("Persons in store after deletion:" + str(resp))
221 def testlongevity(inputtime, port, *ips):
222 """Write longevity"""
223 max_time = int(inputtime)
224 start_time = time.time() # remember when we started
225 while (time.time() - start_time) < max_time:
227 deleteAllCars(ip, port, 0)
228 resp = getCars(ip, port, 0)
229 if resp.status_code == 404:
230 print("Pass: no cars found after deletion")
232 print("Fail: Cars are present after deletion")
233 deleteAllPersons(ip, port, 0)
234 resp = getPersons(ip, port, 0)
235 if resp.status_code == 404:
236 print("Pass: no person found after deletion")
238 print("Fail: people are present after deletion")
240 addCar(ip, port, 100)
242 resp = getCars(ip, port, 0)
243 if resp.status_code == 200:
244 print("Pass: car data available after addition")
245 if resp.content.find("manufacturer100") == -1:
246 print("Fail: last car is not there")
248 print("Pass: car data matches")
250 print("Fail: car addition failed")
251 addPerson(ip, port, 0)
252 addPerson(ip, port, 100)
254 resp = getPersons(ip, port, 0)
255 if resp.status_code == 200:
256 print("Pass: people data available after addition")
257 if resp.content.find("user100") == -1:
258 print("Fail: last person is not there")
260 print("Pass: person data matches")
262 print("Fail: person addition failed")
264 addCarPerson(ip, port, 0)
265 buyCar(ip, port, 100)
267 resp = getCarPersonMappings(ip, port, 0)
268 if resp.status_code == 200:
269 print("Pass: car person data available after addition")
270 if resp.content.find("user100") == -1:
271 print("Fail: last car person is not there")
273 print("Pass: car person data matches")
275 print("Fail: car person addition failed")
276 time.sleep(60) # sleep before next host starts working
280 # Usage message shown to user
285 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' \
286 'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
288 param = '\n\t<param> is\n\t\t' \
289 'number of cars to be added if <command>=ac\n\t\t' \
290 'number of persons to be added if <command>=ap\n\t\t' \
291 'number of car buyers if <command>=bc\n\t\t'\
292 'pass 0 if <command>=gc or gp or gcp or dc or dp'\
295 usageString = 'usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address' \
296 '\n\t<command> = any of the following commands \n\t\t'
298 usageString = usageString + command + param
304 # entry point for command executions
308 if len(sys.argv) < 4:
311 SettingsLibrary.hostname = sys.argv[1]
312 SettingsLibrary.port = '8181'
313 call = dict(ac=addCar, ap=addPerson, bc=buyCar,
314 gc=getCars, gp=getPersons, gcp=getCarPersonMappings, dc=deleteAllCars, dp=deleteAllPersons)
316 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
317 addPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
319 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
320 addCarPerson(SettingsLibrary.hostname, SettingsLibrary.port, 0)
322 call[sys.argv[2]](SettingsLibrary.hostname, SettingsLibrary.port, int(sys.argv[3]))
327 if __name__ == "__main__":