fix SDN: port mapping, switch_id, sdnconn logging
[osm/RO.git] / RO-SDN-onos_vpls / osm_rosdn_onos_vpls / sdn_assist_onos_vpls.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM RO module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
22 ##
23 import logging
24 import uuid
25 import copy
26
27 import requests
28 from requests.auth import HTTPBasicAuth
29
30 from osm_ro.wim.sdnconn import SdnConnectorBase, SdnConnectorError
31
32
33 class OnosVpls(SdnConnectorBase):
34 """
35 https://wiki.onosproject.org/display/ONOS/VPLS+User+Guide
36 """
37 _WIM_LOGGER = "openmano.sdnconn.onosvpls"
38
39 def __init__(self, wim, wim_account, config=None, logger=None):
40 self.logger = logger or logging.getLogger(self._WIM_LOGGER)
41 super().__init__(wim, wim_account, config, logger)
42 self.user = wim_account.get("user")
43 self.password = wim_account.get("password")
44 url = wim.get("wim_url")
45 if not url:
46 raise SdnConnectorError("'url' must be provided")
47 if not url.startswith("http"):
48 url = "http://" + url
49 if not url.endswith("/"):
50 url = url + "/"
51 self.url = url + "onos/v1/network/configuration"
52 self.logger.info("ONOS VPLS Connector Initialized.")
53
54 def check_credentials(self):
55 status_code = 503
56 onos_config_req = None
57 try:
58 onos_config_req = requests.get(self.url, auth=HTTPBasicAuth(self.user, self.password))
59 onos_config_req.raise_for_status()
60 except Exception as e:
61 if onos_config_req:
62 status_code = onos_config_req.status_code
63 self.logger.exception('Error checking credentials')
64 raise SdnConnectorError('Error checking credentials', http_code=status_code)
65
66 def get_connectivity_service_status(self, service_uuid, conn_info=None):
67 try:
68 onos_config = self._get_onos_netconfig()
69 vpls_config = onos_config.get('apps', {}).get('org.onosproject.vpls')
70 if vpls_config:
71 for vpls in vpls_config.get('vpls', {}).get('vplsList'):
72 if vpls.get('name') == service_uuid:
73 return {'sdn_status': 'ACTIVE', 'sdn_info': vpls}
74
75 return {'sdn_status': 'ERROR', 'sdn_info': 'not found'}
76
77 except Exception as e:
78 self.logger.error('Exception getting connectivity service info: %s', e)
79 return {'sdn_status': 'ERROR', 'error_msg': str(e)}
80
81 def _get_onos_netconfig(self):
82 try:
83 onos_config_req = requests.get(self.url, auth=HTTPBasicAuth(self.user, self.password))
84 status_code = onos_config_req.status_code
85 if status_code == requests.codes.ok:
86 return onos_config_req.json()
87 else:
88 self.logger.info("Error obtaining network config, status code: {}".format(status_code))
89 raise SdnConnectorError("Error obtaining network config status code: {}".format(status_code),
90 http_code=status_code)
91 except requests.exceptions.ConnectionError as e:
92 self.logger.info('Exception connecting to onos: %s', e)
93 raise SdnConnectorError("Error connecting to onos: {}".format(e))
94 except Exception as e:
95 self.logger.error('Exception getting onos network config: %s', e)
96 raise SdnConnectorError("Exception getting onos network config: {}".format(e))
97
98 def _post_onos_netconfig(self, onos_config):
99 try:
100 onos_config_resp = requests.post(self.url, json=onos_config, auth=HTTPBasicAuth(self.user, self.password))
101 status_code = onos_config_resp.status_code
102 if status_code != requests.codes.ok:
103 self.logger.info("Error updating network config, status code: {}".format(status_code))
104 raise SdnConnectorError("Error obtaining network config status code: {}".format(status_code),
105 http_code=status_code)
106 except requests.exceptions.ConnectionError as e:
107 self.logger.info('Exception connecting to onos: %s', e)
108 raise SdnConnectorError("Error connecting to onos: {}".format(e))
109 except Exception as e:
110 self.logger.info('Exception posting onos network config: %s', e)
111 raise SdnConnectorError("Exception posting onos network config: {}".format(e))
112
113 def create_connectivity_service(self, service_type, connection_points, **kwargs):
114 self.logger.debug("create_connectivity_service, service_type: {}, connection_points: {}".
115 format(service_type, connection_points))
116 if service_type.lower() != 'elan':
117 raise SdnConnectorError('Only ELAN network type is supported by ONOS VPLS.')
118
119 # FIXME ¿must check number of connection_points?
120 service_uuid = str(uuid.uuid4())
121
122 # Obtain current configuration
123 onos_config_orig = self._get_onos_netconfig()
124 #self.logger.debug("onos config: %s", onos_config_orig)
125 onos_config = copy.deepcopy(onos_config_orig)
126
127 try:
128 # Create missing interfaces, append to created_items if returned, append_port_to_onos_config
129 # returns null if it was already created
130 created_items = []
131 for port in connection_points:
132 created_ifz = self._append_port_to_onos_config(port, onos_config)
133 if created_ifz:
134 created_items.append(created_ifz[1])
135 self._post_onos_netconfig(onos_config)
136
137 # Add vpls service to config
138 encapsulation = self._get_encapsulation(connection_points)
139 interfaces = [port.get("service_endpoint_id") for port in connection_points]
140 if 'org.onosproject.vpls' in onos_config['apps']:
141 if 'vpls' not in onos_config['apps']['org.onosproject.vpls']:
142 onos_config['apps']['org.onosproject.vpls']['vpls'] = {
143 'vplsList': []
144 }
145 for vpls in onos_config['apps']['org.onosproject.vpls']['vpls']['vplsList']:
146 if vpls['name'] == service_uuid:
147 raise SdnConnectorError('Network {} already exists.'.format(service_uuid))
148 onos_config['apps']['org.onosproject.vpls']['vpls']['vplsList'].append({
149 'name': service_uuid,
150 'interfaces': interfaces,
151 'encapsulation': encapsulation
152 })
153 self._pop_last_update_time(onos_config)
154 else:
155 onos_config['apps'] = {
156 'org.onosproject.vpls': {
157 'vpls': {
158 "vplsList": [
159 {
160 'name': service_uuid,
161 'interfaces': interfaces,
162 'encapsulation': encapsulation
163 }
164 ]
165 }
166 }
167 }
168 #self.logger.debug("original config: %s", onos_config_orig)
169 #self.logger.debug("original config: %s", onos_config)
170 self._post_onos_netconfig(onos_config)
171
172 self.logger.debug("created connectivity_service, service_uuid: {}, created_items: {}".
173 format(service_uuid, created_items))
174 return service_uuid, created_items
175 except Exception as e:
176 self.logger.error('Exception add connection_service: %s', e)
177 # try to rollback push original config
178 try:
179 self._post_onos_netconfig(onos_config_orig)
180 except Exception as e:
181 self.logger.error('Exception rolling back to original config: %s', e)
182 # raise exception
183 if isinstance(e, SdnConnectorError):
184 raise
185 else:
186 raise SdnConnectorError("Exception create_connectivity_service: {}".format(e))
187
188 def _get_encapsulation(self, connection_points):
189 """
190 Obtains encapsulation for the vpls service from the connection_points
191 FIXME: Encapsulation is defined for the connection points but for the VPLS service the encapsulation is
192 defined at the service level so only one can be assigned
193 """
194 # check if encapsulation is vlan, check just one connection point
195 encapsulation = "NONE"
196 for connection_point in connection_points:
197 if connection_point.get("service_endpoint_encapsulation_type") == "dot1q":
198 encapsulation = "VLAN"
199 break
200 return encapsulation
201
202 def edit_connectivity_service(self, service_uuid, conn_info = None, connection_points = None, **kwargs):
203 self.logger.debug("edit connectivity service, service_uuid: {}, conn_info: {}, "
204 "connection points: {} ".format(service_uuid, conn_info, connection_points))
205
206 conn_info = conn_info or []
207 # Obtain current configuration
208 onos_config_orig = self._get_onos_netconfig()
209 onos_config = copy.deepcopy(onos_config_orig)
210
211 # get current service data and check if it does not exists
212 for vpls in onos_config.get('apps', {}).get('org.onosproject.vpls', {}).get('vpls', {}).get('vplsList', {}):
213 if vpls['name'] == service_uuid:
214 self.logger.debug("service exists")
215 curr_interfaces = vpls.get("interfaces", [])
216 curr_encapsulation = vpls.get("encapsulation")
217 break
218 else:
219 raise SdnConnectorError("service uuid: {} does not exist".format(service_uuid))
220
221 self.logger.debug("current interfaces: {}".format(curr_interfaces))
222 self.logger.debug("current encapsulation: {}".format(curr_encapsulation))
223
224 # new interfaces names
225 new_interfaces = [port['service_endpoint_id'] for port in new_connection_points]
226
227 # obtain interfaces to delete, list will contain port
228 ifs_delete = list(set(curr_interfaces) - set(new_interfaces))
229 ifs_add = list(set(new_interfaces) - set(curr_interfaces))
230 self.logger.debug("interfaces to delete: {}".format(ifs_delete))
231 self.logger.debug("interfaces to add: {}".format(ifs_add))
232
233 # check if some data of the interfaces that already existed has changed
234 # in that case delete it and add it again
235 ifs_remain = list(set(new_interfaces) & set(curr_interfaces))
236 for port in connection_points:
237 if port['service_endpoint_id'] in ifs_remain:
238 # check if there are some changes
239 curr_port_name, curr_vlan = self._get_current_port_data(onos_config, port['service_endpoint_id'])
240 new_port_name = 'of:{}/{}'.format(port['service_endpoint_encapsulation_info']['switch_dpid'],
241 port['service_endpoint_encapsulation_info']['switch_port'])
242 new_vlan = port['service_endpoint_encapsulation_info']['vlan']
243 if (curr_port_name != new_port_name or curr_vlan != new_vlan):
244 self.logger.debug("TODO: must update data interface: {}".format(port['service_endpoint_id']))
245 ifs_delete.append(port['service_endpoint_id'])
246 ifs_add.append(port['service_endpoint_id'])
247
248 new_encapsulation = self._get_encapsulation(connection_points)
249
250 try:
251 # Delete interfaces, only will delete interfaces that are in provided conn_info
252 # because these are the ones that have been created for this service
253 if ifs_delete:
254 for port in onos_config['ports'].values():
255 for port_interface in port['interfaces']:
256 interface_name = port_interface['name']
257 self.logger.debug("interface name: {}".format(port_interface['name']))
258 if interface_name in ifs_delete and interface_name in conn_info:
259 self.logger.debug("delete interface name: {}".format(interface_name))
260 port['interfaces'].remove(port_interface)
261 conn_info.remove(interface_name)
262
263 # Add new interfaces
264 for port in connection_points:
265 if port['service_endpoint_id'] in ifs_add:
266 created_ifz = self._append_port_to_onos_config(port, onos_config)
267 if created_ifz:
268 conn_info.append(created_ifz[1])
269 self._pop_last_update_time(onos_config)
270 self._post_onos_netconfig(onos_config)
271
272 self.logger.debug("onos config after updating interfaces: {}".format(onos_config))
273 self.logger.debug("conn_info after updating interfaces: {}".format(conn_info))
274
275 # Update interfaces list in vpls service
276 for vpls in onos_config.get('apps', {}).get('org.onosproject.vpls', {}).get('vpls', {}).get('vplsList', {}):
277 if vpls['name'] == service_uuid:
278 vpls['interfaces'] = new_interfaces
279 vpls['encapsulation'] = new_encapsulation
280
281 self._pop_last_update_time(onos_config)
282 self._post_onos_netconfig(onos_config)
283 return conn_info
284 except Exception as e:
285 self.logger.error('Exception add connection_service: %s', e)
286 # try to rollback push original config
287 try:
288 self._post_onos_netconfig(onos_config_orig)
289 except Exception as e2:
290 self.logger.error('Exception rolling back to original config: %s', e2)
291 # raise exception
292 if isinstance(e, SdnConnectorError):
293 raise
294 else:
295 raise SdnConnectorError("Exception create_connectivity_service: {}".format(e))
296
297 def delete_connectivity_service(self, service_uuid, conn_info=None):
298 self.logger.debug("delete_connectivity_service uuid: {}".format(service_uuid))
299
300 conn_info = conn_info or []
301 # Obtain current config
302 onos_config = self._get_onos_netconfig()
303
304 try:
305 # Removes ports used by network from onos config
306 for vpls in onos_config.get('apps', {}).get('org.onosproject.vpls', {}).get('vpls', {}).get('vplsList', {}):
307 if vpls['name'] == service_uuid:
308 # iterate interfaces to check if must delete them
309 for interface in vpls['interfaces']:
310 for port in onos_config['ports'].values():
311 for port_interface in port['interfaces']:
312 if port_interface['name'] == interface:
313 # Delete only created ifzs
314 if port_interface['name'] in conn_info:
315 self.logger.debug("Delete ifz: {}".format(port_interface['name']))
316 port['interfaces'].remove(port_interface)
317 onos_config['apps']['org.onosproject.vpls']['vpls']['vplsList'].remove(vpls)
318 break
319 else:
320 raise SdnConnectorError("service uuid: {} does not exist".format(service_uuid))
321
322 self._pop_last_update_time(onos_config)
323 self._post_onos_netconfig(onos_config)
324 self.logger.debug("deleted connectivity service uuid: {}".format(service_uuid))
325 except SdnConnectorError:
326 raise
327 except Exception as e:
328 self.logger.error('Exception delete connection_service: %s', e, exc_info=True)
329 raise SdnConnectorError("Exception delete connectivity service: {}".format(str(e)))
330
331 def _pop_last_update_time(self, onos_config):
332 """
333 Needed before post when there are already configured vpls services to apply changes
334 """
335 onos_config['apps']['org.onosproject.vpls']['vpls'].pop('lastUpdateTime', None)
336
337 def _get_current_port_data(self, onos_config, interface_name):
338 for port_name, port in onos_config['ports'].items():
339 for port_interface in port['interfaces']:
340 if port_interface['name'] == interface_name:
341 return port_name, port_interface['vlan']
342
343 def _append_port_to_onos_config(self, port, onos_config):
344 created_item = None
345 port_name = 'of:{}/{}'.format(port['service_endpoint_encapsulation_info']['switch_dpid'],
346 port['service_endpoint_encapsulation_info']['switch_port'])
347 interface_config = {'name': port['service_endpoint_id']}
348 if 'vlan' in port['service_endpoint_encapsulation_info'] \
349 and port['service_endpoint_encapsulation_info']['vlan']:
350 interface_config['vlan'] = port['service_endpoint_encapsulation_info']['vlan']
351 if port_name in onos_config['ports'] and 'interfaces' in onos_config['ports'][port_name]:
352 for interface in onos_config['ports'][port_name]['interfaces']:
353 if interface['name'] == port['service_endpoint_id']:
354 #self.logger.debug("interface with same name and port exits")
355 # interface already exists TODO ¿check vlan? ¿delete and recreate?
356 # by the moment use and do not touch
357 #onos_config['ports'][port_name]['interfaces'].remove(interface)
358 break
359 else:
360 #self.logger.debug("port with same name exits but not interface")
361 onos_config['ports'][port_name]['interfaces'].append(interface_config)
362 created_item = (port_name, port['service_endpoint_id'])
363 else:
364 #self.logger.debug("create port and interface")
365 onos_config['ports'][port_name] = {
366 'interfaces': [interface_config]
367 }
368 created_item = (port_name, port['service_endpoint_id'])
369 return created_item
370
371
372 if __name__ == '__main__':
373 logger = logging.getLogger('openmano.sdn.onos_vpls')
374 logging.basicConfig()
375 logger.setLevel(getattr(logging, "DEBUG"))
376 # wim_url = "http://10.95.172.251:8181"
377 wim_url = "http://192.168.56.106:8181"
378 user = "karaf"
379 password = "karaf"
380 wim = {'wim_url': wim_url}
381 wim_account = {'user': user, 'password': password}
382 onos_vpls = OnosVpls(wim=wim, wim_account=wim_account, logger=logger)
383 #conn_service = onos_vpls.get_connectivity_service_status("4e1f4c8a-a874-425d-a9b5-955cb77178f8")
384 #print(conn_service)
385 service_type = 'ELAN'
386 conn_point_0 = {
387 "service_endpoint_id": "switch1:ifz1",
388 "service_endpoint_encapsulation_type": "dot1q",
389 "service_endpoint_encapsulation_info": {
390 "switch_dpid": "0000000000000011",
391 "switch_port": "1",
392 "vlan": "600"
393 }
394 }
395 conn_point_1 = {
396 "service_endpoint_id": "switch3:ifz1",
397 "service_endpoint_encapsulation_type": "dot1q",
398 "service_endpoint_encapsulation_info": {
399 "switch_dpid": "0000000000000031",
400 "switch_port": "3",
401 "vlan": "600"
402 }
403 }
404 connection_points = [conn_point_0, conn_point_1]
405 #service_uuid, created_items = onos_vpls.create_connectivity_service(service_type, connection_points)
406 #print(service_uuid)
407 #print(created_items)
408
409 conn_info = None
410 conn_info = ['switch1:ifz1', 'switch3_ifz3']
411 onos_vpls.delete_connectivity_service("f7afc4de-556d-4b5a-8a12-12b5ef97d269", conn_info)
412
413 conn_point_0 = {
414 "service_endpoint_id": "switch1:ifz1",
415 "service_endpoint_encapsulation_type": "dot1q",
416 "service_endpoint_encapsulation_info": {
417 "switch_dpid": "0000000000000011",
418 "switch_port": "1",
419 "vlan": "500"
420 }
421 }
422 conn_point_2 = {
423 "service_endpoint_id": "switch1:ifz3",
424 "service_endpoint_encapsulation_type": "dot1q",
425 "service_endpoint_encapsulation_info": {
426 "switch_dpid": "0000000000000011",
427 "switch_port": "3",
428 "vlan": "500"
429 }
430 }
431 conn_point_3 = {
432 "service_endpoint_id": "switch3_ifz3",
433 "service_endpoint_encapsulation_type": "dot1q",
434 "service_endpoint_encapsulation_info": {
435 "switch_dpid": "0000000000000033",
436 "switch_port": "3",
437 "vlan": "500"
438 }
439 }
440 new_connection_points = [conn_point_0, conn_point_3]
441 #conn_info = onos_vpls.edit_connectivity_service("f7afc4de-556d-4b5a-8a12-12b5ef97d269", conn_info, new_connection_points)
442 #print(conn_info)