Merge branch 'WIM' into master
[osm/RO.git] / osm_ro / wim / wan_link_actions.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=E1101,E0203,W0201
35 import json
36 from time import time
37
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
41 from .errors import (
42 InconsistentState,
43 MultipleRecordsFound,
44 NoRecordFound,
45 )
46 from wimconn import WimConnectorError
47
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')
53
54
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.
59
60 Arguments:
61 connector: object with API for accessing the WAN
62 Infrastructure Manager system
63 persistence: abstraction layer for the database
64 """
65 fields = ('wim_status', 'wim_info', 'error_msg')
66 result = dict.fromkeys(fields)
67
68 try:
69 result.update(
70 connector
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))
75
76 result = filter_keys(result, fields)
77
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
85
86 persistence.update_wan_link(self.item_id,
87 remove_none_items(link_changes))
88
89 self.save(persistence, **action_changes)
90
91 return result
92
93
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)
99
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.
104
105 Arguments:
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
111 """
112 wan_link = persistence.get_by_uuid('instance_wim_nets', self.item_id)
113
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'])
117
118 try:
119 dependency_statuses = [n['status'] for n in instance_nets]
120 except KeyError:
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]
126 if errored:
127 return self.fail(
128 persistence,
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))
132
133 pending = [instance_nets[i]
134 for i, status in enumerate(dependency_statuses)
135 if status in INSTANCE_NET_STATUS_PENDING]
136 if pending:
137 return self.defer(
138 persistence,
139 'Still waiting for the local networks to be active:\n\t' +
140 '\n\t'.join('{uuid}: {status}'.format(**n) for n in pending))
141
142 return self.execute(connector, persistence, ovim, instance_nets)
143
144 def _get_connection_point_info(self, persistence, ovim, instance_net):
145 """Retrieve information about the connection PoP <> WAN
146
147 Arguments:
148 persistence: object that encapsulates persistence logic
149 (e.g. db connection)
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.
156
157 Returns:
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**.
164 """
165 wim_account = persistence.get_wim_account_by(uuid=self.wim_account_id)
166
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'])
174 else:
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'])
179
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)
184
185 if len(local_port_mapping) > 1:
186 raise MultipleRecordsFound(criteria, 'ovim.ports')
187 local_port_mapping = local_port_mapping[0]
188
189 criteria = {
190 'wim_id': wim_id,
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']}
194
195 wan_port_mapping = persistence.query_one(
196 FROM='wim_port_mappings',
197 WHERE=criteria)
198
199 if local_port_mapping.get('vlan'):
200 wan_port_mapping['wan_service_mapping_info']['vlan'] = (
201 local_port_mapping['vlan'])
202
203 return wan_port_mapping
204
205 @staticmethod
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']
214 }
215 else:
216 point['service_endpoint_encapsulation_type'] = 'none'
217 return point
218
219 @staticmethod
220 def _derive_service_type(connection_points):
221 # TODO: add multipoint and L3 connectivity.
222 if len(connection_points) == 2:
223 return 'ELINE'
224 else:
225 raise NotImplementedError('Multipoint connectivity is not '
226 'supported yet.')
227
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},
233 'status': 'BUILD'})
234
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
238 """
239 try:
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)
243 for w in wan_info]
244
245 uuid, info = connector.create_connectivity_service(
246 self._derive_service_type(connection_points),
247 connection_points
248 # TODO: other properties, e.g. bandwidth
249 )
250 except (WimConnectorError, InconsistentState) as ex:
251 self.logger.exception(ex)
252 return self.fail(
253 persistence,
254 'Impossible to stablish WAN connectivity.\n\t{}'.format(ex))
255
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)
261 return uuid
262
263
264 class WanLinkDelete(DeleteAction):
265 def succeed(self, persistence):
266 try:
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)
271
272 return super(WanLinkDelete, self).succeed(persistence)
273
274 def get_wan_link(self, persistence):
275 """Retrieve information about the wan_link
276
277 It might be cached, or arrive from the database
278 """
279 if self.extra.get('wan_link'):
280 # First try a cached version of the data
281 return self.extra['wan_link']
282
283 return persistence.get_by_uuid(
284 'instance_wim_nets', self.item_id)
285
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 ''):
290 return self.fail(
291 persistence,
292 'Impossible to delete WAN connectivity, '
293 'it was never successfully established:'
294 '\n\t{}'.format(wan_link['error_msg']))
295
296 internal_id = wan_link.get('wim_internal_id') or self.internal_id
297
298 if not 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)
305
306 try:
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)
315 return self.fail(
316 persistence,
317 'Impossible to delete WAN connectivity.\n\t{}'.format(ex))
318
319 self.logger.debug('WAN connectivity removed %s', result)
320 self.succeed(persistence)
321
322 return result
323
324
325 class WanLinkFind(RefreshMixin, FindAction):
326 pass
327
328
329 ACTIONS = {
330 'CREATE': WanLinkCreate,
331 'DELETE': WanLinkDelete,
332 'FIND': WanLinkFind,
333 }