Migrate Get Requests invocations(libraries)
[integration/test.git] / tools / odl-mdsal-clustering-tests / clustering-functional-test / crud.py
1 import sys
2 import util
3 import settings
4
5
6 __author__ = "Basheeruddin Ahmed"
7 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
8 __license__ = "New-style BSD"
9 __email__ = "syedbahm@cisco.com"
10
11
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):
15         strId = str(x)
16         payload = settings.add_car_payload_template.substitute(
17             id=strId,
18             category="category" + strId,
19             model="model" + strId,
20             manufacturer="manufacturer" + strId,
21             year=(2000 + x % 100),
22         )
23         print("payload formed after template substitution=")
24         print(payload)
25         # Send the POST request
26         resp = util.post(settings.getAddCarUrl(), "admin", "admin", payload)
27
28         print("the response of the POST to add car=")
29         print(resp)
30
31     print("getting the cars in store ")
32     resp = getCars(0)
33
34     # TBD: Detailed validation
35
36
37 def addPerson(numberOfPersons):
38     """Creates the specified number of persons based on People yang model using main RPC
39
40     <note>
41         To enable RPC a non-user input person entry is created with personId=user0
42     </note>
43     """
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,
49             gender="unknown",
50             age=0,
51             address=strId + "Way, Some Country, Some Zip  " + strId,
52             contactNo="some number" + strId,
53         )
54         # Send the POST request using RESTCONF
55         resp = util.nonprintpost(settings.getAddPersonUrl(), "admin", "admin", payload)
56         return
57
58     genderToggle = "Male"
59     for x in range(1, numberOfPersons + 1):
60         if genderToggle == "Male":
61             genderToggle = "Female"
62         else:
63             genderToggle = "Male"
64
65         strId = str(x)
66
67         payload = settings.add_person_rpc_payload_template.substitute(
68             personId="user" + strId,
69             gender=genderToggle,
70             age=(20 + x % 100),
71             address=strId + "Way, Some Country, Some Zip  " + str(x % 1000),
72             contactNo="some number" + strId,
73         )
74         # Send the POST request using RPC
75         resp = util.post(settings.getAddPersonRpcUrl(), "admin", "admin", payload)
76
77         print("payload formed after template substitution=")
78         print(payload)
79         print("the response of the POST to add person=")
80         print(resp)
81
82     print("getting the persons for verification")
83     resp = getPersons(0)
84
85     # TBD: Detailed validation
86
87
88 def addCarPerson(numberOfCarPersons):
89     """This method is not exposed via commands as only getCarPersons is of interest
90
91     addCarPerson entry happens when buyCar is called
92
93     <note>
94         To enable RPC a non-user input car-person entry is created with personId=user0
95     </note>
96     """
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)
101         )
102         # Send the POST request REST CONF
103         resp = util.nonprintpost(
104             settings.getAddCarPersonUrl(), "admin", "admin", payload
105         )
106
107         return
108
109     for x in range(1, numberOfCarPersons + 1):
110         strId = str(x)
111
112         payload = settings.add_car_person_template.substitute(
113             Id=strId, personId="user" + strId
114         )
115
116         # Send the POST request REST CONF
117         resp = util.post(settings.getAddCarPersonUrl(), "admin", "admin", payload)
118
119         print("payload formed after template substitution=")
120         print(payload)
121
122         print("the response of the POST to add car_person=")
123         print(resp)
124
125     print("getting the car_persons for verification")
126     resp = getCarPersonMappings(0)
127
128     # TBD detailed validation
129
130
131 def buyCar(numberOfCarBuyers):
132     """Invokes an RPC REST call that does a car purchase by a person id
133
134     <note>
135         It is expected that the Car and Person entries are already created
136         before invoking this method
137     </note>
138     """
139     for x in range(1, numberOfCarBuyers + 1):
140         strId = str(x)
141
142         payload = settings.buy_car_rpc_template.substitute(
143             personId="user" + strId, carId=strId
144         )
145
146         # Send the POST request using RPC
147         resp = util.post(settings.getBuyCarRpcUrl(), "admin", "admin", payload)
148
149         print("payload formed after template substitution=")
150         print(payload)
151
152         print("the response of the POST to buycar=")
153         print(resp)
154
155     print("getting the car_persons for verification")
156     resp = getCarPersonMappings(0)
157
158
159 def getCars(ignore):
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")
162     print(resp)
163     return resp
164
165
166 def getPersons(ignore):
167     """Uses the GET on people:people resource to get all persons in the store using RESTCONF
168
169     <note>
170         This also returns the dummy entry created for routed RPC
171         with personId being user0
172     </note>
173     """
174     resp = util.get(settings.getPersonsUrl(), "admin", "admin")
175     print(resp)
176     return resp
177
178
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
181
182     <note>
183         This also returns the dummy entry created for routed RPC
184         with personId being user0
185     </note>
186     """
187     resp = util.get(settings.getCarPersonUrl(), "admin", "admin")
188     print(resp)
189     return resp
190
191
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))
197
198
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))
204
205
206 #
207 # Usage message shown to user
208 #
209
210
211 def options():
212
213     command = (
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)"
216     )
217
218     param = (
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"
224     )
225     usageString = (
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"
228     )
229
230     usageString = usageString + command + param
231
232     print(usageString)
233
234
235 #
236 # entry point for command executions
237 #
238
239
240 def main():
241     if len(sys.argv) < 4:
242         options()
243         quit(0)
244     settings.hostname = sys.argv[1]
245     settings.port = "8080"
246     call = dict(
247         ac=addCar,
248         ap=addPerson,
249         bc=buyCar,
250         gc=getCars,
251         gp=getPersons,
252         gcp=getCarPersonMappings,
253         dc=deleteAllCars,
254         dp=deleteAllPersons,
255     )
256
257     # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
258     addPerson(0)
259
260     # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
261     addCarPerson(0)
262
263     call[sys.argv[2]](int(sys.argv[3]))
264
265
266 #
267 # main invoked
268 main()