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