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