6 __author__ = "Basheeruddin Ahmed"
7 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
8 __license__ = "New-style BSD"
9 __email__ = "syedbahm@cisco.com"
12 def addCar(numberOfCars):
13 """Creates the specified number of cars based on Cars yang model using RESTCONF"""
14 for x in range(1, numberOfCars + 1):
16 payload = settings.add_car_payload_template.substitute(
18 category="category" + strId,
19 model="model" + strId,
20 manufacturer="manufacturer" + strId,
21 year=(2000 + x % 100),
23 print("payload formed after template substitution=")
25 # Send the POST request
26 resp = util.post(settings.getAddCarUrl(), "admin", "admin", payload)
28 print("the response of the POST to add car=")
31 print("getting the cars in store ")
34 # TBD: Detailed validation
37 def addPerson(numberOfPersons):
38 """Creates the specified number of persons based on People yang model using main RPC
41 To enable RPC a non-user input person entry is created with personId=user0
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(
48 personId="user" + strId,
51 address=strId + "Way, Some Country, Some Zip " + strId,
52 contactNo="some number" + strId,
54 # Send the POST request using RESTCONF
55 resp = util.nonprintpost(settings.getAddPersonUrl(), "admin", "admin", payload)
59 for x in range(1, numberOfPersons + 1):
60 if genderToggle == "Male":
61 genderToggle = "Female"
67 payload = settings.add_person_rpc_payload_template.substitute(
68 personId="user" + strId,
71 address=strId + "Way, Some Country, Some Zip " + str(x % 1000),
72 contactNo="some number" + strId,
74 # Send the POST request using RPC
75 resp = util.post(settings.getAddPersonRpcUrl(), "admin", "admin", payload)
77 print("payload formed after template substitution=")
79 print("the response of the POST to add person=")
82 print("getting the persons for verification")
85 # TBD: Detailed validation
88 def addCarPerson(numberOfCarPersons):
89 """This method is not exposed via commands as only getCarPersons is of interest
91 addCarPerson entry happens when buyCar is called
94 To enable RPC a non-user input car-person entry is created with personId=user0
97 # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
98 if numberOfCarPersons == 0:
99 payload = settings.add_car_person_template.substitute(
100 Id=str(numberOfCarPersons), personId="user" + str(numberOfCarPersons)
102 # Send the POST request REST CONF
103 resp = util.nonprintpost(
104 settings.getAddCarPersonUrl(), "admin", "admin", payload
109 for x in range(1, numberOfCarPersons + 1):
112 payload = settings.add_car_person_template.substitute(
113 Id=strId, personId="user" + strId
116 # Send the POST request REST CONF
117 resp = util.post(settings.getAddCarPersonUrl(), "admin", "admin", payload)
119 print("payload formed after template substitution=")
122 print("the response of the POST to add car_person=")
125 print("getting the car_persons for verification")
126 resp = getCarPersonMappings(0)
128 # TBD detailed validation
131 def buyCar(numberOfCarBuyers):
132 """Invokes an RPC REST call that does a car purchase by a person id
135 It is expected that the Car and Person entries are already created
136 before invoking this method
139 for x in range(1, numberOfCarBuyers + 1):
142 payload = settings.buy_car_rpc_template.substitute(
143 personId="user" + strId, carId=strId
146 # Send the POST request using RPC
147 resp = util.post(settings.getBuyCarRpcUrl(), "admin", "admin", payload)
149 print("payload formed after template substitution=")
152 print("the response of the POST to buycar=")
155 print("getting the car_persons for verification")
156 resp = getCarPersonMappings(0)
160 """Uses the GET on car:cars resource to get all cars in the store using RESTCONF"""
161 resp = util.get(settings.getCarsUrl(), "admin", "admin")
166 def getPersons(ignore):
167 """Uses the GET on people:people resource to get all persons in the store using RESTCONF
170 This also returns the dummy entry created for routed RPC
171 with personId being user0
174 resp = util.get(settings.getPersonsUrl(), "admin", "admin")
179 def getCarPersonMappings(ignore):
180 """Uses the GET on car-people:car-people resource to get all car-persons entry in the store using RESTCONF
183 This also returns the dummy entry created for routed RPC
184 with personId being user0
187 resp = util.get(settings.getCarPersonUrl(), "admin", "admin")
192 def deleteAllCars(ignore):
193 """delete all cars in the store using RESTCONF"""
194 util.delete(settings.getCarsUrl(), "admin", "admin")
195 resp = getCars(ignore)
196 print("Cars in store after deletion:" + str(resp))
199 def deleteAllPersons(ignore):
200 """delete all persons in the store using RESTCONF"""
201 util.delete(settings.getPersonsUrl(), "admin", "admin")
202 resp = getPersons(ignore)
203 print("Persons in store after deletion:" + str(resp))
207 # Usage message shown to user
214 "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"
215 "gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)"
219 "\n\t<param> is\n\t\t"
220 "number of cars to be added if <command>=ac\n\t\t"
221 "number of persons to be added if <command>=ap\n\t\t"
222 "number of car buyers if <command>=bc\n\t\t"
223 "pass 0 if <command>=gc or gp or gcp or dc or dp"
226 "usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address"
227 "\n\t<command> = any of the following commands \n\t\t"
230 usageString = usageString + command + param
236 # entry point for command executions
241 if len(sys.argv) < 4:
244 settings.hostname = sys.argv[1]
245 settings.port = "8080"
252 gcp=getCarPersonMappings,
257 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
260 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
263 call[sys.argv[2]](int(sys.argv[3]))