Adding flake8 tests over some files
[osm/RO.git] / osm_ro / wim / tests / fixtures.py
1 # -*- coding: utf-8 -*-
2 ##
3 # Copyright 2018 University of Bristol - High Performance Networks Research
4 # Group
5 # All Rights Reserved.
6 #
7 # Contributors: Anderson Bravalheri, Dimitrios Gkounis, Abubakar Siddique
8 # Muqaddas, Navdeep Uniyal, Reza Nejabati and Dimitra Simeonidou
9 #
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
13 #
14 # http://www.apache.org/licenses/LICENSE-2.0
15 #
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
20 # under the License.
21 #
22 # For those usages not covered by the Apache License, Version 2.0 please
23 # contact with: <highperformance-networks@bristol.ac.uk>
24 #
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.
28 #
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.
33 ##
34 # pylint: disable=W0621
35
36 from __future__ import unicode_literals
37
38 import json
39 from itertools import izip
40 from time import time
41 from textwrap import wrap
42
43 from six.moves import range
44
45 from ...tests.db_helpers import uuid, sha1
46
47 NUM_WIMS = 3
48 NUM_TENANTS = 2
49 NUM_DATACENTERS = 2
50
51
52 # In the following functions, the identifiers should be simple integers
53
54
55 def wim(identifier=0):
56 return {'name': 'wim%d' % identifier,
57 'uuid': uuid('wim%d' % identifier),
58 'wim_url': 'localhost',
59 'type': 'tapi'}
60
61
62 def tenant(identifier=0):
63 return {'name': 'tenant%d' % identifier,
64 'uuid': uuid('tenant%d' % identifier)}
65
66
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),
73 'created': 'true'}
74
75
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))}
80
81
82 def wim_set(identifier=0, tenant=0):
83 """Records necessary to create a WIM and connect it to a tenant"""
84 return [
85 {'wims': [wim(identifier)]},
86 {'wim_accounts': [wim_account(identifier, tenant)]},
87 {'wim_nfvo_tenants': [wim_tenant_association(identifier, tenant)]}
88 ]
89
90
91 def _datacenter_to_switch_port(dc_id, port=None):
92 digits = 16
93 switch = ':'.join(wrap(('%0' + str(digits) + 'x') % int(dc_id), 2))
94 return (switch, str((port or int(dc_id)) + 1))
95
96
97 def datacenter(identifier, external_ports_config=False):
98 config = '' if not external_ports_config else json.dumps({
99 'external_connections': [
100 {'condition': {
101 'provider:physical_network': 'provider',
102 'encapsulation_type': 'vlan'},
103 'vim_external_port':
104 dict(izip(('switch', 'port'),
105 _datacenter_to_switch_port(identifier)))}
106 ]})
107
108 return {'uuid': uuid('dc%d' % identifier),
109 'name': 'dc%d' % identifier,
110 'type': 'openvim',
111 'vim_url': 'localhost',
112 'config': config}
113
114
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),
119 'created': 'true'}
120
121
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))}
126
127
128 def datacenter_set(identifier=0, tenant=0):
129 """Records necessary to create a datacenter and connect it to a tenant"""
130 return [
131 {'datacenters': [datacenter(identifier)]},
132 {'datacenter_tenants': [datacenter_account(identifier, tenant)]},
133 {'tenants_datacenters': [
134 datacenter_tenant_association(identifier, tenant)
135 ]}
136 ]
137
138
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))
147
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)}
157
158
159 def processed_port_mapping(wim, datacenter,
160 num_pairs=1,
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
165 """
166 return {
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)
179 ]
180 }
181
182
183 def consistent_set(num_wims=NUM_WIMS, num_tenants=NUM_TENANTS,
184 num_datacenters=NUM_DATACENTERS,
185 external_ports_config=False):
186 return [
187 {'nfvo_tenants': [tenant(i) for i in range(num_tenants)]},
188 {'wims': [wim(j) for j in range(num_wims)]},
189 {'wim_accounts': [
190 wim_account(j, i)
191 for i in range(num_tenants)
192 for j in range(num_wims)
193 ]},
194 {'wim_nfvo_tenants': [
195 wim_tenant_association(j, i)
196 for i in range(num_tenants)
197 for j in range(num_wims)
198 ]},
199 {'datacenters': [
200 datacenter(k, external_ports_config)
201 for k in range(num_datacenters)
202 ]},
203 {'datacenter_tenants': [
204 datacenter_account(k, i)
205 for i in range(num_tenants)
206 for k in range(num_datacenters)
207 ]},
208 {'tenants_datacenters': [
209 datacenter_tenant_association(k, i)
210 for i in range(num_tenants)
211 for k in range(num_datacenters)
212 ]},
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)
218 ]},
219 ]
220
221
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)
225 """
226 return [
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
234 'status': status,
235 'vim_net_id': None,
236 'created': True}
237 for k in range(num_datacenters)
238 for l in range(num_links)
239 ]
240
241
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,
246
247 Arguments:
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
252 """
253
254 action_id = action_id or 'ACTION-{}'.format(time())
255
256 return [
257 {
258 'action': action,
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)),
264 'status': status,
265 'task_index': link,
266 'created_at': time(),
267 'modified_at': time(),
268 'extra': None
269 }
270 for link in range(num_links)
271 ]
272
273
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())
277
278 return {
279 'uuid': action_id,
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,
285 }
286
287
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)
292 VIM nets
293 """
294 return [
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
303 'status': status,
304 'created': False}
305 for l in range(num_links)
306 ]
307
308
309 def instance_vm(instance=0, vim_info=None):
310 vim_info = {'OS-EXT-SRV-ATTR:hypervisor_hostname': 'host%d' % instance}
311 return {
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),
316 'status': 'ACTIVE',
317 'vim_info': vim_info,
318 }
319
320
321 def instance_interface(instance=0, interface=0, datacenter=0, link=0):
322 return {
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),
327 'type': 'external',
328 'vlan': 3
329 }