1 # -*- coding: utf-8 -*-
3 # Copyright 2018 University of Bristol - High Performance Networks Research
7 # Contributors: Anderson Bravalheri, Dimitrios Gkounis, Abubakar Siddique
8 # Muqaddas, Navdeep Uniyal, Reza Nejabati and Dimitra Simeonidou
10 # Licensed under the Apache License, Version 2.0 (the "License"); you may
11 # not use this file except in compliance with the License. You may obtain
12 # a copy of the License at
14 # http://www.apache.org/licenses/LICENSE-2.0
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19 # License for the specific language governing permissions and limitations
22 # For those usages not covered by the Apache License, Version 2.0 please
23 # contact with: <highperformance-networks@bristol.ac.uk>
25 # Neither the name of the University of Bristol nor the names of its
26 # contributors may be used to endorse or promote products derived from
27 # this software without specific prior written permission.
29 # This work has been performed in the context of DCMS UK 5G Testbeds
30 # & Trials Programme and in the framework of the Metro-Haul project -
31 # funded by the European Commission under Grant number 761727 through the
32 # Horizon 2020 and 5G-PPP programmes.
37 from unittest
.mock
import MagicMock
39 from . import fixtures
as eg
40 from ...tests
.db_helpers
import TestCaseWithDatabasePerTest
, uuid
41 from ..errors
import NoWimConnectedToDatacenters
42 from ..engine
import WimEngine
43 from ..persistence
import WimPersistence
46 class TestWimEngineDbMethods(TestCaseWithDatabasePerTest
):
48 super(TestWimEngineDbMethods
, self
).setUp()
49 self
.persist
= WimPersistence(self
.db
)
50 self
.engine
= WimEngine(persistence
=self
.persist
)
51 self
.addCleanup(self
.engine
.stop_threads
)
53 def populate(self
, seeds
=None):
54 super(TestWimEngineDbMethods
, self
).populate(
55 seeds
or eg
.consistent_set())
57 def test_find_common_wims(self
):
58 # Given we have 2 WIM, 3 datacenters, but just 1 of the WIMs have
60 self
.populate([{'nfvo_tenants': [eg
.tenant(0)]}] +
63 eg
.datacenter_set(0, 0) +
64 eg
.datacenter_set(1, 0) +
65 eg
.datacenter_set(2, 0) +
66 [{'wim_port_mappings': [
67 eg
.wim_port_mapping(0, 0),
68 eg
.wim_port_mapping(0, 1),
69 eg
.wim_port_mapping(0, 2)]}])
71 # When we retrieve the wims interconnecting some datacenters
72 wim_ids
= self
.engine
.find_common_wims(
73 [uuid('dc0'), uuid('dc1'), uuid('dc2')], tenant
='tenant0')
75 # Then we should have just the first wim
76 self
.assertEqual(len(wim_ids
), 1)
77 self
.assertEqual(wim_ids
[0], uuid('wim0'))
79 def test_find_common_wims_multiple(self
):
80 # Given we have 2 WIM, 3 datacenters, and all the WIMs have access to
82 self
.populate([{'nfvo_tenants': [eg
.tenant(0)]}] +
85 eg
.datacenter_set(0, 0) +
86 eg
.datacenter_set(1, 0) +
87 eg
.datacenter_set(2, 0) +
88 [{'wim_port_mappings': [
89 eg
.wim_port_mapping(0, 0),
90 eg
.wim_port_mapping(0, 1),
91 eg
.wim_port_mapping(0, 2),
92 eg
.wim_port_mapping(1, 0),
93 eg
.wim_port_mapping(1, 1),
94 eg
.wim_port_mapping(1, 2)]}])
96 # When we retrieve the wims interconnecting tree datacenters
97 wim_ids
= self
.engine
.find_common_wims(
98 [uuid('dc0'), uuid('dc1'), uuid('dc2')], tenant
='tenant0')
100 # Then we should have all the wims
101 self
.assertEqual(len(wim_ids
), 2)
102 self
.assertItemsEqual(wim_ids
, [uuid('wim0'), uuid('wim1')])
104 def test_find_common_wim(self
):
105 # Given we have 1 WIM, 3 datacenters but the WIM have access to just 2
107 self
.populate([{'nfvo_tenants': [eg
.tenant(0)]}] +
109 eg
.datacenter_set(0, 0) +
110 eg
.datacenter_set(1, 0) +
111 eg
.datacenter_set(2, 0) +
112 [{'wim_port_mappings': [
113 eg
.wim_port_mapping(0, 0),
114 eg
.wim_port_mapping(0, 1)]}])
116 # When we retrieve the common wim for the 2 datacenter that are
118 wim_id
= self
.engine
.find_common_wim(
119 [uuid('dc0'), uuid('dc1')], tenant
='tenant0')
121 # Then we should find the wim
122 self
.assertEqual(wim_id
, uuid('wim0'))
124 # When we try to retrieve the common wim for the all the datacenters
125 # Then a NoWimConnectedToDatacenters exception should be raised
126 with self
.assertRaises(NoWimConnectedToDatacenters
):
127 self
.engine
.find_common_wim(
128 [uuid('dc0'), uuid('dc1'), uuid('dc2')], tenant
='tenant0')
130 def test_find_common_wim__different_tenants(self
):
131 # Given we have 1 WIM and 2 datacenters connected but the WIMs don't
132 # belong to the tenant we have access to...
133 self
.populate([{'nfvo_tenants': [eg
.tenant(0), eg
.tenant(1)]}] +
135 eg
.datacenter_set(0, 0) +
136 eg
.datacenter_set(1, 0) +
137 [{'wim_port_mappings': [
138 eg
.wim_port_mapping(0, 0),
139 eg
.wim_port_mapping(0, 1)]}])
141 # When we retrieve the common wim for the 2 datacenter that are
142 # interconnected, but using another tenant,
143 # Then we should get an exception
144 with self
.assertRaises(NoWimConnectedToDatacenters
):
145 self
.engine
.find_common_wim(
146 [uuid('dc0'), uuid('dc1')], tenant
='tenant1')
149 class TestWimEngine(unittest
.TestCase
):
150 def test_derive_wan_link(self
):
151 # Given we have 2 datacenters connected by the same WIM, with port
152 # mappings registered
153 mappings
= [eg
.processed_port_mapping(0, 0),
154 eg
.processed_port_mapping(0, 1)]
156 get_wim_port_mappings
=MagicMock(return_value
=mappings
))
158 engine
= WimEngine(persistence
=persist
)
159 self
.addCleanup(engine
.stop_threads
)
161 # When we receive a list of 4 instance nets, representing
162 # 2 VLDs connecting 2 datacenters each
163 instance_nets
= eg
.instance_nets(2, 2)
164 wan_links
= engine
.derive_wan_links({}, instance_nets
, uuid('tenant0'))
166 # Then we should derive 2 wan_links with the same instance_scenario_id
167 # and different scenario_network_id
168 self
.assertEqual(len(wan_links
), 2)
169 for link
in wan_links
:
170 self
.assertEqual(link
['instance_scenario_id'], uuid('nsr0'))
171 # Each VLD needs a network to be created in each datacenter
172 self
.assertItemsEqual([li
['sce_net_id'] for li
in wan_links
],
173 [uuid('vld0'), uuid('vld1')])
176 if __name__
== '__main__':