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
40 from textwrap
import wrap
42 from ...tests
.db_helpers
import uuid
, sha1
49 # In the following functions, the identifiers should be simple integers
52 def wim(identifier
=0):
53 return {'name': 'wim%d' % identifier
,
54 'uuid': uuid('wim%d' % identifier
),
55 'wim_url': 'localhost',
59 def tenant(identifier
=0):
60 return {'name': 'tenant%d' % identifier
,
61 'uuid': uuid('tenant%d' % identifier
)}
64 def wim_account(wim
, tenant
):
65 return {'name': 'wim-account%d%d' % (tenant
, wim
),
66 'uuid': uuid('wim-account%d%d' % (tenant
, wim
)),
67 'user': 'user%d%d' % (tenant
, wim
),
68 'password': 'password%d%d' % (tenant
, wim
),
69 'wim_id': uuid('wim%d' % wim
),
73 def wim_tenant_association(wim
, tenant
):
74 return {'nfvo_tenant_id': uuid('tenant%d' % tenant
),
75 'wim_id': uuid('wim%d' % wim
),
76 'wim_account_id': uuid('wim-account%d%d' % (tenant
, wim
))}
79 def wim_set(identifier
=0, tenant
=0):
80 """Records necessary to create a WIM and connect it to a tenant"""
82 {'wims': [wim(identifier
)]},
83 {'wim_accounts': [wim_account(identifier
, tenant
)]},
84 {'wim_nfvo_tenants': [wim_tenant_association(identifier
, tenant
)]}
88 def _datacenter_to_switch_port(dc_id
, port
=None):
90 switch
= ':'.join(wrap(('%0' + str(digits
) + 'x') % int(dc_id
), 2))
91 return (switch
, str((port
or int(dc_id
)) + 1))
94 def datacenter(identifier
, external_ports_config
=False):
95 config
= '' if not external_ports_config
else json
.dumps({
96 'external_connections': [
98 'provider:physical_network': 'provider',
99 'encapsulation_type': 'vlan'},
101 dict(zip(('switch', 'port'),
102 _datacenter_to_switch_port(identifier
)))}
105 return {'uuid': uuid('dc%d' % identifier
),
106 'name': 'dc%d' % identifier
,
108 'vim_url': 'localhost',
112 def datacenter_account(datacenter
, tenant
):
113 return {'name': 'dc-account%d%d' % (tenant
, datacenter
),
114 'uuid': uuid('dc-account%d%d' % (tenant
, datacenter
)),
115 'datacenter_id': uuid('dc%d' % datacenter
),
119 def datacenter_tenant_association(datacenter
, tenant
):
120 return {'nfvo_tenant_id': uuid('tenant%d' % tenant
),
121 'datacenter_id': uuid('dc%d' % datacenter
),
122 'datacenter_tenant_id': uuid('dc-account%d%d' % (tenant
, datacenter
))}
125 def datacenter_set(identifier
=0, tenant
=0):
126 """Records necessary to create a datacenter and connect it to a tenant"""
128 {'datacenters': [datacenter(identifier
)]},
129 {'datacenter_tenants': [datacenter_account(identifier
, tenant
)]},
130 {'tenants_datacenters': [
131 datacenter_tenant_association(identifier
, tenant
)
136 def wim_port_mapping(wim
, datacenter
,
137 pop_dpid
='AA:AA:AA:AA:AA:AA:AA:AA', pop_port
=None,
138 wan_dpid
='BB:BB:BB:BB:BB:BB:BB:BB', wan_port
=None):
139 mapping_info
= {'mapping_type': 'dpid-port',
140 'wan_switch_dpid': wan_dpid
,
141 'wan_switch_port': (str(wan_port
) if wan_port
else
142 str(int(datacenter
) + int(wim
) + 1))}
143 id_
= 'dpid-port|' + sha1(json
.dumps(mapping_info
, sort_keys
=True))
145 return {'wim_id': uuid('wim%d' % wim
),
146 'datacenter_id': uuid('dc%d' % datacenter
),
147 'device_id': pop_dpid
,
148 'device_interface_id': (str(pop_port
) if pop_port
else
149 str(int(datacenter
) + int(wim
) + 1)),
150 # ^ Datacenter router have one port managed by each WIM
151 'service_endpoint_id': id_
,
152 # ^ WIM managed router have one port connected to each DC
153 'service_mapping_info': json
.dumps(mapping_info
)}
156 def processed_port_mapping(wim
, datacenter
,
158 pop_dpid
='AA:AA:AA:AA:AA:AA:AA:AA',
159 wan_dpid
='BB:BB:BB:BB:BB:BB:BB:BB'):
160 """Emulate the response of the Persistence class, where the records in the
161 data base are grouped by wim and datacenter
164 'wim_id': uuid('wim%d' % wim
),
165 'datacenter_id': uuid('dc%d' % datacenter
),
166 'pop_wan_mappings': [
167 {'device_id': pop_dpid
,
168 'device_interface_id': wim
+ 1 + i
,
169 'service_endpoint_id':
170 sha1('dpid-port|%s|%d' % (wan_dpid
, datacenter
+ 1 + i
)),
171 'service_mapping_info': {
172 'mapping_type': 'dpid-port',
173 'wan_switch_dpid': wan_dpid
,
174 'wan_switch_port': datacenter
+ 1 + i
}}
175 for i
in range(num_pairs
)
180 def consistent_set(num_wims
=NUM_WIMS
, num_tenants
=NUM_TENANTS
,
181 num_datacenters
=NUM_DATACENTERS
,
182 external_ports_config
=False):
184 {'nfvo_tenants': [tenant(i
) for i
in range(num_tenants
)]},
185 {'wims': [wim(j
) for j
in range(num_wims
)]},
188 for i
in range(num_tenants
)
189 for j
in range(num_wims
)
191 {'wim_nfvo_tenants': [
192 wim_tenant_association(j
, i
)
193 for i
in range(num_tenants
)
194 for j
in range(num_wims
)
197 datacenter(k
, external_ports_config
)
198 for k
in range(num_datacenters
)
200 {'datacenter_tenants': [
201 datacenter_account(k
, i
)
202 for i
in range(num_tenants
)
203 for k
in range(num_datacenters
)
205 {'tenants_datacenters': [
206 datacenter_tenant_association(k
, i
)
207 for i
in range(num_tenants
)
208 for k
in range(num_datacenters
)
210 {'wim_port_mappings': [
211 (wim_port_mapping(j
, k
, *_datacenter_to_switch_port(k
))
212 if external_ports_config
else wim_port_mapping(j
, k
))
213 for j
in range(num_wims
)
214 for k
in range(num_datacenters
)
219 def instance_nets(num_datacenters
=2, num_links
=2, status
='BUILD'):
220 """Example of multi-site deploy with N datacenters and M WAN links between
221 them (e.g M = 2 -> back and forth)
224 {'uuid': uuid('net%d%d' % (k
, li
)),
225 'datacenter_id': uuid('dc%d' % k
),
226 'datacenter_tenant_id': uuid('dc-account0%d' % k
),
227 'instance_scenario_id': uuid('nsr0'),
228 # ^ instance_scenario_id == NS Record id
229 'sce_net_id': uuid('vld%d' % li
),
230 # ^ scenario net id == VLD id
234 for k
in range(num_datacenters
)
235 for li
in range(num_links
)
239 def wim_actions(action
='CREATE', status
='SCHEDULED',
240 action_id
=None, instance
=0,
241 wim
=0, tenant
=0, num_links
=1):
242 """Create a list of actions for the WIM,
245 action: type of action (CREATE) by default
246 wim: WIM fixture index to create actions for
247 tenant: tenant fixture index to create actions for
248 num_links: number of WAN links to be established by each WIM
251 action_id
= action_id
or 'ACTION-{}'.format(time())
256 'wim_internal_id': uuid('-wim-net%d%d%d' % (wim
, instance
, link
)),
257 'wim_account_id': uuid('wim-account%d%d' % (tenant
, wim
)),
258 'instance_action_id': action_id
,
259 'item': 'instance_wim_nets',
260 'item_id': uuid('wim-net%d%d%d' % (wim
, instance
, link
)),
263 'created_at': time(),
264 'modified_at': time(),
267 for link
in range(num_links
)
271 def instance_action(tenant
=0, instance
=0, action_id
=None,
272 num_tasks
=1, num_done
=0, num_failed
=0):
273 action_id
= action_id
or 'ACTION-{}'.format(time())
277 'tenant_id': uuid('tenant%d' % tenant
),
278 'instance_id': uuid('nsr%d' % instance
),
279 'number_tasks': num_tasks
,
280 'number_done': num_done
,
281 'number_failed': num_failed
,
285 def instance_wim_nets(instance
=0, wim
=0, num_links
=1,
286 status
='SCHEDULED_CREATION'):
287 """Example of multi-site deploy with N wims and M WAN links between
288 them (e.g M = 2 -> back and forth)
292 {'uuid': uuid('wim-net%d%d%d' % (wim
, instance
, li
)),
293 'wim_id': uuid('wim%d' % wim
),
294 'wim_account_id': uuid('wim-account%d' % wim
),
295 'wim_internal_id': uuid('-net%d%d' % (wim
, li
)),
296 'instance_scenario_id': uuid('nsr%d' % instance
),
297 # ^ instance_scenario_id == NS Record id
298 'sce_net_id': uuid('vld%d' % li
),
299 # ^ scenario net id == VLD id
302 for li
in range(num_links
)
306 def instance_vm(instance
=0, vim_info
=None):
307 vim_info
= {'OS-EXT-SRV-ATTR:hypervisor_hostname': 'host%d' % instance
}
309 'uuid': uuid('vm%d' % instance
),
310 'instance_vnf_id': uuid('vnf%d' % instance
),
311 'vm_id': uuid('vm%d' % instance
),
312 'vim_vm_id': uuid('vm%d' % instance
),
314 'vim_info': vim_info
,
318 def instance_interface(instance
=0, interface
=0, datacenter
=0, link
=0):
320 'uuid': uuid('interface%d%d' % (instance
, interface
)),
321 'instance_vm_id': uuid('vm%d' % instance
),
322 'instance_net_id': uuid('net%d%d' % (datacenter
, link
)),
323 'interface_id': uuid('iface%d' % interface
),