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=E1101,E0203,W0201
38 from ..utils
import filter_dict_keys
as filter_keys
39 from ..utils
import merge_dicts
, remove_none_items
, safe_get
, truncate
40 from .actions
import CreateAction
, DeleteAction
, FindAction
46 from wimconn
import WimConnectorError
48 INSTANCE_NET_STATUS_ERROR
= ('DOWN', 'ERROR', 'VIM_ERROR',
49 'DELETED', 'SCHEDULED_DELETION')
50 INSTANCE_NET_STATUS_PENDING
= ('BUILD', 'INACTIVE', 'SCHEDULED_CREATION')
51 INSTANCE_VM_STATUS_ERROR
= ('ERROR', 'VIM_ERROR',
52 'DELETED', 'SCHEDULED_DELETION')
55 class RefreshMixin(object):
56 def refresh(self
, connector
, persistence
):
57 """Ask the external WAN Infrastructure Manager system for updates on
58 the status of the task.
61 connector: object with API for accessing the WAN
62 Infrastructure Manager system
63 persistence: abstraction layer for the database
65 fields
= ('wim_status', 'wim_info', 'error_msg')
66 result
= dict.fromkeys(fields
)
71 .get_connectivity_service_status(self
.wim_internal_id
))
72 except WimConnectorError
as ex
:
73 self
.logger
.exception(ex
)
74 result
.update(wim_status
='WIM_ERROR', error_msg
=truncate(ex
))
76 result
= filter_keys(result
, fields
)
78 action_changes
= remove_none_items({
79 'extra': merge_dicts(self
.extra
, result
),
80 'status': 'BUILD' if result
['wim_status'] == 'BUILD' else None,
81 'error_msg': result
['error_msg'],
82 'modified_at': time()})
83 link_changes
= merge_dicts(result
, status
=result
.pop('wim_status'))
84 # ^ Rename field: wim_status => status
86 persistence
.update_wan_link(self
.item_id
,
87 remove_none_items(link_changes
))
89 self
.save(persistence
, **action_changes
)
94 class WanLinkCreate(RefreshMixin
, CreateAction
):
95 def fail(self
, persistence
, reason
, status
='FAILED'):
96 changes
= {'status': 'ERROR', 'error_msg': truncate(reason
)}
97 persistence
.update_wan_link(self
.item_id
, changes
)
98 return super(WanLinkCreate
, self
).fail(persistence
, reason
, status
)
100 def process(self
, connector
, persistence
, ovim
):
101 """Process the current task.
102 First we check if all the dependencies are ready,
103 then we call ``execute`` to actually execute the action.
106 connector: object with API for accessing the WAN
107 Infrastructure Manager system
108 persistence: abstraction layer for the database
109 ovim: instance of openvim, abstraction layer that enable
110 SDN-related operations
112 wan_link
= persistence
.get_by_uuid('instance_wim_nets', self
.item_id
)
114 # First we check if all the dependencies are solved
115 instance_nets
= persistence
.get_instance_nets(
116 wan_link
['instance_scenario_id'], wan_link
['sce_net_id'])
119 dependency_statuses
= [n
['status'] for n
in instance_nets
]
121 self
.logger
.debug('`status` not found in\n\n%s\n\n',
122 json
.dumps(instance_nets
, indent
=4))
123 errored
= [instance_nets
[i
]
124 for i
, status
in enumerate(dependency_statuses
)
125 if status
in INSTANCE_NET_STATUS_ERROR
]
129 'Impossible to stablish WAN connectivity due to an issue '
130 'with the local networks:\n\t' +
131 '\n\t'.join('{uuid}: {status}'.format(**n
) for n
in errored
))
133 pending
= [instance_nets
[i
]
134 for i
, status
in enumerate(dependency_statuses
)
135 if status
in INSTANCE_NET_STATUS_PENDING
]
139 'Still waiting for the local networks to be active:\n\t' +
140 '\n\t'.join('{uuid}: {status}'.format(**n
) for n
in pending
))
142 return self
.execute(connector
, persistence
, ovim
, instance_nets
)
144 def _get_connection_point_info(self
, persistence
, ovim
, instance_net
):
145 """Retrieve information about the connection PoP <> WAN
148 persistence: object that encapsulates persistence logic
150 ovim: object that encapsulates network management logic (openvim)
151 instance_net: record with the information about a local network
152 (inside a VIM). This network will be connected via a WAN link
153 to a different network in a distinct VIM.
154 This method is used to trace what would be the way this network
155 can be accessed from the outside world.
158 dict: Record representing the wan_port_mapping associated to the
159 given instance_net. The expected fields are:
160 **wim_id**, **datacenter_id**, **pop_switch_id** (the local
161 network is expected to be connected at this switch),
162 **pop_switch_port**, **wan_service_endpoint_id**,
163 **wan_service_mapping_info**.
165 wim_account
= persistence
.get_wim_account_by(uuid
=self
.wim_account_id
)
167 # TODO: make more generic to support networks that are not created with
168 # the SDN assist. This method should have a consistent way of getting
169 # the endpoint for all different types of networks used in the VIM
170 # (provider networks, SDN assist, overlay networks, ...)
171 if instance_net
.get('sdn_net_id'):
172 return self
._get
_connection
_point
_info
_sdn
(
173 persistence
, ovim
, instance_net
, wim_account
['wim_id'])
175 raise InconsistentState(
176 'The field `instance_nets.sdn_net_id` was expected to be '
177 'found in the database for the record %s after the network '
178 'become active, but it is still NULL', instance_net
['uuid'])
180 def _get_connection_point_info_sdn(self
, persistence
, ovim
,
181 instance_net
, wim_id
):
182 criteria
= {'net_id': instance_net
['sdn_net_id']}
183 local_port_mapping
= ovim
.get_ports(filter=criteria
)
185 if len(local_port_mapping
) > 1:
186 raise MultipleRecordsFound(criteria
, 'ovim.ports')
187 local_port_mapping
= local_port_mapping
[0]
191 'pop_switch_dpid': local_port_mapping
['switch_dpid'],
192 'pop_switch_port': local_port_mapping
['switch_port'],
193 'datacenter_id': instance_net
['datacenter_id']}
195 wan_port_mapping
= persistence
.query_one(
196 FROM
='wim_port_mappings',
199 if local_port_mapping
.get('vlan'):
200 wan_port_mapping
['wan_service_mapping_info']['vlan'] = (
201 local_port_mapping
['vlan'])
203 return wan_port_mapping
206 def _derive_connection_point(wan_info
):
207 point
= {'service_endpoint_id': wan_info
['wan_service_endpoint_id']}
208 # TODO: Cover other scenarios, e.g. VXLAN.
209 details
= wan_info
.get('wan_service_mapping_info', {})
210 if 'vlan' in details
:
211 point
['service_endpoint_encapsulation_type'] = 'dot1q'
212 point
['service_endpoint_encapsulation_info'] = {
213 'vlan': details
['vlan']
216 point
['service_endpoint_encapsulation_type'] = 'none'
220 def _derive_service_type(connection_points
):
221 # TODO: add multipoint and L3 connectivity.
222 if len(connection_points
) == 2:
225 raise NotImplementedError('Multipoint connectivity is not '
228 def _update_persistent_data(self
, persistence
, service_uuid
, conn_info
):
229 """Store plugin/connector specific information in the database"""
230 persistence
.update_wan_link(self
.item_id
, {
231 'wim_internal_id': service_uuid
,
232 'wim_info': {'conn_info': conn_info
},
235 def execute(self
, connector
, persistence
, ovim
, instance_nets
):
236 """Actually execute the action, since now we are sure all the
237 dependencies are solved
240 wan_info
= (self
._get
_connection
_point
_info
(persistence
, ovim
, net
)
241 for net
in instance_nets
)
242 connection_points
= [self
._derive
_connection
_point
(w
)
245 uuid
, info
= connector
.create_connectivity_service(
246 self
._derive
_service
_type
(connection_points
),
248 # TODO: other properties, e.g. bandwidth
250 except (WimConnectorError
, InconsistentState
) as ex
:
251 self
.logger
.exception(ex
)
254 'Impossible to stablish WAN connectivity.\n\t{}'.format(ex
))
256 self
.logger
.debug('WAN connectivity established %s\n%s\n',
257 uuid
, json
.dumps(info
, indent
=4))
258 self
.wim_internal_id
= uuid
259 self
._update
_persistent
_data
(persistence
, uuid
, info
)
260 self
.succeed(persistence
)
264 class WanLinkDelete(DeleteAction
):
265 def succeed(self
, persistence
):
267 persistence
.update_wan_link(self
.item_id
, {'status': 'DELETED'})
268 except NoRecordFound
:
269 self
.logger
.debug('%s(%s) record already deleted',
270 self
.item
, self
.item_id
)
272 return super(WanLinkDelete
, self
).succeed(persistence
)
274 def get_wan_link(self
, persistence
):
275 """Retrieve information about the wan_link
277 It might be cached, or arrive from the database
279 if self
.extra
.get('wan_link'):
280 # First try a cached version of the data
281 return self
.extra
['wan_link']
283 return persistence
.get_by_uuid(
284 'instance_wim_nets', self
.item_id
)
286 def process(self
, connector
, persistence
, ovim
):
287 """Delete a WAN link previously created"""
288 wan_link
= self
.get_wan_link(persistence
)
289 if 'ERROR' in (wan_link
.get('status') or ''):
292 'Impossible to delete WAN connectivity, '
293 'it was never successfully established:'
294 '\n\t{}'.format(wan_link
['error_msg']))
296 internal_id
= wan_link
.get('wim_internal_id') or self
.internal_id
299 self
.logger
.debug('No wim_internal_id found in\n%s\n%s\n'
300 'Assuming no network was created yet, '
301 'so no network have to be deleted.',
302 json
.dumps(wan_link
, indent
=4),
303 json
.dumps(self
.as_dict(), indent
=4))
304 return self
.succeed(persistence
)
307 id = self
.wim_internal_id
308 conn_info
= safe_get(wan_link
, 'wim_info.conn_info')
309 self
.logger
.debug('Connection Service %s (wan_link: %s):\n%s\n',
310 id, wan_link
['uuid'],
311 json
.dumps(conn_info
, indent
=4))
312 result
= connector
.delete_connectivity_service(id, conn_info
)
313 except (WimConnectorError
, InconsistentState
) as ex
:
314 self
.logger
.exception(ex
)
317 'Impossible to delete WAN connectivity.\n\t{}'.format(ex
))
319 self
.logger
.debug('WAN connectivity removed %s', result
)
320 self
.succeed(persistence
)
325 class WanLinkFind(RefreshMixin
, FindAction
):
330 'CREATE': WanLinkCreate
,
331 'DELETE': WanLinkDelete
,