update scripts because docker hostname can not have '_'
[faas.git] / demos / integration / testCases.py
1 import util
2 import urllib
3 import constants
4 #from enum import Enum
5 import json
6 import sys
7 import getopt
8 import inputsCommon
9 import inputsVcontainer
10 import inputsFabric
11 import inputsGBP
12 import inputsSFC
13 import tenant_2EPG_SFC
14 import tenant_3EPG_SFC
15 import tenant_3EPG_demo
16
17 #
18 # Manifested constants
19 #
20 #class RestMethods_e(Enum):
21 get_c = 1
22 put_c = 2
23 post_c = 3
24 post_data_array_c = 4
25 delete_c = 5
26
27 #
28 # Global variables
29 #
30 testCases_ga = {}
31
32
33 #
34 # Function Implimentations
35 #
36
37 #===============================================================================# 
38 def printTestCase(desc):
39   key = 0
40   key2 = 0
41   for key in sorted(testCases_ga.keys()):
42     print key, ": ", testCases_ga[key][1]
43
44   for key2 in sorted(testCases2_ga.keys()):
45     print key2, ": ", testCases2_ga[key2][3]
46
47   return constants.OK_STR
48
49 #===============================================================================#
50 def getTopology(desc):
51
52         # Set the request URL
53   #url = 'http://127.0.0.1:8181/restconf/config/network-topology:network-topology'
54   url = 'http://127.0.0.1:8181/restconf/operational/network-topology:network-topology'
55
56   # Send the GET request
57   resp = util.requestGET(url)
58
59   if resp == constants.ERROR_STR:
60     print "ERROR: getTopology() failed"
61     return resp
62
63   return resp
64
65 #===============================================================================# 
66 def registerEndpointLocation_acl(desc):
67   nc_id1 = "sw1-eth1"
68   nc_id2 = "sw2-eth3"
69   nodeId1 = util.getOvsdbNodeIdByName("sw1")
70
71   if nodeId1 == constants.ERROR_STR: 
72     return constants.ERROR_STR
73
74   nodeId2 = util.getOvsdbNodeIdByName("sw2")
75   if nodeId1 == constants.ERROR_STR: 
76     return constants.ERROR_STR
77
78   for inputData in inputsGBP.get_endpoint_location_data(nc_id1, nc_id2, nodeId1, nodeId2):
79     result = util.runRequestPOST(inputsGBP.get_endpoint_location_uri(), json.dumps(inputData), sys._getframe().f_code.co_name) 
80
81   return result
82
83 #===============================================================================# 
84 def registerEndpointLocation_sfc(desc):
85   nc_id1 = "vethl-h35-2"
86   nc_id2 = "vethl-h36-4"
87   nodeId1 = util.getOvsdbNodeIdByName("sw1")
88
89   if nodeId1 == constants.ERROR_STR:
90     return constants.ERROR_STR
91
92   nodeId2 = util.getOvsdbNodeIdByName("sw6")
93   if nodeId1 == constants.ERROR_STR:
94     return constants.ERROR_STR
95
96   for inputData in tenant_2EPG_SFC.get_endpoint_location_data(nc_id1, nc_id2, nodeId1, nodeId2):
97     result = util.runRequestPOST(inputsGBP.get_endpoint_location_uri(), json.dumps(inputData), sys._getframe().f_code.co_name)
98
99   return result
100
101 #===============================================================================# 
102 def registerEndpointLocation_3epg_sfc(desc):
103   nc_id1 = "vethl-h35-2"
104   nc_id2 = "vethl-h36-4"
105   nc_id3 = "vethl-h37-2"
106
107   nodeId1 = util.getOvsdbNodeIdByName("sw1")
108   if nodeId1 == constants.ERROR_STR:
109     return constants.ERROR_STR
110
111   nodeId2 = util.getOvsdbNodeIdByName("sw6")
112   if nodeId1 == constants.ERROR_STR:
113     return constants.ERROR_STR
114
115   nodeId3 = util.getOvsdbNodeIdByName("sw7")
116   if nodeId1 == constants.ERROR_STR:
117     return constants.ERROR_STR
118
119   for inputData in tenant_3EPG_SFC.get_endpoint_location_data(nc_id1, nc_id2, nc_id3, nodeId1, nodeId2, nodeId3):
120     result = util.runRequestPOST(inputsGBP.get_endpoint_location_uri(), json.dumps(inputData), sys._getframe().f_code.co_name)
121
122   return result
123
124 #===============================================================================# 
125 def registerEndpointLocation_3epg_sfc_acl(desc):
126   nc_id1a = "vethl-h35-2"
127   nc_id1b = "vethl-h35-3"
128   nc_id2a = "vethl-h36-4"
129   nc_id2b = "vethl-h36-5"
130   nc_id3a = "vethl-h37-2"
131   nc_id3b = "vethl-h37-3"
132
133   nodeId1 = util.getOvsdbNodeIdByName("sw1")
134   if nodeId1 == constants.ERROR_STR:
135     return constants.ERROR_STR
136
137   nodeId2 = util.getOvsdbNodeIdByName("sw6")
138   if nodeId1 == constants.ERROR_STR:
139     return constants.ERROR_STR
140
141   nodeId3 = util.getOvsdbNodeIdByName("sw7")
142   if nodeId1 == constants.ERROR_STR:
143     return constants.ERROR_STR
144
145   for inputData in tenant_3EPG_demo.get_endpoint_location_data(nc_id1a, nc_id1b, nc_id2a, nc_id2b, nc_id3a, nc_id3b, nodeId1, nodeId2, nodeId3):
146     result = util.runRequestPOST(inputsGBP.get_endpoint_location_uri(), json.dumps(inputData), sys._getframe().f_code.co_name)
147
148   return result
149
150 #===============================================================================#  
151 BRIDGE_REF_P="/network-topology:network-topology/network-topology:topology[network-topology:topology-id='ovsdb:1']/network-topology:node[network-topology:node-id='%s']"
152 TP_REF_P="/network-topology:network-topology/network-topology:topology[network-topology:topology-id='ovsdb:1']/network-topology:node[network-topology:node-id='%s']/network-topology:termination-point[network-topology:tp-id='%s']"
153 def epMigrationDemo(desc):
154    # unregister h35-3
155    fabricId = "fabric:1" 
156    mac = "00:00:00:00:35:03" 
157    ip = "10.0.35.3"
158    resp = util.runRequestGET(inputsFabric.get_epList_uri(), sys._getframe().f_code.co_name)
159    if resp == constants.ERROR_STR:
160       print "ERROR: getOvsdbNodeIdByName() failed"
161       return constants.ERROR_STR
162   
163    eplist = list()
164    lswId = ''
165    lport = ''
166    endpointResp=json.loads(resp)
167    if endpointResp.has_key("endpoints"):
168       endpoints = endpointResp["endpoints"]
169       if endpoints.has_key("endpoint"):
170          for endpoint in endpoints["endpoint"]:
171             if endpoint["ip-address"] == ip:
172                eplist.append(endpoint["endpoint-uuid"])
173                logicLocation = endpoint["logic-location"]
174                lswId = logicLocation["node-id"]
175                lport = logicLocation["tp-id"]
176                break
177
178    result = util.runRequestPOST(inputsFabric.get_unregister_endpoint_uri(), json.dumps(inputsFabric.get_unregister_endpoint_data("fabric:1", eplist)), sys._getframe().f_code.co_name)
179
180    # register h35-4
181    fabricId = "fabric:1"
182    mac = "00:00:00:00:35:04"
183    ip = "10.0.35.4"
184    gw = "10.0.35.1"
185    port = "vethl-h35-4"
186    nodeid = util.getOvsdbNodeIdByName("sw6")
187    nodeRef = BRIDGE_REF_P % (nodeid)
188    tpRef = TP_REF_P % (nodeid, port)
189
190    if nodeid == constants.ERROR_STR:
191       return constants.ERROR_STR
192
193    result = util.runRequestPOST(inputsFabric.get_register_endpoint_uri(), json.dumps(inputsFabric.get_register_endpoint_data(fabricId, mac, ip, gw, lswId, lport, nodeRef, tpRef)), sys._getframe().f_code.co_name) 
194
195    return desc
196
197 #===============================================================================# 
198 testCases_ga = {'0': (printTestCase, 'Print test case table'),
199    'p1': (getTopology, 'Print Topology'),
200    'vc05': (registerEndpointLocation_acl, 'Register endpoint locations for Layer 3 ULN with ACL'),
201    'vc052': (registerEndpointLocation_sfc, 'Register endpoint locations for Layer 3 ULN with SFC'),
202    'vc053': (registerEndpointLocation_3epg_sfc, 'Register endpoint locations for 3EPG-Layer3-ULN with SFC'),
203    'vc054': (registerEndpointLocation_3epg_sfc_acl, 'Register 6 endpoints locations for 3EPG-Layer3-ULN with SFC'),
204    'vc064': (epMigrationDemo, 'EP Migration from h35-3 on sw1 to h35-4 on sw6'),
205 }
206
207 testCases2_ga = {
208   'vc01': (post_c, 
209             inputsFabric.fa001Url_gc, 
210             inputsFabric.rpc_compose_fabric_data(), 
211             'Compose Fabric'),
212   'vc011': (post_c, 
213             inputsFabric.fa002Url_gc, 
214             inputsFabric.rpc_create_logic_switch_data("lsw001", 100, "fabric:1"), 
215             'Create LSW on Fabric'),
216   'vc012': (post_c, 
217             inputsFabric.fa003Url_gc, 
218             inputsFabric.rpc_create_logic_router_data("lr001", "fabric:1"), 
219             'Create LR on Fabric'),
220   'vc02': (post_c, 
221             inputsVcontainer.vc001Url_gc, 
222             inputsVcontainer.rpc_create_vcontainer_data(inputsCommon.tenant1Id_gc, "fabric:1"), 
223             'create vcontainer with one Fabric'),
224   'vc021': (post_c, 
225             inputsVcontainer.vc002Url_gc, 
226             inputsVcontainer.rpc_netnode_create_lsw_data(inputsCommon.tenant1Id_gc, "fabric:1"), 
227             'Create NetNode LSW'),
228   'vc022': (post_c, 
229             inputsVcontainer.vc003Url_gc, 
230             inputsVcontainer.rpc_netnode_create_lr_data(inputsCommon.tenant1Id_gc, "fabric:1"), 
231             'Create NetNode LR'),
232   'vc03': (put_c, 
233            inputsGBP.get_tenant_uri(inputsCommon.tenant1Id_gc), 
234            inputsGBP.get_tenant_data_layer3_acl(inputsCommon.tenant1Id_gc),
235            'Create tenant policy for a Layer 3 ULN with ACL'),
236   'vc031': (put_c, 
237             inputsGBP.get_tenant_uri(inputsCommon.tenant1Id_gc), 
238             inputsGBP.get_tenant_data_layer2_acl(inputsCommon.tenant1Id_gc),
239             'Create tenant of Layer 2 ULN with ACL'),
240   'vc032': (put_c, 
241             inputsGBP.get_tenant_uri(inputsCommon.tenant1Id_gc), 
242             tenant_2EPG_SFC.get_tenant_data(inputsCommon.tenant1Id_gc),
243             'Create tenant of Layer 3 ULN with SFC'),
244   'vc033': (put_c, 
245             inputsGBP.get_tenant_uri(inputsCommon.tenant1Id_gc), 
246             tenant_3EPG_SFC.get_tenant_data(inputsCommon.tenant1Id_gc),
247             'Create 3-EPG tenant of Layer 3 ULN with SFC'),
248   'vc034': (put_c, 
249             inputsGBP.get_tenant_uri(inputsCommon.tenant1Id_gc), 
250             tenant_3EPG_demo.get_tenant_data(inputsCommon.tenant1Id_gc),
251             'Create 3-EPG tenant of Layer 3 ULN with SFC and ACL and 6 endpoints'),
252   'vc04': (post_data_array_c, 
253            inputsGBP.get_endpoint_uri(), 
254            inputsGBP.get_endpoint_data_layer3(inputsCommon.tenant1Id_gc),
255            'Register endpoints for Layer 3 ULN with ACL'),
256   'vc041': (post_c, 
257             inputsGBP.get_endpoint_uri(), 
258             inputsGBP.get_endpoint_data(inputsCommon.tenant1Id_gc),
259             'Register endpoints on Layer 2 ULN with ACL'),
260   'vc042': (post_data_array_c, 
261            inputsGBP.get_endpoint_uri(), 
262            tenant_2EPG_SFC.get_endpoint_data(inputsCommon.tenant1Id_gc),
263            'Register endpoints for Layer 3 ULN with SFC'),
264   'vc043': (post_data_array_c, 
265            inputsGBP.get_endpoint_uri(), 
266            tenant_3EPG_SFC.get_endpoint_data(inputsCommon.tenant1Id_gc),
267            'Register endpoints for 3EPG-Layer3-ULN with SFC'),
268   'vc044': (post_data_array_c, 
269            inputsGBP.get_endpoint_uri(), 
270            tenant_3EPG_demo.get_endpoint_data(inputsCommon.tenant1Id_gc),
271            'Register 6 endpoints for 3EPG-Layer3-ULN with SFC and ACL'),
272   'vc051': (post_data_array_c, 
273            inputsGBP.get_endpoint_location_uri(),
274            inputsGBP.get_endpoint_location_data_old(),
275            'Register endpoint locations for Layer 3 ULN with ACL in sanity environment (i.e., no ovs running)'),
276   'vc06': (post_c, 
277            inputsGBP.get_unreg_endpoint_uri(), 
278            inputsGBP.get_unreg_endpoint_data_layer3(),
279            'Unregister endpoints on Layer 3 ULN'),
280   'vc07': (delete_c, 
281            inputsGBP.get_tenant_uri(inputsCommon.tenant1Id_gc), 
282            'unused param',
283            'Delete tenant of Layer 3 ULN'),
284   'p2': (get_c, 
285           inputsGBP.getLogicalNetworksUri_gc, 
286           'unused param',
287           'Get the logical network entities for all tenants on the uln-mapper side'),
288   'p3': (get_c, 
289           inputsGBP.getUlnUri_gc, 
290           'unused param',
291           'Get ULN on the GBP Faas renderer side'),
292   'p4': (get_c, 
293           inputsGBP.getMappedEntities_gc, 
294           'unused param',
295           'Get mapped IDs on the GBP FAAS renderer side'),
296   'p5': (get_c, 
297           inputsSFC.get_acl_uri(), 
298           'unused param',
299           'Get ACL list'),
300   'p6': (get_c, 
301           inputsSFC.get_invmgr_uri(), 
302           'unused param',
303           'Get flow rules'),
304   'p7': (get_c, 
305           inputsSFC.get_sfcList_uri(), 
306           'unused param',
307           'Get SFC list'),
308   'p8': (get_c, 
309           inputsSFC.get_sfList_uri(), 
310           'unused param',
311           'Get SF list'),
312   'p9': (get_c, 
313           inputsSFC.get_sffList_uri(), 
314           'unused param',
315           'Get service function forwarder(SFF) list'),
316   'p10': (get_c, 
317           inputsSFC.get_sfpList_uri(), 
318           'unused param',
319           'Get service function paths'),
320   'p11': (get_c, 
321           inputsFabric.get_epList_uri(), 
322           'unused param',
323           'Get registered endpoint list'),
324   'acl01': (put_c, 
325            inputsSFC.get_acl_uri(), 
326            inputsSFC.get_acl_data(),
327            'Create ACL'),
328   'acl02': (put_c, 
329            inputsSFC.get_acl_uri(), 
330            inputsSFC.get_acl_data2(),
331            'Create ACL')
332 }
333
334 def main(argv):
335   inputfile = ''
336   outputfile = ''
337   testNum = '0'
338   result = ''
339
340   try:
341       opts, args = getopt.getopt(argv,"hi:o:t:n:",["ifile=","ofile=","testcase=", "netnode="])
342   except getopt.GetoptError:
343     print 'test.py -i <inputfile> -o <outputfile>'
344     sys.exit(2)
345   for opt, arg in opts:
346     if opt == '-h':
347       print 'test.py -i <inputfile> -o <outputfile> -t <num>'
348       sys.exit()
349     elif opt in ("-i", "--ifile"):
350       inputfile = arg
351     elif opt in ("-o", "--ofile"):
352       outputfile = arg
353     elif opt in ("-t", "--testcase"):
354       #testNum = int(arg)
355       testNum = arg
356     else:
357       assert False, "unhandled option"
358
359   #workName = raw_input('Please enter a work name:')
360   res = util.setupAuthentication()
361
362   if res != constants.OK_STR:
363     sys.exit(1)
364
365   if testNum in testCases2_ga.keys():
366     print testCases2_ga[testNum][3]
367     if testCases2_ga[testNum][0] == put_c:
368       result = util.runRequestPUT(testCases2_ga[testNum][1], json.dumps(testCases2_ga[testNum][2]), sys._getframe().f_code.co_name) 
369     elif testCases2_ga[testNum][0] == post_c:
370       #print json.dumps(testCases2_ga[testNum][2])
371       result = util.runRequestPOST(testCases2_ga[testNum][1], json.dumps(testCases2_ga[testNum][2]), sys._getframe().f_code.co_name) 
372     elif testCases2_ga[testNum][0] == post_data_array_c:
373       #print json.dumps(testCases2_ga[testNum][2])
374       for inputData in (testCases2_ga[testNum][2]):
375         result = util.runRequestPOST(testCases2_ga[testNum][1], json.dumps(inputData), sys._getframe().f_code.co_name) 
376     elif testCases2_ga[testNum][0] == get_c:
377       result = util.runRequestGET(testCases2_ga[testNum][1], sys._getframe().f_code.co_name) 
378       print result
379     elif testCases2_ga[testNum][0] == delete_c:
380       result = util.runRequestDELETE(testCases2_ga[testNum][1], sys._getframe().f_code.co_name) 
381       print result
382   else:
383     result = testCases_ga[testNum][0](testCases_ga[testNum][1]) 
384     print result
385
386
387 #===============================================================================#  
388 # Main code
389 #
390 if __name__ == "__main__":
391    main(sys.argv[1:])
392
393
394