4f8124995ea6bec9338d45bce563109ff8ae7c51
[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, category="category" + strId, model="model" + strId,
18             manufacturer="manufacturer" + strId,
19             year=(2000 + x % 100))
20         print("payload formed after template substitution=")
21         print(payload)
22         # Send the POST request
23         resp = util.post(settings.getAddCarUrl(), "admin", "admin", payload)
24
25         print("the response of the POST to add car=")
26         print(resp)
27
28     print("getting the cars in store ")
29     resp = getCars(0)
30
31     # TBD: Detailed validation
32
33
34 def addPerson(numberOfPersons):
35     """Creates the specified number of persons based on People yang model using main RPC
36
37     <note>
38         To enable RPC a non-user input person entry is created with personId=user0
39     </note>
40     """
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)
50         return
51
52     genderToggle = "Male"
53     for x in range(1, numberOfPersons+1):
54         if(genderToggle == "Male"):
55             genderToggle = "Female"
56         else:
57             genderToggle = "Male"
58
59         strId = str(x)
60
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)
67
68         print("payload formed after template substitution=")
69         print(payload)
70         print("the response of the POST to add person=")
71         print(resp)
72
73     print("getting the persons for verification")
74     resp = getPersons(0)
75
76     # TBD: Detailed validation
77
78
79 def addCarPerson(numberOfCarPersons):
80     """This method is not exposed via commands as only getCarPersons is of interest
81
82     addCarPerson entry happens when buyCar is called
83
84     <note>
85         To enable RPC a non-user input car-person entry is created with personId=user0
86     </note>
87     """
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)
94
95         return
96
97     for x in range(1, numberOfCarPersons + 1):
98         strId = str(x)
99
100         payload = settings.add_car_person_template.substitute(Id=strId, personId="user" + strId)
101
102         # Send the POST request REST CONF
103         resp = util.post(settings.getAddCarPersonUrl(), "admin", "admin", payload)
104
105         print("payload formed after template substitution=")
106         print(payload)
107
108         print("the response of the POST to add car_person=")
109         print(resp)
110
111     print("getting the car_persons for verification")
112     resp = getCarPersonMappings(0)
113
114     # TBD detailed validation
115
116
117 def buyCar(numberOfCarBuyers):
118     """Invokes an RPC REST call that does a car purchase by a person id
119
120     <note>
121         It is expected that the Car and Person entries are already created
122         before invoking this method
123     </note>
124     """
125     for x in range(1, numberOfCarBuyers + 1):
126         strId = str(x)
127
128         payload = settings.buy_car_rpc_template.substitute(personId="user" + strId, carId=strId)
129
130         # Send the POST request using RPC
131         resp = util.post(settings.getBuyCarRpcUrl(), "admin", "admin", payload)
132
133         print("payload formed after template substitution=")
134         print(payload)
135
136         print("the response of the POST to buycar=")
137         print(resp)
138
139     print("getting the car_persons for verification")
140     resp = getCarPersonMappings(0)
141
142
143 def getCars(ignore):
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")
146     print(resp)
147     return resp
148
149
150 def getPersons(ignore):
151     """Uses the GET on people:people resource to get all persons in the store using RESTCONF
152
153     <note>
154         This also returns the dummy entry created for routed RPC
155         with personId being user0
156     </note>
157     """
158     resp = util.get(settings.getPersonsUrl(), "admin", "admin")
159     print(resp)
160     return resp
161
162
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
165
166     <note>
167         This also returns the dummy entry created for routed RPC
168         with personId being user0
169     </note>
170     """
171     resp = util.get(settings.getCarPersonUrl(), "admin", "admin")
172     print(resp)
173     return resp
174
175
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))
181
182
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))
188
189
190 #
191 # Usage message shown to user
192 #
193
194 def options():
195
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)'
198
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'\
204
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'
207
208     usageString = usageString + command + param
209
210     print (usageString)
211
212
213 #
214 # entry point for command executions
215 #
216
217 def main():
218     if len(sys.argv) < 4:
219         options()
220         quit(0)
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)
225
226     # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
227     addPerson(0)
228
229     # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
230     addCarPerson(0)
231
232     call[sys.argv[2]](int(sys.argv[3]))
233
234 #
235 # main invoked
236 main()