renamed the test cases file name to contain proper
[integration/test.git] / test / csit / libraries / CrudLibrary.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 UtilLibrary
7 import SettingsLibrary
8
9
10 #
11 #Creates the specified number of cars based on Cars yang model
12 # using RESTCONF
13 #
14
15 def addCar(hostname,port,numberOfCars):
16
17     for x in range(1, numberOfCars+1):
18         strId = str(x)
19         payload = SettingsLibrary.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 = UtilLibrary.post(SettingsLibrary.getAddCarUrl(hostname,port),"admin", "admin",payload)
26
27         print("the response of the POST to add car=")
28         print(resp)
29
30     return resp
31
32     #TBD: Detailed validation
33
34
35 #
36 #Creates the specified number of persons based on People yang model
37 # using main RPC
38 # <note>
39 #   To enable RPC a non-user input person entry is created with personId=user0
40 # </note>
41 #
42 def addPerson(hostname,port,numberOfPersons):
43     #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
44     if(numberOfPersons==0):
45         strId =str(numberOfPersons)
46         payload = SettingsLibrary.add_person_payload_template.substitute(personId="user"+strId,gender="unknown",age=0,
47                                                                   address=strId + "Way, Some Country, Some Zip  "+strId,
48                                                                   contactNo= "some number"+strId)
49         # Send the POST request using RESTCONF
50         resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddPersonUrl(hostname,port),"admin", "admin",payload)
51         return resp
52
53     genderToggle = "Male"
54     for x in range(1, numberOfPersons+1):
55         if(genderToggle == "Male"):
56             genderToggle = "Female"
57         else:
58             genderToggle = "Male"
59
60         strId = str(x)
61
62         payload = SettingsLibrary.add_person_rpc_payload_template.substitute(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 = UtilLibrary.post(SettingsLibrary.getAddPersonRpcUrl(hostname,port),"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     return resp
74
75
76     #TBD: Detailed validation
77
78 #This method is not exposed via commands as only getCarPersons is of interest
79 #addCarPerson entry happens when buyCar is called
80 # <note>
81 #   To enable RPC a non-user input car-person entry is created with personId=user0
82 # </note>
83 #
84 def addCarPerson(hostname,port,numberOfCarPersons):
85
86     #FOR RPC TO WORK PROPERLY THE FIRST ENTRY SHOULD BE VIA RESTCONF
87     if(numberOfCarPersons==0):
88         payload = SettingsLibrary.add_car_person_template.substitute(Id=str(numberOfCarPersons),personId="user"+str(numberOfCarPersons))
89         # Send the POST request REST CONF
90         resp = UtilLibrary.nonprintpost(SettingsLibrary.getAddCarPersonUrl(hostname,port),"admin", "admin",payload)
91
92         return
93
94     for x in range(1, numberOfCarPersons+1):
95         strId = str(x)
96
97         payload = SettingsLibrary.add_car_person_template.substitute(Id=strId,personId="user"+strId)
98
99         # Send the POST request REST CONF
100         resp = UtilLibrary.post(SettingsLibrary.getAddCarPersonUrl(hostname,port),"admin", "admin",payload)
101
102         print("payload formed after template substitution=")
103         print(payload)
104
105         print("the response of the POST to add car_person=")
106         print(resp)
107
108     print("getting the car_persons for verification")
109     resp=getCarPersonMappings(hostname,port,0)
110
111     #TBD detailed validation
112
113 #
114 # Invokes an RPC REST call that does a car purchase by a person id
115 # <note>
116 # It is expected that the Car and Person entries are already created
117 # before invoking this method
118 # </note>
119 #
120
121 def buyCar(hostname,port,numberOfCarBuyers):
122     for x in range(1, numberOfCarBuyers+1):
123         strId = str(x)
124
125         payload = SettingsLibrary.buy_car_rpc_template.substitute(personId="user"+strId,carId=strId)
126
127         # Send the POST request using RPC
128         resp = UtilLibrary.post(SettingsLibrary.getBuyCarRpcUrl(hostname,port),"admin", "admin",payload)
129
130         print("payload formed after template substitution=")
131         print(payload)
132
133         print("the response of the POST to buycar=")
134         print(resp)
135
136     print("getting the car_persons for verification")
137     resp=getCarPersonMappings(hostname,port,0)
138
139
140 #
141 # Uses the GET on car:cars resource
142 # to get all cars in the store using RESTCONF
143 #
144 #
145 def getCars(hostname,port,ignore):
146     resp = UtilLibrary.get(SettingsLibrary.getCarsUrl(hostname,port),"admin", "admin")
147     resp.encoding = 'utf-8'
148     print (resp.text)
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(hostname,port,ignore):
161     resp = UtilLibrary.get(SettingsLibrary.getPersonsUrl(hostname,port),"admin","admin")
162     resp.encoding = 'utf-8'
163     print (resp.text)
164     return resp
165
166 #
167 # Uses the GET on car-people:car-people resource
168 # to get all car-persons entry in the store using RESTCONF
169 #<note>
170 #This also returns the dummy entry created for routed RPC
171 # with personId being user0
172 #</note>
173 #
174 def getCarPersonMappings(hostname,port,ignore):
175     resp = UtilLibrary.get(SettingsLibrary.getCarPersonUrl(hostname,port),"admin","admin")
176     resp.encoding = 'utf-8'
177     print (resp)
178
179     return resp
180
181 #
182 #delete all cars in the store using RESTCONF
183 #
184 #
185 def deleteAllCars(hostname,port,ignore):
186     UtilLibrary.delete(SettingsLibrary.getCarsUrl(hostname,port),"admin","admin")
187     resp = getCars(hostname,port,ignore)
188     print("Cars in store after deletion:"+ str(resp))
189
190 #
191 #delete all persons in the store using RESTCONF
192 #
193 #
194 def deleteAllPersons(hostname,port,ignore):
195     UtilLibrary.delete(SettingsLibrary.getPersonsUrl(hostname,port),"admin","admin")
196     resp = getPersons(hostname,port,ignore)
197     print("Persons in store after deletion:"+ str(resp))
198
199
200 #
201 #delete all car -poeple s in the store using RESTCONF
202 #
203 #
204 def deleteAllCarsPersons(hostname,port,ignore):
205     UtilLibrary.delete(SettingsLibrary.getCarPersonsUrl(hostname,port),"admin","admin")
206     resp = getPersons(hostname,port,ignore)
207     print("Persons in store after deletion:"+ str(resp))
208
209 #
210 # Usage message shown to user
211 #
212
213 def options():
214
215     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' \
216               'gcp=Get Car-Person Mappings\n\t\tdc=Delete All Cars\n\t\tdp=Delete All Persons)'
217
218     param =  '\n\t<param> is\n\t\t' \
219              'number of cars to be added if <command>=ac\n\t\t' \
220              'number of persons to be added if <command>=ap\n\t\t' \
221              'number of car buyers if <command>=bc\n\t\t'\
222              'pass 0 if <command>=gc or gp or gcp or dc or dp'\
223
224
225     usageString = 'usage: python crud <ipaddress> <command> <param>\nwhere\n\t<ipaddress> = ODL server ip address' \
226                   '\n\t<command> = any of the following commands \n\t\t'
227
228     usageString = usageString + command +param
229
230     print (usageString)
231
232
233 #
234 # entry point for command executions
235 #
236
237 def main():
238     if len(sys.argv) < 4:
239         options()
240         quit(0)
241     SettingsLibrary.hostname = sys.argv[1]
242     SettingsLibrary.port = '8080'
243     call = dict(ac=addCar, ap=addPerson, bc=buyCar,
244                 gc=getCars, gp=getPersons, gcp=getCarPersonMappings,dc=deleteAllCars,dp=deleteAllPersons)
245
246     #FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
247     addPerson(SettingsLibrary.hostname,SettingsLibrary.port,0)
248
249     #FOR RPC TO WORK PROPERLY THE FIRST PERSON SHOULD BE ADDED VIA RESTCONF
250     addCarPerson(SettingsLibrary.hostname,SettingsLibrary.port,0)
251
252
253     call[sys.argv[2]](SettingsLibrary.hostname,SettingsLibrary.port,int(sys.argv[3]))
254
255 #
256 # main invoked
257 if __name__ == "__main__":
258  main()