5GTANGO LLCM: Made APIs compatible with tng-cli
[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 initialize_GK()
48 # create network
49 self.createNet(nswitches=0, ndatacenter=2, nhosts=2,
50 ndockers=0, enable_learning=True)
51 # setup links
52 self.net.addLink(self.dc[0], self.h[0])
53 self.net.addLink(self.dc[0], self.dc[1])
54 self.net.addLink(self.h[1], self.dc[1])
55 # connect llcm to data centers
56 sdkg1 = TangoLLCMEndpoint("127.0.0.1", 56000)
57 sdkg1.connectDatacenter(self.dc[0])
58 sdkg1.connectDatacenter(self.dc[1])
59 # run the dummy gatekeeper (in another thread, don't block)
60 sdkg1.start()
61 time.sleep(3)
62 # start Mininet network
63 self.startNet()
64 time.sleep(3)
65
66 # board package
67 files = {"package": open(PACKAGE_PATH, "rb")}
68 r = requests.post("http://127.0.0.1:56000/packages", files=files)
69 self.assertEqual(r.status_code, 201)
70 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
71
72 # instantiate service
73 self.service_uuid = json.loads(r.text).get("service_uuid")
74 r2 = requests.post("http://127.0.0.1:56000/instantiations",
75 data=json.dumps({"service_uuid": self.service_uuid}))
76 self.assertEqual(r2.status_code, 201)
77
78 # give the emulator some time to instantiate everything
79 time.sleep(2)
80
81 # check get request APIs
82 r3 = requests.get("http://127.0.0.1:56000/packages")
83 self.assertEqual(len(json.loads(r3.text)), 1)
84 r4 = requests.get("http://127.0.0.1:56000/instantiations")
85 self.assertEqual(len(json.loads(r4.text)), 1)
86
87 # check number of running nodes
88 self.assertEqual(len(self.getContainernetContainers()), 2)
89 self.assertEqual(len(self.net.hosts), 4)
90 self.assertEqual(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 initialize_GK()
167 # create network
168 self.createNet(ndatacenter=2, nhosts=2)
169 # setup links
170 self.net.addLink(self.dc[0], self.h[0])
171 self.net.addLink(self.dc[0], self.dc[1])
172 self.net.addLink(self.h[1], self.dc[1])
173 # connect dummy GK to data centers
174 sdkg1 = TangoLLCMEndpoint("127.0.0.1", 56001)
175 sdkg1.connectDatacenter(self.dc[0])
176 sdkg1.connectDatacenter(self.dc[1])
177 # run the dummy gatekeeper (in another thread, don't block)
178 sdkg1.start()
179 time.sleep(3)
180 # start Mininet network
181 self.startNet()
182 time.sleep(3)
183
184 print "starting tests"
185 # board package
186 files = {"package": open(PACKAGE_PATH, "rb")}
187 r = requests.post("http://127.0.0.1:56001/packages", files=files)
188 self.assertEqual(r.status_code, 201)
189 self.assertTrue(json.loads(r.text).get("service_uuid") is not None)
190
191 # instantiate service
192 self.service_uuid = json.loads(r.text).get("service_uuid")
193 r2 = requests.post("http://127.0.0.1:56001/instantiations",
194 data=json.dumps({"service_uuid": self.service_uuid}))
195 self.assertEqual(r2.status_code, 201)
196
197 # give the emulator some time to instantiate everything
198 time.sleep(2)
199
200 # check get request APIs
201 r3 = requests.get("http://127.0.0.1:56001/packages")
202 self.assertEqual(len(json.loads(r3.text)), 1)
203 r4 = requests.get("http://127.0.0.1:56001/instantiations")
204 self.assertEqual(len(json.loads(r4.text)), 1)
205
206 # check number of running nodes
207 self.assertEqual(len(self.getContainernetContainers()), 2)
208 self.assertEqual(len(self.net.hosts), 4)
209 self.assertEqual(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.assertEqual(len(json.loads(r5.text)), 0)
224
225 # stop Mininet network
226 self.stopNet()
227 initialize_GK()