1 __author__ = "Basheeruddin Ahmed"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "syedbahm@cisco.com"
10 def addCar(numberOfCars):
11 """Creates the specified number of cars based on Cars yang model using RESTCONF"""
12 for x in range(1, numberOfCars+1):
14 payload = settings.add_car_payload_template.substitute(
15 id=strId, category="category" + strId, model="model" + strId,
16 manufacturer="manufacturer" + strId,
17 year=(2000 + x % 100))
18 print("payload formed after template substitution=")
20 # Send the POST request
21 resp = util.post(settings.getAddCarUrl(), "admin", "admin", payload)
23 print("the response of the POST to add car=")
26 print("getting the cars in store ")
29 # TBD: Detailed validation
32 def addPerson(numberOfPersons):
33 """Creates the specified number of persons based on People yang model using main RPC
36 To enable RPC a non-user input person entry is created with personId=user0
39 # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
40 if numberOfPersons == 0:
41 strId = str(numberOfPersons)
42 payload = settings.add_person_payload_template.substitute(
43 personId="user" + strId, gender="unknown", age=0,
44 address=strId + "Way, Some Country, Some Zip " + strId,
45 contactNo="some number" + strId)
46 # Send the POST request using RESTCONF
47 resp = util.nonprintpost(settings.getAddPersonUrl(), "admin", "admin", payload)
51 for x in range(1, numberOfPersons+1):
52 if(genderToggle == "Male"):
53 genderToggle = "Female"
59 payload = settings.add_person_rpc_payload_template.substitute(
60 personId="user" + strId, gender=genderToggle, age=(20 + x % 100),
61 address=strId + "Way, Some Country, Some Zip " + str(x % 1000),
62 contactNo="some number" + strId)
63 # Send the POST request using RPC
64 resp = util.post(settings.getAddPersonRpcUrl(), "admin", "admin", payload)
66 print("payload formed after template substitution=")
68 print("the response of the POST to add person=")
71 print("getting the persons for verification")
74 # TBD: Detailed validation
77 def addCarPerson(numberOfCarPersons):
78 """This method is not exposed via commands as only getCarPersons is of interest
80 addCarPerson entry happens when buyCar is called
83 To enable RPC a non-user input car-person entry is created with personId=user0
86 # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
87 if numberOfCarPersons == 0:
88 payload = settings.add_car_person_template.substitute(
89 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 def buyCar(numberOfCarBuyers):
116 """Invokes an RPC REST call that does a car purchase by a person id
119 It is expected that the Car and Person entries are already created
120 before invoking this method
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 to get all cars in the store using RESTCONF"""
143 resp = util.get(settings.getCarsUrl(), "admin", "admin")
148 def getPersons(ignore):
149 """Uses the GET on people:people resource to get all persons in the store using RESTCONF
152 This also returns the dummy entry created for routed RPC
153 with personId being user0
156 resp = util.get(settings.getPersonsUrl(), "admin", "admin")
161 def getCarPersonMappings(ignore):
162 """Uses the GET on car-people:car-people resource to get all car-persons entry in the store using RESTCONF
165 This also returns the dummy entry created for routed RPC
166 with personId being user0
169 resp = util.get(settings.getCarPersonUrl(), "admin", "admin")
174 def deleteAllCars(ignore):
175 """delete all cars in the store using RESTCONF"""
176 util.delete(settings.getCarsUrl(), "admin", "admin")
177 resp = getCars(ignore)
178 print("Cars in store after deletion:" + str(resp))
181 def deleteAllPersons(ignore):
182 """delete all persons in the store using RESTCONF"""
183 util.delete(settings.getPersonsUrl(), "admin", "admin")
184 resp = getPersons(ignore)
185 print("Persons in store after deletion:" + str(resp))
189 # Usage message shown to user
194 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' \
195 'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
197 param = '\n\t<param> is\n\t\t' \
198 'number of cars to be added if <command>=ac\n\t\t' \
199 'number of persons to be added if <command>=ap\n\t\t' \
200 'number of car buyers if <command>=bc\n\t\t'\
201 'pass 0 if <command>=gc or gp or gcp or dc or dp'\
203 usageString = 'usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address' \
204 '\n\t<command> = any of the following commands \n\t\t'
206 usageString = usageString + command + param
212 # entry point for command executions
216 if len(sys.argv) < 4:
219 settings.hostname = sys.argv[1]
220 settings.port = '8080'
221 call = dict(ac=addCar, ap=addPerson, bc=buyCar,
222 gc=getCars, gp=getPersons, gcp=getCarPersonMappings, dc=deleteAllCars, dp=deleteAllPersons)
224 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
227 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
230 call[sys.argv[2]](int(sys.argv[3]))