Merge "Added 5GTANGO lightweight lifecycle manager (LLCM)."
[osm/vim-emu.git] / src / emuvim / test / unittests / test_sonata_dummy_gatekeeper.py
1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
2 # ALL RIGHTS RESERVED.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Neither the name of the SONATA-NFV, Paderborn University
17 # nor the names of its contributors may be used to endorse or promote
18 # products derived from this software without specific prior written
19 # permission.
20 #
21 # This work has been performed in the framework of the SONATA project,
22 # funded by the European Commission under Grant number 671517 through
23 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
24 # acknowledge the contributions of their colleagues of the SONATA
25 # partner consortium (www.sonata-nfv.eu).
26 import time
27 import requests
28 import json
29 import unittest
30 from emuvim.test.base import SimpleTestTopology
31 from emuvim.api.sonata import SonataDummyGatekeeperEndpoint
32 from emuvim.api.sonata.dummygatekeeper import initialize_GK, parse_interface
33 from ipaddress import ip_network
34
35 PACKAGE_PATH = "misc/sonata-demo-service.son"
36
37
38 class testSonataDummyGatekeeper(SimpleTestTopology):
39
40 # @unittest.skip("disabled")
41 def test_GK_Api_start_service(self):
42 # create network
43 self.createNet(nswitches=0, ndatacenter=2, nhosts=2,
44 ndockers=0, enable_learning=True)
45 # setup links
46 self.net.addLink(self.dc[0], self.h[0])
47 self.net.addLink(self.dc[0], self.dc[1])
48 self.net.addLink(self.h[1], self.dc[1])
49 # connect dummy GK to data centers
50 sdkg1 = SonataDummyGatekeeperEndpoint("127.0.0.1", 55000)
51 sdkg1.connectDatacenter(self.dc[0])
52 sdkg1.connectDatacenter(self.dc[1])
53 # run the dummy gatekeeper (in another thread, don't block)
54 sdkg1.start()
55 time.sleep(3)
56 # start Mininet network
57 self.startNet()
58 time.sleep(3)
59
60 print "starting tests"
61 # board package
62 files = {"package": open(PACKAGE_PATH, "rb")}
63 r = requests.post("http://127.0.0.1:55000/packages", files=files)
64 self.assertEqual(r.status_code, 201)
65 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
66
67 # instantiate service
68 self.service_uuid = json.loads(r.text).get("service_uuid")
69 r2 = requests.post("http://127.0.0.1:55000/instantiations",
70 data=json.dumps({"service_uuid": self.service_uuid}))
71 self.assertEqual(r2.status_code, 201)
72
73 # give the emulator some time to instantiate everything
74 time.sleep(2)
75
76 # check get request APIs
77 r3 = requests.get("http://127.0.0.1:55000/packages")
78 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
79 r4 = requests.get("http://127.0.0.1:55000/instantiations")
80 self.assertEqual(len(json.loads(r4.text).get(
81 "service_instantiations_list")), 1)
82
83 # check number of running nodes
84 self.assertTrue(len(self.getContainernetContainers()) == 3)
85 self.assertTrue(len(self.net.hosts) == 5)
86 self.assertTrue(len(self.net.switches) == 2)
87 # check compute list result
88 self.assertEqual(len(self.dc[0].listCompute()), 2)
89 # check connectivity by using ping
90 ELAN_list = []
91
92 # check E-Line connection, by checking the IP addresses
93 for link in self.net.deployed_elines:
94 vnf_src, intf_src, vnf_sap_docker_name = parse_interface(
95 link['connection_points_reference'][0])
96 print vnf_src, intf_src
97 src = self.net.getNodeByName(vnf_src)
98 if not src:
99 continue
100 network_list = src.getNetworkStatus()
101 src_ip = [intf['ip']
102 for intf in network_list if intf['intf_name'] == intf_src][0]
103 src_mask = [intf['netmask']
104 for intf in network_list if intf['intf_name'] == intf_src][0]
105
106 vnf_dst, intf_dst, vnf_sap_docker_name = parse_interface(
107 link['connection_points_reference'][1])
108 dst = self.net.getNodeByName(vnf_dst)
109 if not dst:
110 continue
111 network_list = dst.getNetworkStatus()
112 dst_ip = [intf['ip']
113 for intf in network_list if intf['intf_name'] == intf_dst][0]
114 dst_mask = [intf['netmask']
115 for intf in network_list if intf['intf_name'] == intf_dst][0]
116
117 print "src = {0}:{1} ip={2} ".format(
118 vnf_src, intf_src, src_ip, src_mask)
119 print "dst = {0}:{1} ip={2} ".format(
120 vnf_dst, intf_dst, dst_ip, dst_mask)
121
122 # check if the E-Line IP's are in the same subnet
123 ret = ip_network(u'{0}'.format(src_ip, src_mask), strict=False)\
124 .compare_networks(ip_network(u'{0}'.format(dst_ip, dst_mask), strict=False))
125 self.assertTrue(ret == 0)
126
127 for vnf in self.dc[0].listCompute():
128 # check E LAN connection
129 network_list = vnf.getNetworkStatus()
130 mgmt_ip = [intf['ip']
131 for intf in network_list if intf['intf_name'] == 'mgmt']
132 self.assertTrue(len(mgmt_ip) > 0)
133 ip_address = mgmt_ip[0]
134 ELAN_list.append(ip_address)
135 print ip_address
136
137 # check ELAN connection by ping over the mgmt network (needs to be
138 # configured as ELAN in the test service)
139 for vnf in self.dc[0].listCompute():
140 network_list = vnf.getNetworkStatus()
141 mgmt_ip = [intf['ip']
142 for intf in network_list if intf['intf_name'] == 'mgmt']
143 self.assertTrue(len(mgmt_ip) > 0)
144 ip_address = mgmt_ip[0]
145 print ELAN_list
146 print ip_address
147 test_ip_list = list(ELAN_list)
148 test_ip_list.remove(ip_address)
149 for ip in test_ip_list:
150 # only take ip address, without netmask
151 p = self.net.ping([vnf], manualdestip=ip.split('/')[0])
152 print p
153 self.assertTrue(p <= 0.0)
154
155 # stop Mininet network
156 self.stopNet()
157 initialize_GK()
158
159 # @unittest.skip("disabled")
160 def test_GK_Api_stop_service(self):
161 # create network
162 self.createNet(ndatacenter=2, nhosts=2)
163 # setup links
164 self.net.addLink(self.dc[0], self.h[0])
165 self.net.addLink(self.dc[0], self.dc[1])
166 self.net.addLink(self.h[1], self.dc[1])
167 # connect dummy GK to data centers
168 sdkg1 = SonataDummyGatekeeperEndpoint("127.0.0.1", 55001)
169 sdkg1.connectDatacenter(self.dc[0])
170 sdkg1.connectDatacenter(self.dc[1])
171 # run the dummy gatekeeper (in another thread, don't block)
172 sdkg1.start()
173 time.sleep(3)
174 # start Mininet network
175 self.startNet()
176 time.sleep(3)
177
178 print "starting tests"
179 # board package
180 files = {"package": open(PACKAGE_PATH, "rb")}
181 r = requests.post("http://127.0.0.1:55001/packages", files=files)
182 self.assertEqual(r.status_code, 201)
183 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
184
185 # instantiate service
186 self.service_uuid = json.loads(r.text).get("service_uuid")
187 r2 = requests.post("http://127.0.0.1:55001/instantiations",
188 data=json.dumps({"service_uuid": self.service_uuid}))
189 self.assertEqual(r2.status_code, 201)
190
191 # give the emulator some time to instantiate everything
192 time.sleep(2)
193
194 # check get request APIs
195 r3 = requests.get("http://127.0.0.1:55001/packages")
196 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
197 r4 = requests.get("http://127.0.0.1:55001/instantiations")
198 self.assertEqual(len(json.loads(r4.text).get(
199 "service_instantiations_list")), 1)
200
201 # check number of running nodes
202 self.assertTrue(len(self.getContainernetContainers()) == 3)
203 self.assertTrue(len(self.net.hosts) == 5)
204 self.assertTrue(len(self.net.switches) == 2)
205 # check compute list result
206 self.assertEqual(len(self.dc[0].listCompute()), 2)
207
208 # stop the service
209 service_instance_uuid = json.loads(
210 r2.text).get("service_instance_uuid")
211 self.assertTrue(service_instance_uuid is not None)
212 requests.delete("http://127.0.0.1:55001/instantiations", data=json.dumps(
213 {"service_uuid": self.service_uuid, "service_instance_uuid": service_instance_uuid}))
214
215 r5 = requests.get("http://127.0.0.1:55001/instantiations")
216 # note that there was 1 instance before
217 self.assertTrue(len(json.loads(r5.text).get(
218 "service_instantiations_list")), 0)
219
220 # stop Mininet network
221 self.stopNet()
222 initialize_GK()
223
224 @unittest.skip("disabled")
225 def test_GK_stress_service(self):
226 # create network
227 self.createNet(ndatacenter=2, nhosts=2)
228 # connect dummy GK to data centers
229 sdkg1 = SonataDummyGatekeeperEndpoint("127.0.0.1", 55002)
230 sdkg1.connectDatacenter(self.dc[0])
231 sdkg1.connectDatacenter(self.dc[1])
232 # run the dummy gatekeeper (in another thread, don't block)
233 sdkg1.start()
234 time.sleep(3)
235 # start Mininet network
236 self.startNet()
237 time.sleep(3)
238
239 print "starting tests"
240 # board package
241 files = {"package": open("misc/sonata-stress-service.son", "rb")}
242 r = requests.post("http://127.0.0.1:55002/packages", files=files)
243 self.assertEqual(r.status_code, 201)
244 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
245
246 # instantiate service
247 self.service_uuid = json.loads(r.text).get("service_uuid")
248 r2 = requests.post("http://127.0.0.1:55002/instantiations",
249 data=json.dumps({"service_uuid": self.service_uuid}))
250 self.assertEqual(r2.status_code, 201)
251
252 # give the emulator some time to instantiate everything
253 time.sleep(2)
254
255 # check get request APIs
256 r3 = requests.get("http://127.0.0.1:55002/packages")
257 self.assertEqual(len(json.loads(r3.text).get("service_uuid_list")), 1)
258 r4 = requests.get("http://127.0.0.1:55002/instantiations")
259 self.assertEqual(len(json.loads(r4.text).get(
260 "service_instantiations_list")), 1)
261
262 # stop the service
263 service_instance_uuid = json.loads(
264 r2.text).get("service_instance_uuid")
265 self.assertTrue(service_instance_uuid is not None)
266 requests.delete("http://127.0.0.1:55002/instantiations", data=json.dumps(
267 {"service_uuid": self.service_uuid, "service_instance_uuid": service_instance_uuid}))
268
269 r5 = requests.get("http://127.0.0.1:55002/instantiations")
270 # note that there was 1 instance before
271 self.assertTrue(len(json.loads(r5.text).get(
272 "service_instantiations_list")), 0)
273
274 # stop Mininet network
275 self.stopNet()
276 initialize_GK()