Implement feature 5949
[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 time import time
40
41 from six.moves import range
42
43 from ...tests.db_helpers import uuid, sha1
44
45 NUM_WIMS = 3
46 NUM_TENANTS = 2
47 NUM_DATACENTERS = 2
48
49
50 # In the following functions, the identifiers should be simple integers
51
52
53 def wim(identifier=0):
54 return {'name': 'wim%d' % identifier,
55 'uuid': uuid('wim%d' % identifier),
56 'wim_url': 'localhost',
57 'type': 'tapi'}
58
59
60 def tenant(identifier=0):
61 return {'name': 'tenant%d' % identifier,
62 'uuid': uuid('tenant%d' % identifier)}
63
64
65 def wim_account(wim, tenant):
66 return {'name': 'wim-account%d%d' % (tenant, wim),
67 'uuid': uuid('wim-account%d%d' % (tenant, wim)),
68 'user': 'user%d%d' % (tenant, wim),
69 'password': 'password%d%d' % (tenant, wim),
70 'wim_id': uuid('wim%d' % wim),
71 'created': 'true'}
72
73
74 def wim_tenant_association(wim, tenant):
75 return {'nfvo_tenant_id': uuid('tenant%d' % tenant),
76 'wim_id': uuid('wim%d' % wim),
77 'wim_account_id': uuid('wim-account%d%d' % (tenant, wim))}
78
79
80 def wim_set(identifier=0, tenant=0):
81 """Records necessary to create a WIM and connect it to a tenant"""
82 return [
83 {'wims': [wim(identifier)]},
84 {'wim_accounts': [wim_account(identifier, tenant)]},
85 {'wim_nfvo_tenants': [wim_tenant_association(identifier, tenant)]}
86 ]
87
88
89 def datacenter(identifier):
90 return {'uuid': uuid('dc%d' % identifier),
91 'name': 'dc%d' % identifier,
92 'type': 'openvim',
93 'vim_url': 'localhost'}
94
95
96 def datacenter_account(datacenter, tenant):
97 return {'name': 'dc-account%d%d' % (tenant, datacenter),
98 'uuid': uuid('dc-account%d%d' % (tenant, datacenter)),
99 'datacenter_id': uuid('dc%d' % datacenter),
100 'created': 'true'}
101
102
103 def datacenter_tenant_association(datacenter, tenant):
104 return {'nfvo_tenant_id': uuid('tenant%d' % tenant),
105 'datacenter_id': uuid('dc%d' % datacenter),
106 'datacenter_tenant_id':
107 uuid('dc-account%d%d' % (tenant, datacenter))}
108
109
110 def datacenter_set(identifier, tenant):
111 """Records necessary to create a datacenter and connect it to a tenant"""
112 return [
113 {'datacenters': [datacenter(identifier)]},
114 {'datacenter_tenants': [datacenter_account(identifier, tenant)]},
115 {'tenants_datacenters': [
116 datacenter_tenant_association(identifier, tenant)
117 ]}
118 ]
119
120
121 def wim_port_mapping(wim, datacenter,
122 pop_dpid='AA:AA:AA:AA:AA:AA:AA:AA', pop_port=0,
123 wan_dpid='BB:BB:BB:BB:BB:BB:BB:BB', wan_port=0):
124 mapping_info = {'mapping_type': 'dpid-port',
125 'wan_switch_dpid': wan_dpid,
126 'wan_switch_port': wan_port + datacenter + 1}
127 id_ = 'dpid-port|' + sha1(json.dumps(mapping_info, sort_keys=True))
128
129 return {'wim_id': uuid('wim%d' % wim),
130 'datacenter_id': uuid('dc%d' % datacenter),
131 'pop_switch_dpid': pop_dpid,
132 'pop_switch_port': pop_port + wim + 1,
133 # ^ Datacenter router have one port managed by each WIM
134 'wan_service_endpoint_id': id_,
135 # ^ WIM managed router have one port connected to each DC
136 'wan_service_mapping_info': json.dumps(mapping_info)}
137
138
139 def processed_port_mapping(wim, datacenter,
140 num_pairs=1,
141 pop_dpid='AA:AA:AA:AA:AA:AA:AA:AA',
142 wan_dpid='BB:BB:BB:BB:BB:BB:BB:BB'):
143 """Emulate the response of the Persistence class, where the records in the
144 data base are grouped by wim and datacenter
145 """
146 return {
147 'wim_id': uuid('wim%d' % wim),
148 'datacenter_id': uuid('dc%d' % datacenter),
149 'wan_pop_port_mappings': [
150 {'pop_switch_dpid': pop_dpid,
151 'pop_switch_port': wim + 1 + i,
152 'wan_service_endpoint_id':
153 sha1('dpid-port|%s|%d' % (wan_dpid, datacenter + 1 + i)),
154 'wan_service_mapping_info': {
155 'mapping_type': 'dpid-port',
156 'wan_switch_dpid': wan_dpid,
157 'wan_switch_port': datacenter + 1 + i}}
158 for i in range(num_pairs)
159 ]
160 }
161
162
163 def consistent_set(num_wims=NUM_WIMS, num_tenants=NUM_TENANTS,
164 num_datacenters=NUM_DATACENTERS):
165 return [
166 {'nfvo_tenants': [tenant(i) for i in range(num_tenants)]},
167 {'wims': [wim(j) for j in range(num_wims)]},
168 {'wim_accounts': [
169 wim_account(j, i)
170 for i in range(num_tenants)
171 for j in range(num_wims)
172 ]},
173 {'wim_nfvo_tenants': [
174 wim_tenant_association(j, i)
175 for i in range(num_tenants)
176 for j in range(num_wims)
177 ]},
178 {'datacenters': [
179 datacenter(k)
180 for k in range(num_datacenters)
181 ]},
182 {'datacenter_tenants': [
183 datacenter_account(k, i)
184 for i in range(num_tenants)
185 for k in range(num_datacenters)
186 ]},
187 {'tenants_datacenters': [
188 datacenter_tenant_association(k, i)
189 for i in range(num_tenants)
190 for k in range(num_datacenters)
191 ]},
192 {'wim_port_mappings': [
193 wim_port_mapping(j, k)
194 for j in range(num_wims)
195 for k in range(num_datacenters)
196 ]},
197 ]
198
199
200 def instance_nets(num_datacenters=2, num_links=2):
201 """Example of multi-site deploy with N datacenters and M WAN links between
202 them (e.g M = 2 -> back and forth)
203 """
204 return [
205 {'uuid': uuid('net%d%d' % (k, l)),
206 'datacenter_id': uuid('dc%d' % k),
207 'datacenter_tenant_id': uuid('dc-account0%d' % k),
208 'instance_scenario_id': uuid('nsr0'),
209 # ^ instance_scenario_id == NS Record id
210 'sce_net_id': uuid('vld%d' % l),
211 # ^ scenario net id == VLD id
212 'status': 'BUILD',
213 'vim_net_id': None,
214 'created': True}
215 for k in range(num_datacenters)
216 for l in range(num_links)
217 ]
218
219
220 def wim_actions(action='CREATE', status='SCHEDULED',
221 action_id=None, instance=0,
222 wim=0, tenant=0, num_links=1):
223 """Create a list of actions for the WIM,
224
225 Arguments:
226 action: type of action (CREATE) by default
227 wim: WIM fixture index to create actions for
228 tenant: tenant fixture index to create actions for
229 num_links: number of WAN links to be established by each WIM
230 """
231
232 action_id = action_id or 'ACTION-{}'.format(time())
233
234 return [
235 {
236 'action': action,
237 'wim_internal_id': uuid('-wim-net%d%d%d' % (wim, instance, link)),
238 'wim_account_id': uuid('wim-account%d%d' % (tenant, wim)),
239 'instance_action_id': action_id,
240 'item': 'instance_wim_nets',
241 'item_id': uuid('wim-net%d%d%d' % (wim, instance, link)),
242 'status': status,
243 'task_index': link,
244 'created_at': time(),
245 'modified_at': time(),
246 'extra': None
247 }
248 for link in range(num_links)
249 ]
250
251
252 def instance_action(tenant=0, instance=0, action_id=None,
253 num_tasks=1, num_done=0, num_failed=0):
254 action_id = action_id or 'ACTION-{}'.format(time())
255
256 return {
257 'uuid': action_id,
258 'tenant_id': uuid('tenant%d' % tenant),
259 'instance_id': uuid('nsr%d' % instance),
260 'number_tasks': num_tasks,
261 'number_done': num_done,
262 'number_failed': num_failed,
263 }
264
265
266 def instance_wim_nets(instance=0, wim=0, num_links=1,
267 status='SCHEDULED_CREATION'):
268 """Example of multi-site deploy with N wims and M WAN links between
269 them (e.g M = 2 -> back and forth)
270 VIM nets
271 """
272 return [
273 {'uuid': uuid('wim-net%d%d%d' % (wim, instance, l)),
274 'wim_id': uuid('wim%d' % wim),
275 'wim_account_id': uuid('wim-account%d' % wim),
276 'wim_internal_id': uuid('-net%d%d' % (wim, l)),
277 'instance_scenario_id': uuid('nsr%d' % instance),
278 # ^ instance_scenario_id == NS Record id
279 'sce_net_id': uuid('vld%d' % l),
280 # ^ scenario net id == VLD id
281 'status': status,
282 'created': False}
283 for l in range(num_links)
284 ]
285
286
287 def instance_vm(instance=0, vim_info=None):
288 vim_info = {'OS-EXT-SRV-ATTR:hypervisor_hostname': 'host%d' % instance}
289 return {
290 'uuid': uuid('vm%d' % instance),
291 'instance_vnf_id': uuid('vnf%d' % instance),
292 'vm_id': uuid('vm%d' % instance),
293 'vim_vm_id': uuid('vm%d' % instance),
294 'status': 'ACTIVE',
295 'vim_info': vim_info,
296 }
297
298
299 def instance_interface(instance=0, interface=0, datacenter=0, link=0):
300 return {
301 'uuid': uuid('interface%d%d' % (instance, interface)),
302 'instance_vm_id': uuid('vm%d' % instance),
303 'instance_net_id': uuid('net%d%d' % (datacenter, link)),
304 'interface_id': uuid('iface%d' % interface),
305 'type': 'external',
306 'vlan': 3
307 }