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(
17 id=strId, category="category" + strId, model="model" + strId,
18 manufacturer="manufacturer" + strId,
19 year=(2000 + x % 100))
20 print("payload formed after template substitution=")
22 # Send the POST request
23 resp = util.post(settings.getAddCarUrl(), "admin", "admin", payload)
25 print("the response of the POST to add car=")
28 print("getting the cars in store ")
31 # TBD: Detailed validation
34 def addPerson(numberOfPersons):
35 """Creates the specified number of persons based on People yang model using main RPC
38 To enable RPC a non-user input person entry is created with personId=user0
41 # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
42 if numberOfPersons == 0:
43 strId = str(numberOfPersons)
44 payload = settings.add_person_payload_template.substitute(
45 personId="user" + strId, gender="unknown", age=0,
46 address=strId + "Way, Some Country, Some Zip " + strId,
47 contactNo="some number" + strId)
48 # Send the POST request using RESTCONF
49 resp = util.nonprintpost(settings.getAddPersonUrl(), "admin", "admin", payload)
53 for x in range(1, numberOfPersons+1):
54 if(genderToggle == "Male"):
55 genderToggle = "Female"
61 payload = settings.add_person_rpc_payload_template.substitute(
62 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 = util.post(settings.getAddPersonRpcUrl(), "admin", "admin", payload)
68 print("payload formed after template substitution=")
70 print("the response of the POST to add person=")
73 print("getting the persons for verification")
76 # TBD: Detailed validation
79 def addCarPerson(numberOfCarPersons):
80 """This method is not exposed via commands as only getCarPersons is of interest
82 addCarPerson entry happens when buyCar is called
85 To enable RPC a non-user input car-person entry is created with personId=user0
88 # FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
89 if numberOfCarPersons == 0:
90 payload = settings.add_car_person_template.substitute(
91 Id=str(numberOfCarPersons), personId="user" + str(numberOfCarPersons))
92 # Send the POST request REST CONF
93 resp = util.nonprintpost(settings.getAddCarPersonUrl(), "admin", "admin", payload)
97 for x in range(1, numberOfCarPersons + 1):
100 payload = settings.add_car_person_template.substitute(Id=strId, personId="user" + strId)
102 # Send the POST request REST CONF
103 resp = util.post(settings.getAddCarPersonUrl(), "admin", "admin", payload)
105 print("payload formed after template substitution=")
108 print("the response of the POST to add car_person=")
111 print("getting the car_persons for verification")
112 resp = getCarPersonMappings(0)
114 # TBD detailed validation
117 def buyCar(numberOfCarBuyers):
118 """Invokes an RPC REST call that does a car purchase by a person id
121 It is expected that the Car and Person entries are already created
122 before invoking this method
125 for x in range(1, numberOfCarBuyers + 1):
128 payload = settings.buy_car_rpc_template.substitute(personId="user" + strId, carId=strId)
130 # Send the POST request using RPC
131 resp = util.post(settings.getBuyCarRpcUrl(), "admin", "admin", payload)
133 print("payload formed after template substitution=")
136 print("the response of the POST to buycar=")
139 print("getting the car_persons for verification")
140 resp = getCarPersonMappings(0)
144 """Uses the GET on car:cars resource to get all cars in the store using RESTCONF"""
145 resp = util.get(settings.getCarsUrl(), "admin", "admin")
150 def getPersons(ignore):
151 """Uses the GET on people:people resource to get all persons in the store using RESTCONF
154 This also returns the dummy entry created for routed RPC
155 with personId being user0
158 resp = util.get(settings.getPersonsUrl(), "admin", "admin")
163 def getCarPersonMappings(ignore):
164 """Uses the GET on car-people:car-people resource to get all car-persons entry in the store using RESTCONF
167 This also returns the dummy entry created for routed RPC
168 with personId being user0
171 resp = util.get(settings.getCarPersonUrl(), "admin", "admin")
176 def deleteAllCars(ignore):
177 """delete all cars in the store using RESTCONF"""
178 util.delete(settings.getCarsUrl(), "admin", "admin")
179 resp = getCars(ignore)
180 print("Cars in store after deletion:" + str(resp))
183 def deleteAllPersons(ignore):
184 """delete all persons in the store using RESTCONF"""
185 util.delete(settings.getPersonsUrl(), "admin", "admin")
186 resp = getPersons(ignore)
187 print("Persons in store after deletion:" + str(resp))
191 # Usage message shown to user
196 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' \
197 'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
199 param = '\n\t<param> is\n\t\t' \
200 'number of cars to be added if <command>=ac\n\t\t' \
201 'number of persons to be added if <command>=ap\n\t\t' \
202 'number of car buyers if <command>=bc\n\t\t'\
203 'pass 0 if <command>=gc or gp or gcp or dc or dp'\
205 usageString = 'usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address' \
206 '\n\t<command> = any of the following commands \n\t\t'
208 usageString = usageString + command + param
214 # entry point for command executions
218 if len(sys.argv) < 4:
221 settings.hostname = sys.argv[1]
222 settings.port = '8080'
223 call = dict(ac=addCar, ap=addPerson, bc=buyCar,
224 gc=getCars, gp=getPersons, gcp=getCarPersonMappings, dc=deleteAllCars, dp=deleteAllPersons)
226 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
229 # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
232 call[sys.argv[2]](int(sys.argv[3]))