Fix 1475 - Incorrect description in instantiating error
[osm/RO.git] / RO / 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 time import time
40 from textwrap import wrap
41
42 from ...tests.db_helpers import uuid, sha1
43
44 NUM_WIMS = 3
45 NUM_TENANTS = 2
46 NUM_DATACENTERS = 2
47
48
49 # In the following functions, the identifiers should be simple integers
50
51
52 def wim(identifier=0):
53 return {'name': 'wim%d' % identifier,
54 'uuid': uuid('wim%d' % identifier),
55 'wim_url': 'localhost',
56 'type': 'ietfl2vpn'}
57
58
59 def tenant(identifier=0):
60 return {'name': 'tenant%d' % identifier,
61 'uuid': uuid('tenant%d' % identifier)}
62
63
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),
70 'created': 'true'}
71
72
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))}
77
78
79 def wim_set(identifier=0, tenant=0):
80 """Records necessary to create a WIM and connect it to a tenant"""
81 return [
82 {'wims': [wim(identifier)]},
83 {'wim_accounts': [wim_account(identifier, tenant)]},
84 {'wim_nfvo_tenants': [wim_tenant_association(identifier, tenant)]}
85 ]
86
87
88 def _datacenter_to_switch_port(dc_id, port=None):
89 digits = 16
90 switch = ':'.join(wrap(('%0' + str(digits) + 'x') % int(dc_id), 2))
91 return (switch, str((port or int(dc_id)) + 1))
92
93
94 def datacenter(identifier, external_ports_config=False):
95 config = '' if not external_ports_config else json.dumps({
96 'external_connections': [
97 {'condition': {
98 'provider:physical_network': 'provider',
99 'encapsulation_type': 'vlan'},
100 'vim_external_port':
101 dict(zip(('switch', 'port'),
102 _datacenter_to_switch_port(identifier)))}
103 ]})
104
105 return {'uuid': uuid('dc%d' % identifier),
106 'name': 'dc%d' % identifier,
107 'type': 'openvim',
108 'vim_url': 'localhost',
109 'config': config}
110
111
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),
116 'created': 'true'}
117
118
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))}
123
124
125 def datacenter_set(identifier=0, tenant=0):
126 """Records necessary to create a datacenter and connect it to a tenant"""
127 return [
128 {'datacenters': [datacenter(identifier)]},
129 {'datacenter_tenants': [datacenter_account(identifier, tenant)]},
130 {'tenants_datacenters': [
131 datacenter_tenant_association(identifier, tenant)
132 ]}
133 ]
134
135
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))
144
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)}
154
155
156 def processed_port_mapping(wim, datacenter,
157 num_pairs=1,
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
162 """
163 return {
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)
176 ]
177 }
178
179
180 def consistent_set(num_wims=NUM_WIMS, num_tenants=NUM_TENANTS,
181 num_datacenters=NUM_DATACENTERS,
182 external_ports_config=False):
183 return [
184 {'nfvo_tenants': [tenant(i) for i in range(num_tenants)]},
185 {'wims': [wim(j) for j in range(num_wims)]},
186 {'wim_accounts': [
187 wim_account(j, i)
188 for i in range(num_tenants)
189 for j in range(num_wims)
190 ]},
191 {'wim_nfvo_tenants': [
192 wim_tenant_association(j, i)
193 for i in range(num_tenants)
194 for j in range(num_wims)
195 ]},
196 {'datacenters': [
197 datacenter(k, external_ports_config)
198 for k in range(num_datacenters)
199 ]},
200 {'datacenter_tenants': [
201 datacenter_account(k, i)
202 for i in range(num_tenants)
203 for k in range(num_datacenters)
204 ]},
205 {'tenants_datacenters': [
206 datacenter_tenant_association(k, i)
207 for i in range(num_tenants)
208 for k in range(num_datacenters)
209 ]},
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)
215 ]},
216 ]
217
218
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)
222 """
223 return [
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
231 'status': status,
232 'vim_net_id': None,
233 'created': True}
234 for k in range(num_datacenters)
235 for li in range(num_links)
236 ]
237
238
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,
243
244 Arguments:
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
249 """
250
251 action_id = action_id or 'ACTION-{}'.format(time())
252
253 return [
254 {
255 'action': action,
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)),
261 'status': status,
262 'task_index': link,
263 'created_at': time(),
264 'modified_at': time(),
265 'extra': None
266 }
267 for link in range(num_links)
268 ]
269
270
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())
274
275 return {
276 'uuid': action_id,
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,
282 }
283
284
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)
289 VIM nets
290 """
291 return [
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
300 'status': status,
301 'created': False}
302 for li in range(num_links)
303 ]
304
305
306 def instance_vm(instance=0, vim_info=None):
307 vim_info = {'OS-EXT-SRV-ATTR:hypervisor_hostname': 'host%d' % instance}
308 return {
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),
313 'status': 'ACTIVE',
314 'vim_info': vim_info,
315 }
316
317
318 def instance_interface(instance=0, interface=0, datacenter=0, link=0):
319 return {
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),
324 'type': 'external',
325 'vlan': 3
326 }