migration to python3 (#1)
[osm/vim-emu.git] / src / emuvim / test / unittests / test_tango_llcm.py
1 # Copyright (c) 2018 SONATA-NFV, 5GTANGO 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, 5GTANGO, 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 #
27 # This work has also been performed in the framework of the 5GTANGO project,
28 # funded by the European Commission under Grant number 761493 through
29 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
30 # acknowledge the contributions of their colleagues of the 5GTANGO
31 # partner consortium (www.5gtango.eu).
32 import time
33 import requests
34 import json
35 from emuvim.test.base import SimpleTestTopology
36 from emuvim.api.tango import TangoLLCMEndpoint
37 from emuvim.api.tango.llcm import initialize_GK, parse_interface
38 from ipaddress import ip_network
39
40
41 PACKAGE_PATH = "misc/eu.5gtango.emulator-example-service.0.1.tgo"
42
43
44 class testTangoLLCM(SimpleTestTopology):
45
46 # @unittest.skip("disabled")
47 def test_tango_llcm_start_service(self):
48 initialize_GK()
49 # create network
50 self.createNet(nswitches=0, ndatacenter=2, nhosts=2,
51 ndockers=0, enable_learning=True)
52 # setup links
53 self.net.addLink(self.dc[0], self.h[0])
54 self.net.addLink(self.dc[0], self.dc[1])
55 self.net.addLink(self.h[1], self.dc[1])
56 # connect llcm to data centers
57 sdkg1 = TangoLLCMEndpoint("127.0.0.1", 56000)
58 sdkg1.connectDatacenter(self.dc[0])
59 sdkg1.connectDatacenter(self.dc[1])
60 # run the dummy gatekeeper (in another thread, don't block)
61 sdkg1.start()
62 time.sleep(3)
63 # start Mininet network
64 self.startNet()
65 time.sleep(3)
66
67 # board package
68 files = {"package": open(PACKAGE_PATH, "rb")}
69 r = requests.post("http://127.0.0.1:56000/packages", files=files)
70 self.assertEqual(r.status_code, 201)
71 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
72
73 # instantiate service
74 self.service_uuid = json.loads(r.text).get("service_uuid")
75 r2 = requests.post("http://127.0.0.1:56000/instantiations",
76 data=json.dumps({"service_uuid": self.service_uuid}))
77 self.assertEqual(r2.status_code, 201)
78
79 # give the emulator some time to instantiate everything
80 time.sleep(2)
81
82 # check get request APIs
83 r3 = requests.get("http://127.0.0.1:56000/packages")
84 self.assertEqual(len(json.loads(r3.text)), 1)
85 r4 = requests.get("http://127.0.0.1:56000/instantiations")
86 self.assertEqual(len(json.loads(r4.text)), 1)
87
88 # check number of running nodes
89 self.assertEqual(len(self.getContainernetContainers()), 2)
90 self.assertEqual(len(self.net.hosts), 4)
91 self.assertEqual(len(self.net.switches), 2)
92 # check compute list result (considering placement)
93 self.assertEqual(len(self.dc[0].listCompute()), 1)
94 self.assertEqual(len(self.dc[1].listCompute()), 1)
95 # check connectivity by using ping
96 ELAN_list = []
97
98 # check E-Line connection, by checking the IP addresses
99 for link in self.net.deployed_elines:
100 vnf_src, intf_src = parse_interface(
101 link['connection_points_reference'][0])
102 print(vnf_src, intf_src)
103 src = self.net.getNodeByName(vnf_src)
104 if not src:
105 continue
106 network_list = src.getNetworkStatus()
107 src_ip = [intf['ip']
108 for intf in network_list if intf['intf_name'] == intf_src][0]
109 src_mask = [intf['netmask']
110 for intf in network_list if intf['intf_name'] == intf_src][0]
111
112 vnf_dst, intf_dst = parse_interface(
113 link['connection_points_reference'][1])
114 dst = self.net.getNodeByName(vnf_dst)
115 if not dst:
116 continue
117 network_list = dst.getNetworkStatus()
118 dst_ip = [intf['ip']
119 for intf in network_list if intf['intf_name'] == intf_dst][0]
120 dst_mask = [intf['netmask']
121 for intf in network_list if intf['intf_name'] == intf_dst][0]
122
123 print("src = {0}:{1} ip={2} ".format(
124 vnf_src, intf_src, src_ip, src_mask))
125 print("dst = {0}:{1} ip={2} ".format(
126 vnf_dst, intf_dst, dst_ip, dst_mask))
127
128 # check if the E-Line IP's are in the same subnet
129 ret = ip_network(u'{0}'.format(src_ip, src_mask), strict=False)\
130 .compare_networks(ip_network(u'{0}'.format(dst_ip, dst_mask), strict=False))
131 self.assertTrue(ret == 0)
132
133 for vnf in self.dc[0].listCompute():
134 # check E LAN connection
135 network_list = vnf.getNetworkStatus()
136 mgmt_ip = [intf['ip']
137 for intf in network_list if intf['intf_name'] == 'mgmt']
138 self.assertTrue(len(mgmt_ip) > 0)
139 ip_address = mgmt_ip[0]
140 ELAN_list.append(ip_address)
141 print(ip_address)
142
143 # check ELAN connection by ping over the mgmt network (needs to be
144 # configured as ELAN in the test service)
145 for vnf in self.dc[0].listCompute():
146 network_list = vnf.getNetworkStatus()
147 mgmt_ip = [intf['ip']
148 for intf in network_list if intf['intf_name'] == 'mgmt']
149 self.assertTrue(len(mgmt_ip) > 0)
150 ip_address = mgmt_ip[0]
151 print(ELAN_list)
152 print(ip_address)
153 test_ip_list = list(ELAN_list)
154 test_ip_list.remove(ip_address)
155 for ip in test_ip_list:
156 # only take ip address, without netmask
157 p = self.net.ping([vnf], manualdestip=ip.split('/')[0])
158 print(p)
159 self.assertTrue(p <= 0.0)
160
161 # stop Mininet network
162 self.stopNet()
163 initialize_GK()
164
165 # @unittest.skip("disabled")
166 def test_tango_llcm_stop_service(self):
167 initialize_GK()
168 # create network
169 self.createNet(ndatacenter=2, nhosts=2)
170 # setup links
171 self.net.addLink(self.dc[0], self.h[0])
172 self.net.addLink(self.dc[0], self.dc[1])
173 self.net.addLink(self.h[1], self.dc[1])
174 # connect dummy GK to data centers
175 sdkg1 = TangoLLCMEndpoint("127.0.0.1", 56001)
176 sdkg1.connectDatacenter(self.dc[0])
177 sdkg1.connectDatacenter(self.dc[1])
178 # run the dummy gatekeeper (in another thread, don't block)
179 sdkg1.start()
180 time.sleep(3)
181 # start Mininet network
182 self.startNet()
183 time.sleep(3)
184
185 print("starting tests")
186 # board package
187 files = {"package": open(PACKAGE_PATH, "rb")}
188 r = requests.post("http://127.0.0.1:56001/packages", files=files)
189 self.assertEqual(r.status_code, 201)
190 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
191
192 # instantiate service
193 self.service_uuid = json.loads(r.text).get("service_uuid")
194 r2 = requests.post("http://127.0.0.1:56001/instantiations",
195 data=json.dumps({"service_uuid": self.service_uuid}))
196 self.assertEqual(r2.status_code, 201)
197
198 # give the emulator some time to instantiate everything
199 time.sleep(2)
200
201 # check get request APIs
202 r3 = requests.get("http://127.0.0.1:56001/packages")
203 self.assertEqual(len(json.loads(r3.text)), 1)
204 r4 = requests.get("http://127.0.0.1:56001/instantiations")
205 self.assertEqual(len(json.loads(r4.text)), 1)
206
207 # check number of running nodes
208 self.assertEqual(len(self.getContainernetContainers()), 2)
209 self.assertEqual(len(self.net.hosts), 4)
210 self.assertEqual(len(self.net.switches), 2)
211 # check compute list result (considering placement)
212 self.assertEqual(len(self.dc[0].listCompute()), 1)
213 self.assertEqual(len(self.dc[1].listCompute()), 1)
214
215 # stop the service
216 service_instance_uuid = json.loads(
217 r2.text).get("service_instance_uuid")
218 self.assertTrue(service_instance_uuid is not None)
219 requests.delete("http://127.0.0.1:56001/instantiations", data=json.dumps(
220 {"service_uuid": self.service_uuid, "service_instance_uuid": service_instance_uuid}))
221
222 r5 = requests.get("http://127.0.0.1:56001/instantiations")
223 # note that there was 1 instance before
224 self.assertEqual(len(json.loads(r5.text)), 0)
225
226 # stop Mininet network
227 self.stopNet()
228 initialize_GK()