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.
35 from __future__
import unicode_literals
39 from mock
import MagicMock
41 from . import fixtures
as eg
42 from ...tests
.db_helpers
import TestCaseWithDatabasePerTest
, uuid
43 from ..errors
import NoWimConnectedToDatacenters
44 from ..engine
import WimEngine
45 from ..persistence
import WimPersistence
48 class TestWimEngineDbMethods(TestCaseWithDatabasePerTest
):
50 super(TestWimEngineDbMethods
, self
).setUp()
51 self
.persist
= WimPersistence(self
.db
)
52 self
.engine
= WimEngine(persistence
=self
.persist
)
53 self
.addCleanup(self
.engine
.stop_threads
)
55 def populate(self
, seeds
=None):
56 super(TestWimEngineDbMethods
, self
).populate(
57 seeds
or eg
.consistent_set())
59 def test_find_common_wims(self
):
60 # Given we have 2 WIM, 3 datacenters, but just 1 of the WIMs have
62 self
.populate([{'nfvo_tenants': [eg
.tenant(0)]}] +
65 eg
.datacenter_set(0, 0) +
66 eg
.datacenter_set(1, 0) +
67 eg
.datacenter_set(2, 0) +
68 [{'wim_port_mappings': [
69 eg
.wim_port_mapping(0, 0),
70 eg
.wim_port_mapping(0, 1),
71 eg
.wim_port_mapping(0, 2)]}])
73 # When we retrieve the wims interconnecting some datacenters
74 wim_ids
= self
.engine
.find_common_wims(
75 [uuid('dc0'), uuid('dc1'), uuid('dc2')], tenant
='tenant0')
77 # Then we should have just the first wim
78 self
.assertEqual(len(wim_ids
), 1)
79 self
.assertEqual(wim_ids
[0], uuid('wim0'))
81 def test_find_common_wims_multiple(self
):
82 # Given we have 2 WIM, 3 datacenters, and all the WIMs have access to
84 self
.populate([{'nfvo_tenants': [eg
.tenant(0)]}] +
87 eg
.datacenter_set(0, 0) +
88 eg
.datacenter_set(1, 0) +
89 eg
.datacenter_set(2, 0) +
90 [{'wim_port_mappings': [
91 eg
.wim_port_mapping(0, 0),
92 eg
.wim_port_mapping(0, 1),
93 eg
.wim_port_mapping(0, 2),
94 eg
.wim_port_mapping(1, 0),
95 eg
.wim_port_mapping(1, 1),
96 eg
.wim_port_mapping(1, 2)]}])
98 # When we retrieve the wims interconnecting tree datacenters
99 wim_ids
= self
.engine
.find_common_wims(
100 [uuid('dc0'), uuid('dc1'), uuid('dc2')], tenant
='tenant0')
102 # Then we should have all the wims
103 self
.assertEqual(len(wim_ids
), 2)
104 self
.assertItemsEqual(wim_ids
, [uuid('wim0'), uuid('wim1')])
106 def test_find_common_wim(self
):
107 # Given we have 1 WIM, 3 datacenters but the WIM have access to just 2
109 self
.populate([{'nfvo_tenants': [eg
.tenant(0)]}] +
111 eg
.datacenter_set(0, 0) +
112 eg
.datacenter_set(1, 0) +
113 eg
.datacenter_set(2, 0) +
114 [{'wim_port_mappings': [
115 eg
.wim_port_mapping(0, 0),
116 eg
.wim_port_mapping(0, 1)]}])
118 # When we retrieve the common wim for the 2 datacenter that are
120 wim_id
= self
.engine
.find_common_wim(
121 [uuid('dc0'), uuid('dc1')], tenant
='tenant0')
123 # Then we should find the wim
124 self
.assertEqual(wim_id
, uuid('wim0'))
126 # When we try to retrieve the common wim for the all the datacenters
127 # Then a NoWimConnectedToDatacenters exception should be raised
128 with self
.assertRaises(NoWimConnectedToDatacenters
):
129 self
.engine
.find_common_wim(
130 [uuid('dc0'), uuid('dc1'), uuid('dc2')], tenant
='tenant0')
132 def test_find_common_wim__different_tenants(self
):
133 # Given we have 1 WIM and 2 datacenters connected but the WIMs don't
134 # belong to the tenant we have access to...
135 self
.populate([{'nfvo_tenants': [eg
.tenant(0), eg
.tenant(1)]}] +
137 eg
.datacenter_set(0, 0) +
138 eg
.datacenter_set(1, 0) +
139 [{'wim_port_mappings': [
140 eg
.wim_port_mapping(0, 0),
141 eg
.wim_port_mapping(0, 1)]}])
143 # When we retrieve the common wim for the 2 datacenter that are
144 # interconnected, but using another tenant,
145 # Then we should get an exception
146 with self
.assertRaises(NoWimConnectedToDatacenters
):
147 self
.engine
.find_common_wim(
148 [uuid('dc0'), uuid('dc1')], tenant
='tenant1')
151 class TestWimEngine(unittest
.TestCase
):
152 def test_derive_wan_link(self
):
153 # Given we have 2 datacenters connected by the same WIM, with port
154 # mappings registered
155 mappings
= [eg
.processed_port_mapping(0, 0),
156 eg
.processed_port_mapping(0, 1)]
158 get_wim_port_mappings
=MagicMock(return_value
=mappings
))
160 engine
= WimEngine(persistence
=persist
)
161 self
.addCleanup(engine
.stop_threads
)
163 # When we receive a list of 4 instance nets, representing
164 # 2 VLDs connecting 2 datacenters each
165 instance_nets
= eg
.instance_nets(2, 2)
166 wan_links
= engine
.derive_wan_links(
167 instance_nets
, uuid('tenant0'))
169 # Then we should derive 2 wan_links with the same instance_scenario_id
170 # and different scenario_network_id
171 self
.assertEqual(len(wan_links
), 2)
172 for link
in wan_links
:
173 self
.assertEqual(link
['instance_scenario_id'], uuid('nsr0'))
174 # Each VLD needs a network to be created in each datacenter
175 self
.assertItemsEqual([l
['sce_net_id'] for l
in wan_links
],
176 [uuid('vld0'), uuid('vld1')])
179 if __name__
== '__main__':