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=")
32 #TBD: Detailed validation
36 #Creates the specified number of persons based on People yang model
39 # To enable RPC a non-user input person entry is created with personId=user0
42 def addPerson(hostname,port,numberOfPersons):
43 #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
44 if(numberOfPersons==0):
45 strId =str(numberOfPersons)
46 payload = SettingsLibrary.add_person_payload_template.substitute(personId="user"+strId,gender="unknown",age=0,
47 address=strId + "Way, Some Country, Some Zip "+strId,
48 contactNo= "some number"+strId)
49 # Send the POST request using RESTCONF
50 resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddPersonUrl(hostname,port),"admin", "admin",payload)
54 for x in range(1, numberOfPersons+1):
55 if(genderToggle == "Male"):
56 genderToggle = "Female"
62 payload = SettingsLibrary.add_person_rpc_payload_template.substitute(personId="user"+strId,gender=genderToggle,age=(20+x%100),
63 address=strId + "Way, Some Country, Some Zip "+str(x%1000),
64 contactNo= "some number"+strId)
65 # Send the POST request using RPC
66 resp = UtilLibrary.post(SettingsLibrary.getAddPersonRpcUrl(hostname,port),"admin", "admin",payload)
68 print("payload formed after template substitution=")
70 print("the response of the POST to add person=")
76 #TBD: Detailed validation
78 #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 def addCarPerson(hostname,port,numberOfCarPersons):
86 #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
87 if(numberOfCarPersons==0):
88 payload = SettingsLibrary.add_car_person_template.substitute(Id=str(numberOfCarPersons),personId="user"+str(numberOfCarPersons))
89 # Send the POST request REST CONF
90 resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddCarPersonUrl(hostname,port),"admin", "admin",payload)
94 for x in range(1, numberOfCarPersons+1):
97 payload = SettingsLibrary.add_car_person_template.substitute(Id=strId,personId="user"+strId)
99 # Send the POST request REST CONF
100 resp = UtilLibrary.post(SettingsLibrary.getAddCarPersonUrl(hostname,port),"admin", "admin",payload)
102 print("payload formed after template substitution=")
105 print("the response of the POST to add car_person=")
108 print("getting the car_persons for verification")
109 resp=getCarPersonMappings(hostname,port,0)
111 #TBD detailed validation
114 # Invokes an RPC REST call that does a car purchase by a person id
116 # It is expected that the Car and Person entries are already created
117 # before invoking this method
121 def buyCar(hostname,port,numberOfCarBuyers):
122 for x in range(1, numberOfCarBuyers+1):
125 payload = SettingsLibrary.buy_car_rpc_template.substitute(personId="user"+strId,carId=strId)
127 # Send the POST request using RPC
128 resp = UtilLibrary.post(SettingsLibrary.getBuyCarRpcUrl(hostname,port),"admin", "admin",payload)
130 print("payload formed after template substitution=")
133 print("the response of the POST to buycar=")
136 print("getting the car_persons for verification")
137 resp=getCarPersonMappings(hostname,port,0)
141 # Uses the GET on car:cars resource
142 # to get all cars in the store using RESTCONF
145 def getCars(hostname,port,ignore):
146 resp = UtilLibrary.get(SettingsLibrary.getCarsUrl(hostname,port),"admin", "admin")
147 resp.encoding = 'utf-8'
152 # Uses the GET on people:people resource
153 # to get all persons in the store using RESTCONF
155 #This also returns the dummy entry created for routed RPC
156 # with personId being user0
160 def getPersons(hostname,port,ignore):
161 resp = UtilLibrary.get(SettingsLibrary.getPersonsUrl(hostname,port),"admin","admin")
162 resp.encoding = 'utf-8'
167 # Uses the GET on car-people:car-people resource
168 # to get all car-persons entry in the store using RESTCONF
170 #This also returns the dummy entry created for routed RPC
171 # with personId being user0
174 def getCarPersonMappings(hostname,port,ignore):
175 resp = UtilLibrary.get(SettingsLibrary.getCarPersonUrl(hostname,port),"admin","admin")
176 resp.encoding = 'utf-8'
182 #delete all cars in the store using RESTCONF
185 def deleteAllCars(hostname,port,ignore):
186 UtilLibrary.delete(SettingsLibrary.getCarsUrl(hostname,port),"admin","admin")
187 resp = getCars(hostname,port,ignore)
188 print("Cars in store after deletion:"+ str(resp))
191 #delete all persons in the store using RESTCONF
194 def deleteAllPersons(hostname,port,ignore):
195 UtilLibrary.delete(SettingsLibrary.getPersonsUrl(hostname,port),"admin","admin")
196 resp = getPersons(hostname,port,ignore)
197 print("Persons in store after deletion:"+ str(resp))
201 #delete all car -poeple s in the store using RESTCONF
204 def deleteAllCarsPersons(hostname,port,ignore):
205 UtilLibrary.delete(SettingsLibrary.getCarPersonsUrl(hostname,port),"admin","admin")
206 resp = getPersons(hostname,port,ignore)
207 print("Persons in store after deletion:"+ str(resp))
212 def testlongevity(inputtime,port,*ips):
213 max_time = int(inputtime)
214 start_time = time.time() # remember when we started
215 while (time.time() - start_time) < max_time:
217 deleteAllCars(ip,port,0)
218 resp = getCars(ip,port,0)
219 if resp.status_code == 404:
220 print("Pass: no cars found after deletion")
222 print("Fail: Cars are present after deletion")
223 deleteAllPersons(ip,port,0)
224 resp = getPersons(ip,port,0)
225 if resp.status_code == 404:
226 print("Pass: no person found after deletion")
228 print("Fail: people are present after deletion")
232 resp = getCars(ip,port,0)
233 if resp.status_code == 200:
234 print("Pass: car data available after addition")
235 if resp.content.find("manufacturer100") == -1:
236 print("Fail: last car is not there")
238 print("Pass: car data matches")
240 print("Fail: car addition failed")
242 addPerson(ip,port,100)
244 resp = getPersons(ip,port,0)
245 if resp.status_code == 200:
246 print("Pass: people data available after addition")
247 if resp.content.find("user100") == -1:
248 print("Fail: last person is not there")
250 print("Pass: person data matches")
252 print("Fail: person addition failed")
254 addCarPerson(ip,port,0)
257 resp = getCarPersonMappings(ip,port,0)
258 if resp.status_code == 200:
259 print("Pass: car person data available after addition")
260 if resp.content.find("user100") == -1:
261 print("Fail: last car person is not there")
263 print("Pass: car person data matches")
265 print("Fail: car person addition failed")
266 time.sleep(60) # sleep before next host starts working
270 # Usage message shown to user
275 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' \
276 'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
278 param = '\n\t<param> is\n\t\t' \
279 'number of cars to be added if <command>=ac\n\t\t' \
280 'number of persons to be added if <command>=ap\n\t\t' \
281 'number of car buyers if <command>=bc\n\t\t'\
282 'pass 0 if <command>=gc or gp or gcp or dc or dp'\
285 usageString = 'usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address' \
286 '\n\t<command> = any of the following commands \n\t\t'
288 usageString = usageString + command +param
294 # entry point for command executions
298 if len(sys.argv) < 4:
301 SettingsLibrary.hostname = sys.argv[1]
302 SettingsLibrary.port = '8181'
303 call = dict(ac=addCar, ap=addPerson, bc=buyCar,
304 gc=getCars, gp=getPersons, gcp=getCarPersonMappings,dc=deleteAllCars,dp=deleteAllPersons)
306 #FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
307 addPerson(SettingsLibrary.hostname,SettingsLibrary.port,0)
309 #FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
310 addCarPerson(SettingsLibrary.hostname,SettingsLibrary.port,0)
313 call[sys.argv[2]](SettingsLibrary.hostname,SettingsLibrary.port,int(sys.argv[3]))
317 if __name__ == "__main__":