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(numberOfCars):
17 for x in range(1, numberOfCars+1):
19 payload = settings.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 = util.post(settings.getAddCarUrl(),"admin", "admin",payload)
27 print("the response of the POST to add car=")
30 print("getting the cars in store ")
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(numberOfPersons):
44 #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
45 if(numberOfPersons==0):
46 strId =str(numberOfPersons)
47 payload = settings.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 = util.nonprintpost(settings.getAddPersonUrl(),"admin", "admin",payload)
55 for x in range(1, numberOfPersons+1):
56 if(genderToggle == "Male"):
57 genderToggle = "Female"
63 payload = settings.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 = util.post(settings.getAddPersonRpcUrl(),"admin", "admin",payload)
69 print("payload formed after template substitution=")
71 print("the response of the POST to add person=")
74 print("getting the persons for verification")
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(numberOfCarPersons):
87 #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
88 if(numberOfCarPersons==0):
89 payload = settings.add_car_person_template.substitute(Id=str(numberOfCarPersons),personId="user"+str(numberOfCarPersons))
90 # Send the POST request REST CONF
91 resp = util.nonprintpost(settings.getAddCarPersonUrl(),"admin", "admin",payload)
95 for x in range(1, numberOfCarPersons+1):
98 payload = settings.add_car_person_template.substitute(Id=strId,personId="user"+strId)
100 # Send the POST request REST CONF
101 resp = util.post(settings.getAddCarPersonUrl(),"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(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(numberOfCarBuyers):
123 for x in range(1, numberOfCarBuyers+1):
126 payload = settings.buy_car_rpc_template.substitute(personId="user"+strId,carId=strId)
128 # Send the POST request using RPC
129 resp = util.post(settings.getBuyCarRpcUrl(),"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(0)
142 # Uses the GET on car:cars resource
143 # to get all cars in the store using RESTCONF
147 resp = util.get(settings.getCarsUrl(),"admin", "admin")
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(ignore):
161 resp = util.get(settings.getPersonsUrl(),"admin","admin")
166 # Uses the GET on car-people:car-people resource
167 # to get all car-persons entry in the store using RESTCONF
169 #This also returns the dummy entry created for routed RPC
170 # with personId being user0
173 def getCarPersonMappings(ignore):
174 resp = util.get(settings.getCarPersonUrl(),"admin","admin")
179 #delete all cars in the store using RESTCONF
182 def deleteAllCars(ignore):
183 util.delete(settings.getCarsUrl(),"admin","admin")
184 resp = getCars(ignore)
185 print("Cars in store after deletion:"+ str(resp))
188 #delete all persons in the store using RESTCONF
191 def deleteAllPersons(ignore):
192 util.delete(settings.getPersonsUrl(),"admin","admin")
193 resp = getPersons(ignore)
194 print("Persons in store after deletion:"+ str(resp))
197 # Usage message shown to user
202 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' \
203 'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
205 param = '\n\t<param> is\n\t\t' \
206 'number of cars to be added if <command>=ac\n\t\t' \
207 'number of persons to be added if <command>=ap\n\t\t' \
208 'number of car buyers if <command>=bc\n\t\t'\
209 'pass 0 if <command>=gc or gp or gcp or dc or dp'\
212 usageString = 'usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address' \
213 '\n\t<command> = any of the following commands \n\t\t'
215 usageString = usageString + command +param
221 # entry point for command executions
225 if len(sys.argv) < 4:
228 settings.hostname = sys.argv[1]
229 settings.port = '8080'
230 call = dict(ac=addCar, ap=addPerson, bc=buyCar,
231 gc=getCars, gp=getPersons, gcp=getCarPersonMappings,dc=deleteAllCars,dp=deleteAllPersons)
233 #FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
236 #FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
240 call[sys.argv[2]](int(sys.argv[3]))