Merge "Renamed populate.py to crud.py Fixed method call name change Signed-off-by...
[integration/test.git] / test / 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 #
11 #Creates the specified number of cars based on Cars yang model
12 # using RESTCONF
13 #
14
15 def addCar(numberOfCars):
16
17     for x in range(1, numberOfCars+1):
18         strId = str(x)
19         payload = settings.add_car_payload_template.substitute(id=strId,category="category"+strId,model="model"+strId,
20                                                            manufacturer="manufacturer"+strId,
21                                                            year=(2000+x%100))
22         print("payload formed after template substitution=")
23         print(payload)
24         # Send the POST request
25         resp = util.post(settings.getAddCarUrl(),"admin", "admin",payload)
26
27         print("the response of the POST to add car=")
28         print(resp)
29
30     print("getting the cars in store ")
31     resp = getCars(0)
32
33     #TBD: Detailed validation
34
35
36 #
37 #Creates the specified number of persons based on People yang model
38 # using main RPC
39 # <note>
40 #   To enable RPC a non-user input person entry is created with personId=user0
41 # </note>
42 #
43 def addPerson(numberOfPersons):
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(personId="user"+strId,gender="unknown",age=0,
48                                                                   address=strId + "Way, Some Country, Some Zip  "+strId,
49                                                                   contactNo= "some number"+strId)
50         # Send the POST request using RESTCONF
51         resp = util.nonprintpost(settings.getAddPersonUrl(),"admin", "admin",payload)
52         return
53
54     genderToggle = "Male"
55     for x in range(1, numberOfPersons+1):
56         if(genderToggle == "Male"):
57             genderToggle = "Female"
58         else:
59             genderToggle = "Male"
60
61         strId = str(x)
62
63         payload = settings.add_person_rpc_payload_template.substitute(personId="user"+strId,gender=genderToggle,age=(20+x%100),
64                                                                       address=strId + "Way, Some Country, Some Zip  "+str(x%1000),
65                                                                       contactNo= "some number"+strId)
66         # Send the POST request using RPC
67         resp = util.post(settings.getAddPersonRpcUrl(),"admin", "admin",payload)
68
69         print("payload formed after template substitution=")
70         print(payload)
71         print("the response of the POST to add person=")
72         print(resp)
73
74     print("getting the persons for verification")
75     resp=getPersons(0)
76
77     #TBD: Detailed validation
78
79 #This method is not exposed via commands as only getCarPersons is of interest
80 #addCarPerson entry happens when buyCar is called
81 # <note>
82 #   To enable RPC a non-user input car-person entry is created with personId=user0
83 # </note>
84 #
85 def addCarPerson(numberOfCarPersons):
86
87     #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
88     if(numberOfCarPersons==0):
89         payload = settings.add_car_person_template.substitute(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 # Invokes an RPC REST call that does a car purchase by a person id
116 # <note>
117 # It is expected that the Car and Person entries are already created
118 # before invoking this method
119 # </note>
120 #
121
122 def buyCar(numberOfCarBuyers):
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 #
142 # Uses the GET on car:cars resource
143 # to get all cars in the store using RESTCONF
144 #
145 #
146 def getCars(ignore):
147     resp = util.get(settings.getCarsUrl(),"admin", "admin")
148     print (resp)
149     return resp
150
151 #
152 # Uses the GET on people:people resource
153 # to get all persons in the store using RESTCONF
154 #<note>
155 #This also returns the dummy entry created for routed RPC
156 # with personId being user0
157 #</note>
158 #
159 #
160 def getPersons(ignore):
161     resp = util.get(settings.getPersonsUrl(),"admin","admin")
162     print (resp)
163     return resp
164
165 #
166 # Uses the GET on car-people:car-people resource
167 # to get all car-persons entry in the store using RESTCONF
168 #<note>
169 #This also returns the dummy entry created for routed RPC
170 # with personId being user0
171 #</note>
172 #
173 def getCarPersonMappings(ignore):
174     resp = util.get(settings.getCarPersonUrl(),"admin","admin")
175     print (resp)
176     return resp
177
178 #
179 #delete all cars in the store using RESTCONF
180 #
181 #
182 def deleteAllCars(ignore):
183     util.delete(settings.getCarsUrl(),"admin","admin")
184     resp = getCars(ignore)
185     print("Cars in store after deletion:"+ str(resp))
186
187 #
188 #delete all persons in the store using RESTCONF
189 #
190 #
191 def deleteAllPersons(ignore):
192     util.delete(settings.getPersonsUrl(),"admin","admin")
193     resp = getPersons(ignore)
194     print("Persons in store after deletion:"+ str(resp))
195
196 #
197 # Usage message shown to user
198 #
199
200 def options():
201
202     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' \
203               'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
204
205     param =  '\n\t<param> is\n\t\t' \
206              'number of cars to be added if <command>=ac\n\t\t' \
207              'number of persons to be added if <command>=ap\n\t\t' \
208              'number of car buyers if <command>=bc\n\t\t'\
209              'pass 0 if <command>=gc or gp or gcp or dc or dp'\
210
211
212     usageString = 'usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address' \
213                   '\n\t<command> = any of the following commands \n\t\t'
214
215     usageString = usageString + command +param
216
217     print (usageString)
218
219
220 #
221 # entry point for command executions
222 #
223
224 def main():
225     if len(sys.argv) < 4:
226         options()
227         quit(0)
228     settings.hostname = sys.argv[1]
229     settings.port = '8080'
230     call = dict(ac=addCar, ap=addPerson, bc=buyCar,
231                 gc=getCars, gp=getPersons, gcp=getCarPersonMappings,dc=deleteAllCars,dp=deleteAllPersons)
232
233     #FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
234     addPerson(0)
235
236     #FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
237     addCarPerson(0)
238
239
240     call[sys.argv[2]](int(sys.argv[3]))
241
242 #
243 # main invoked
244 main()