Step 2: Move test folder to root
[integration/test.git] / tools / odl-mdsal-clustering-tests / clustering-functional-test / crud.py
1 __author__ = "Basheeruddin Ahmed"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "syedbahm@cisco.com"
5 import sys
6 import util
7 import settings
8
9
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):
13         strId = str(x)
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=")
19         print(payload)
20         # Send the POST request
21         resp = util.post(settings.getAddCarUrl(), "admin", "admin", payload)
22
23         print("the response of the POST to add car=")
24         print(resp)
25
26     print("getting the cars in store ")
27     resp = getCars(0)
28
29     # TBD: Detailed validation
30
31
32 def addPerson(numberOfPersons):
33     """Creates the specified number of persons based on People yang model using main RPC
34
35     <note>
36         To enable RPC a non-user input person entry is created with personId=user0
37     </note>
38     """
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)
48         return
49
50     genderToggle = "Male"
51     for x in range(1, numberOfPersons+1):
52         if(genderToggle == "Male"):
53             genderToggle = "Female"
54         else:
55             genderToggle = "Male"
56
57         strId = str(x)
58
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)
65
66         print("payload formed after template substitution=")
67         print(payload)
68         print("the response of the POST to add person=")
69         print(resp)
70
71     print("getting the persons for verification")
72     resp = getPersons(0)
73
74     # TBD: Detailed validation
75
76
77 def addCarPerson(numberOfCarPersons):
78     """This method is not exposed via commands as only getCarPersons is of interest
79
80     addCarPerson entry happens when buyCar is called
81
82     <note>
83         To enable RPC a non-user input car-person entry is created with personId=user0
84     </note>
85     """
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)
92
93         return
94
95     for x in range(1, numberOfCarPersons + 1):
96         strId = str(x)
97
98         payload = settings.add_car_person_template.substitute(Id=strId, personId="user" + strId)
99
100         # Send the POST request REST CONF
101         resp = util.post(settings.getAddCarPersonUrl(), "admin", "admin", payload)
102
103         print("payload formed after template substitution=")
104         print(payload)
105
106         print("the response of the POST to add car_person=")
107         print(resp)
108
109     print("getting the car_persons for verification")
110     resp = getCarPersonMappings(0)
111
112     # TBD detailed validation
113
114
115 def buyCar(numberOfCarBuyers):
116     """Invokes an RPC REST call that does a car purchase by a person id
117
118     <note>
119         It is expected that the Car and Person entries are already created
120         before invoking this method
121     </note>
122     """
123     for x in range(1, numberOfCarBuyers + 1):
124         strId = str(x)
125
126         payload = settings.buy_car_rpc_template.substitute(personId="user" + strId, carId=strId)
127
128         # Send the POST request using RPC
129         resp = util.post(settings.getBuyCarRpcUrl(), "admin", "admin", payload)
130
131         print("payload formed after template substitution=")
132         print(payload)
133
134         print("the response of the POST to buycar=")
135         print(resp)
136
137     print("getting the car_persons for verification")
138     resp = getCarPersonMappings(0)
139
140
141 def getCars(ignore):
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")
144     print(resp)
145     return resp
146
147
148 def getPersons(ignore):
149     """Uses the GET on people:people resource to get all persons in the store using RESTCONF
150
151     <note>
152         This also returns the dummy entry created for routed RPC
153         with personId being user0
154     </note>
155     """
156     resp = util.get(settings.getPersonsUrl(), "admin", "admin")
157     print(resp)
158     return resp
159
160
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
163
164     <note>
165         This also returns the dummy entry created for routed RPC
166         with personId being user0
167     </note>
168     """
169     resp = util.get(settings.getCarPersonUrl(), "admin", "admin")
170     print(resp)
171     return resp
172
173
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))
179
180
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))
186
187
188 #
189 # Usage message shown to user
190 #
191
192 def options():
193
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)'
196
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'\
202
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'
205
206     usageString = usageString + command + param
207
208     print (usageString)
209
210
211 #
212 # entry point for command executions
213 #
214
215 def main():
216     if len(sys.argv) < 4:
217         options()
218         quit(0)
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)
223
224     # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
225     addPerson(0)
226
227     # FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
228     addCarPerson(0)
229
230     call[sys.argv[2]](int(sys.argv[3]))
231
232 #
233 # main invoked
234 main()