1 __author__ = "Basheeruddin Ahmed"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "syedbahm@cisco.com"
11 #Creates the specified number of cars based on Cars yang model
15 def addCar(hostname,port,numberOfCars):
17 for x in range(1, numberOfCars+1):
19 payload = SettingsLibrary.add_car_payload_template.substitute(id=strId,category="category"+strId,model="model"+strId,
20 manufacturer="manufacturer"+strId,
22 print("payload formed after template substitution=")
24 # Send the POST request
25 resp = UtilLibrary.post(SettingsLibrary.getAddCarUrl(hostname,port),"admin", "admin",payload)
27 print("the response of the POST to add car=")
30 time.sleep(5) # Let the add finish
33 #TBD: Detailed validation
37 #Creates the specified number of persons based on People yang model
40 # To enable RPC a non-user input person entry is created with personId=user0
43 def addPerson(hostname,port,numberOfPersons):
44 #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
45 if(numberOfPersons==0):
46 strId =str(numberOfPersons)
47 payload = SettingsLibrary.add_person_payload_template.substitute(personId="user"+strId,gender="unknown",age=0,
48 address=strId + "Way, Some Country, Some Zip "+strId,
49 contactNo= "some number"+strId)
50 # Send the POST request using RESTCONF
51 resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddPersonUrl(hostname,port),"admin", "admin",payload)
55 for x in range(1, numberOfPersons+1):
56 if(genderToggle == "Male"):
57 genderToggle = "Female"
63 payload = SettingsLibrary.add_person_rpc_payload_template.substitute(personId="user"+strId,gender=genderToggle,age=(20+x%100),
64 address=strId + "Way, Some Country, Some Zip "+str(x%1000),
65 contactNo= "some number"+strId)
66 # Send the POST request using RPC
67 resp = UtilLibrary.post(SettingsLibrary.getAddPersonRpcUrl(hostname,port),"admin", "admin",payload)
69 print("payload formed after template substitution=")
71 print("the response of the POST to add person=")
77 #TBD: Detailed validation
79 #This method is not exposed via commands as only getCarPersons is of interest
80 #addCarPerson entry happens when buyCar is called
82 # To enable RPC a non-user input car-person entry is created with personId=user0
85 def addCarPerson(hostname,port,numberOfCarPersons):
87 #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
88 if(numberOfCarPersons==0):
89 payload = SettingsLibrary.add_car_person_template.substitute(Id=str(numberOfCarPersons),personId="user"+str(numberOfCarPersons))
90 # Send the POST request REST CONF
91 resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddCarPersonUrl(hostname,port),"admin", "admin",payload)
95 for x in range(1, numberOfCarPersons+1):
98 payload = SettingsLibrary.add_car_person_template.substitute(Id=strId,personId="user"+strId)
100 # Send the POST request REST CONF
101 resp = UtilLibrary.post(SettingsLibrary.getAddCarPersonUrl(hostname,port),"admin", "admin",payload)
103 print("payload formed after template substitution=")
106 print("the response of the POST to add car_person=")
109 print("getting the car_persons for verification")
110 resp=getCarPersonMappings(hostname,port,0)
112 #TBD detailed validation
115 # 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
122 def buyCar(hostname,port,numberOfCarBuyers):
123 for x in range(1, numberOfCarBuyers+1):
126 payload = SettingsLibrary.buy_car_rpc_template.substitute(personId="user"+strId,carId=strId)
128 # Send the POST request using RPC
129 resp = UtilLibrary.post(SettingsLibrary.getBuyCarRpcUrl(hostname,port),"admin", "admin",payload)
131 print("payload formed after template substitution=")
134 print("the response of the POST to buycar=")
137 print("getting the car_persons for verification")
138 resp=getCarPersonMappings(hostname,port,0)
142 # Uses the GET on car:cars resource
143 # to get all cars in the store using RESTCONF
146 def getCars(hostname,port,ignore):
147 resp = UtilLibrary.get(SettingsLibrary.getCarsUrl(hostname,port),"admin", "admin")
148 resp.encoding = 'utf-8'
153 # Uses the GET on people:people resource
154 # to get all persons in the store using RESTCONF
156 #This also returns the dummy entry created for routed RPC
157 # with personId being user0
161 def getPersons(hostname,port,ignore):
162 resp = UtilLibrary.get(SettingsLibrary.getPersonsUrl(hostname,port),"admin","admin")
163 resp.encoding = 'utf-8'
168 # Uses the GET on car-people:car-people resource
169 # to get all car-persons entry in the store using RESTCONF
171 #This also returns the dummy entry created for routed RPC
172 # with personId being user0
175 def getCarPersonMappings(hostname,port,ignore):
176 resp = UtilLibrary.get(SettingsLibrary.getCarPersonUrl(hostname,port),"admin","admin")
177 resp.encoding = 'utf-8'
183 #delete all cars in the store using RESTCONF
186 def deleteAllCars(hostname,port,ignore):
187 UtilLibrary.delete(SettingsLibrary.getCarsUrl(hostname,port),"admin","admin")
188 resp = getCars(hostname,port,ignore)
189 print("Cars in store after deletion:"+ str(resp))
192 #delete all persons in the store using RESTCONF
195 def deleteAllPersons(hostname,port,ignore):
196 UtilLibrary.delete(SettingsLibrary.getPersonsUrl(hostname,port),"admin","admin")
197 resp = getPersons(hostname,port,ignore)
198 print("Persons in store after deletion:"+ str(resp))
202 #delete all car -poeple s in the store using RESTCONF
205 def deleteAllCarsPersons(hostname,port,ignore):
206 UtilLibrary.delete(SettingsLibrary.getCarPersonUrl(hostname,port),"admin","admin")
207 resp = getPersons(hostname,port,ignore)
208 print("Persons in store after deletion:"+ str(resp))
213 def testlongevity(inputtime,port,*ips):
214 max_time = int(inputtime)
215 start_time = time.time() # remember when we started
216 while (time.time() - start_time) < max_time:
218 deleteAllCars(ip,port,0)
219 resp = getCars(ip,port,0)
220 if resp.status_code == 404:
221 print("Pass: no cars found after deletion")
223 print("Fail: Cars are present after deletion")
224 deleteAllPersons(ip,port,0)
225 resp = getPersons(ip,port,0)
226 if resp.status_code == 404:
227 print("Pass: no person found after deletion")
229 print("Fail: people are present after deletion")
233 resp = getCars(ip,port,0)
234 if resp.status_code == 200:
235 print("Pass: car data available after addition")
236 if resp.content.find("manufacturer100") == -1:
237 print("Fail: last car is not there")
239 print("Pass: car data matches")
241 print("Fail: car addition failed")
243 addPerson(ip,port,100)
245 resp = getPersons(ip,port,0)
246 if resp.status_code == 200:
247 print("Pass: people data available after addition")
248 if resp.content.find("user100") == -1:
249 print("Fail: last person is not there")
251 print("Pass: person data matches")
253 print("Fail: person addition failed")
255 addCarPerson(ip,port,0)
258 resp = getCarPersonMappings(ip,port,0)
259 if resp.status_code == 200:
260 print("Pass: car person data available after addition")
261 if resp.content.find("user100") == -1:
262 print("Fail: last car person is not there")
264 print("Pass: car person data matches")
266 print("Fail: car person addition failed")
267 time.sleep(60) # sleep before next host starts working
271 # Usage message shown to user
276 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' \
277 'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
279 param = '\n\t<param> is\n\t\t' \
280 'number of cars to be added if <command>=ac\n\t\t' \
281 'number of persons to be added if <command>=ap\n\t\t' \
282 'number of car buyers if <command>=bc\n\t\t'\
283 'pass 0 if <command>=gc or gp or gcp or dc or dp'\
286 usageString = 'usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address' \
287 '\n\t<command> = any of the following commands \n\t\t'
289 usageString = usageString + command +param
295 # entry point for command executions
299 if len(sys.argv) < 4:
302 SettingsLibrary.hostname = sys.argv[1]
303 SettingsLibrary.port = '8181'
304 call = dict(ac=addCar, ap=addPerson, bc=buyCar,
305 gc=getCars, gp=getPersons, gcp=getCarPersonMappings,dc=deleteAllCars,dp=deleteAllPersons)
307 #FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
308 addPerson(SettingsLibrary.hostname,SettingsLibrary.port,0)
310 #FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
311 addCarPerson(SettingsLibrary.hostname,SettingsLibrary.port,0)
314 call[sys.argv[2]](SettingsLibrary.hostname,SettingsLibrary.port,int(sys.argv[3]))
318 if __name__ == "__main__":