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.
34 # pylint: disable=W0621
36 from __future__
import unicode_literals
39 from itertools
import izip
41 from textwrap
import wrap
43 from six
.moves
import range
45 from ...tests
.db_helpers
import uuid
, sha1
52 # In the following functions, the identifiers should be simple integers
55 def wim(identifier
=0):
56 return {'name': 'wim%d' % identifier
,
57 'uuid': uuid('wim%d' % identifier
),
58 'wim_url': 'localhost',
62 def tenant(identifier
=0):
63 return {'name': 'tenant%d' % identifier
,
64 'uuid': uuid('tenant%d' % identifier
)}
67 def wim_account(wim
, tenant
):
68 return {'name': 'wim-account%d%d' % (tenant
, wim
),
69 'uuid': uuid('wim-account%d%d' % (tenant
, wim
)),
70 'user': 'user%d%d' % (tenant
, wim
),
71 'password': 'password%d%d' % (tenant
, wim
),
72 'wim_id': uuid('wim%d' % wim
),
76 def wim_tenant_association(wim
, tenant
):
77 return {'nfvo_tenant_id': uuid('tenant%d' % tenant
),
78 'wim_id': uuid('wim%d' % wim
),
79 'wim_account_id': uuid('wim-account%d%d' % (tenant
, wim
))}
82 def wim_set(identifier
=0, tenant
=0):
83 """Records necessary to create a WIM and connect it to a tenant"""
85 {'wims': [wim(identifier
)]},
86 {'wim_accounts': [wim_account(identifier
, tenant
)]},
87 {'wim_nfvo_tenants': [wim_tenant_association(identifier
, tenant
)]}
91 def _datacenter_to_switch_port(dc_id
, port
=None):
93 switch
= ':'.join(wrap(('%0' + str(digits
) + 'x') % int(dc_id
), 2))
94 return (switch
, str((port
or int(dc_id
)) + 1))
97 def datacenter(identifier
, external_ports_config
=False):
98 config
= '' if not external_ports_config
else json
.dumps({
99 'external_connections': [
101 'provider:physical_network': 'provider',
102 'encapsulation_type': 'vlan'},
104 dict(izip(('switch', 'port'),
105 _datacenter_to_switch_port(identifier
)))}
108 return {'uuid': uuid('dc%d' % identifier
),
109 'name': 'dc%d' % identifier
,
111 'vim_url': 'localhost',
115 def datacenter_account(datacenter
, tenant
):
116 return {'name': 'dc-account%d%d' % (tenant
, datacenter
),
117 'uuid': uuid('dc-account%d%d' % (tenant
, datacenter
)),
118 'datacenter_id': uuid('dc%d' % datacenter
),
122 def datacenter_tenant_association(datacenter
, tenant
):
123 return {'nfvo_tenant_id': uuid('tenant%d' % tenant
),
124 'datacenter_id': uuid('dc%d' % datacenter
),
125 'datacenter_tenant_id': uuid('dc-account%d%d' % (tenant
, datacenter
))}
128 def datacenter_set(identifier
=0, tenant
=0):
129 """Records necessary to create a datacenter and connect it to a tenant"""
131 {'datacenters': [datacenter(identifier
)]},
132 {'datacenter_tenants': [datacenter_account(identifier
, tenant
)]},
133 {'tenants_datacenters': [
134 datacenter_tenant_association(identifier
, tenant
)
139 def wim_port_mapping(wim
, datacenter
,
140 pop_dpid
='AA:AA:AA:AA:AA:AA:AA:AA', pop_port
=None,
141 wan_dpid
='BB:BB:BB:BB:BB:BB:BB:BB', wan_port
=None):
142 mapping_info
= {'mapping_type': 'dpid-port',
143 'wan_switch_dpid': wan_dpid
,
144 'wan_switch_port': (str(wan_port
) if wan_port
else
145 str(int(datacenter
) + int(wim
) + 1))}
146 id_
= 'dpid-port|' + sha1(json
.dumps(mapping_info
, sort_keys
=True))
148 return {'wim_id': uuid('wim%d' % wim
),
149 'datacenter_id': uuid('dc%d' % datacenter
),
150 'pop_switch_dpid': pop_dpid
,
151 'pop_switch_port': (str(pop_port
) if pop_port
else
152 str(int(datacenter
) + int(wim
) + 1)),
153 # ^ Datacenter router have one port managed by each WIM
154 'wan_service_endpoint_id': id_
,
155 # ^ WIM managed router have one port connected to each DC
156 'wan_service_mapping_info': json
.dumps(mapping_info
)}
159 def processed_port_mapping(wim
, datacenter
,
161 pop_dpid
='AA:AA:AA:AA:AA:AA:AA:AA',
162 wan_dpid
='BB:BB:BB:BB:BB:BB:BB:BB'):
163 """Emulate the response of the Persistence class, where the records in the
164 data base are grouped by wim and datacenter
167 'wim_id': uuid('wim%d' % wim
),
168 'datacenter_id': uuid('dc%d' % datacenter
),
169 'pop_wan_mappings': [
170 {'pop_switch_dpid': pop_dpid
,
171 'pop_switch_port': wim
+ 1 + i
,
172 'wan_service_endpoint_id':
173 sha1('dpid-port|%s|%d' % (wan_dpid
, datacenter
+ 1 + i
)),
174 'wan_service_mapping_info': {
175 'mapping_type': 'dpid-port',
176 'wan_switch_dpid': wan_dpid
,
177 'wan_switch_port': datacenter
+ 1 + i
}}
178 for i
in range(num_pairs
)
183 def consistent_set(num_wims
=NUM_WIMS
, num_tenants
=NUM_TENANTS
,
184 num_datacenters
=NUM_DATACENTERS
,
185 external_ports_config
=False):
187 {'nfvo_tenants': [tenant(i
) for i
in range(num_tenants
)]},
188 {'wims': [wim(j
) for j
in range(num_wims
)]},
191 for i
in range(num_tenants
)
192 for j
in range(num_wims
)
194 {'wim_nfvo_tenants': [
195 wim_tenant_association(j
, i
)
196 for i
in range(num_tenants
)
197 for j
in range(num_wims
)
200 datacenter(k
, external_ports_config
)
201 for k
in range(num_datacenters
)
203 {'datacenter_tenants': [
204 datacenter_account(k
, i
)
205 for i
in range(num_tenants
)
206 for k
in range(num_datacenters
)
208 {'tenants_datacenters': [
209 datacenter_tenant_association(k
, i
)
210 for i
in range(num_tenants
)
211 for k
in range(num_datacenters
)
213 {'wim_port_mappings': [
214 (wim_port_mapping(j
, k
, *_datacenter_to_switch_port(k
))
215 if external_ports_config
else wim_port_mapping(j
, k
))
216 for j
in range(num_wims
)
217 for k
in range(num_datacenters
)
222 def instance_nets(num_datacenters
=2, num_links
=2, status
='BUILD'):
223 """Example of multi-site deploy with N datacenters and M WAN links between
224 them (e.g M = 2 -> back and forth)
227 {'uuid': uuid('net%d%d' % (k
, l
)),
228 'datacenter_id': uuid('dc%d' % k
),
229 'datacenter_tenant_id': uuid('dc-account0%d' % k
),
230 'instance_scenario_id': uuid('nsr0'),
231 # ^ instance_scenario_id == NS Record id
232 'sce_net_id': uuid('vld%d' % l
),
233 # ^ scenario net id == VLD id
237 for k
in range(num_datacenters
)
238 for l
in range(num_links
)
242 def wim_actions(action
='CREATE', status
='SCHEDULED',
243 action_id
=None, instance
=0,
244 wim
=0, tenant
=0, num_links
=1):
245 """Create a list of actions for the WIM,
248 action: type of action (CREATE) by default
249 wim: WIM fixture index to create actions for
250 tenant: tenant fixture index to create actions for
251 num_links: number of WAN links to be established by each WIM
254 action_id
= action_id
or 'ACTION-{}'.format(time())
259 'wim_internal_id': uuid('-wim-net%d%d%d' % (wim
, instance
, link
)),
260 'wim_account_id': uuid('wim-account%d%d' % (tenant
, wim
)),
261 'instance_action_id': action_id
,
262 'item': 'instance_wim_nets',
263 'item_id': uuid('wim-net%d%d%d' % (wim
, instance
, link
)),
266 'created_at': time(),
267 'modified_at': time(),
270 for link
in range(num_links
)
274 def instance_action(tenant
=0, instance
=0, action_id
=None,
275 num_tasks
=1, num_done
=0, num_failed
=0):
276 action_id
= action_id
or 'ACTION-{}'.format(time())
280 'tenant_id': uuid('tenant%d' % tenant
),
281 'instance_id': uuid('nsr%d' % instance
),
282 'number_tasks': num_tasks
,
283 'number_done': num_done
,
284 'number_failed': num_failed
,
288 def instance_wim_nets(instance
=0, wim
=0, num_links
=1,
289 status
='SCHEDULED_CREATION'):
290 """Example of multi-site deploy with N wims and M WAN links between
291 them (e.g M = 2 -> back and forth)
295 {'uuid': uuid('wim-net%d%d%d' % (wim
, instance
, l
)),
296 'wim_id': uuid('wim%d' % wim
),
297 'wim_account_id': uuid('wim-account%d' % wim
),
298 'wim_internal_id': uuid('-net%d%d' % (wim
, l
)),
299 'instance_scenario_id': uuid('nsr%d' % instance
),
300 # ^ instance_scenario_id == NS Record id
301 'sce_net_id': uuid('vld%d' % l
),
302 # ^ scenario net id == VLD id
305 for l
in range(num_links
)
309 def instance_vm(instance
=0, vim_info
=None):
310 vim_info
= {'OS-EXT-SRV-ATTR:hypervisor_hostname': 'host%d' % instance
}
312 'uuid': uuid('vm%d' % instance
),
313 'instance_vnf_id': uuid('vnf%d' % instance
),
314 'vm_id': uuid('vm%d' % instance
),
315 'vim_vm_id': uuid('vm%d' % instance
),
317 'vim_info': vim_info
,
321 def instance_interface(instance
=0, interface
=0, datacenter
=0, link
=0):
323 'uuid': uuid('interface%d%d' % (instance
, interface
)),
324 'instance_vm_id': uuid('vm%d' % instance
),
325 'instance_net_id': uuid('net%d%d' % (datacenter
, link
)),
326 'interface_id': uuid('iface%d' % interface
),