2 # Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U.
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
19 from http
import HTTPStatus
20 from osm_ro
.wim
.sdnconn
import SdnConnectorBase
, SdnConnectorError
21 from uuid
import uuid4
24 Implement an Abstract class 'OpenflowConn' and an engine 'SdnConnectorOpenFlow' used for base class for SDN plugings
25 that implements a pro-active opeflow rules.
28 __author__
= "Alfonso Tierno"
29 __date__
= "2019-11-11"
32 class OpenflowConnException(Exception):
33 """Common and base class Exception for all vimconnector exceptions"""
34 def __init__(self
, message
, http_code
=HTTPStatus
.BAD_REQUEST
.value
):
35 Exception.__init
__(self
, message
)
36 self
.http_code
= http_code
39 class OpenflowConnConnectionException(OpenflowConnException
):
40 """Connectivity error with the VIM"""
41 def __init__(self
, message
, http_code
=HTTPStatus
.SERVICE_UNAVAILABLE
.value
):
42 OpenflowConnException
.__init
__(self
, message
, http_code
)
45 class OpenflowConnUnexpectedResponse(OpenflowConnException
):
46 """Get an wrong response from VIM"""
47 def __init__(self
, message
, http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
.value
):
48 OpenflowConnException
.__init
__(self
, message
, http_code
)
51 class OpenflowConnAuthException(OpenflowConnException
):
52 """Invalid credentials or authorization to perform this action over the VIM"""
53 def __init__(self
, message
, http_code
=HTTPStatus
.UNAUTHORIZED
.value
):
54 OpenflowConnException
.__init
__(self
, message
, http_code
)
57 class OpenflowConnNotFoundException(OpenflowConnException
):
58 """The item is not found at VIM"""
59 def __init__(self
, message
, http_code
=HTTPStatus
.NOT_FOUND
.value
):
60 OpenflowConnException
.__init
__(self
, message
, http_code
)
63 class OpenflowConnConflictException(OpenflowConnException
):
64 """There is a conflict, e.g. more item found than one"""
65 def __init__(self
, message
, http_code
=HTTPStatus
.CONFLICT
.value
):
66 OpenflowConnException
.__init
__(self
, message
, http_code
)
69 class OpenflowConnNotSupportedException(OpenflowConnException
):
70 """The request is not supported by connector"""
71 def __init__(self
, message
, http_code
=HTTPStatus
.SERVICE_UNAVAILABLE
.value
):
72 OpenflowConnException
.__init
__(self
, message
, http_code
)
75 class OpenflowConnNotImplemented(OpenflowConnException
):
76 """The method is not implemented by the connected"""
77 def __init__(self
, message
, http_code
=HTTPStatus
.NOT_IMPLEMENTED
.value
):
78 OpenflowConnException
.__init
__(self
, message
, http_code
)
83 Openflow controller connector abstract implementeation.
85 def __init__(self
, params
):
86 self
.name
= "openflow_conector"
87 self
.pp2ofi
= {} # From Physical Port to OpenFlow Index
88 self
.ofi2pp
= {} # From OpenFlow Index to Physical Port
89 self
.logger
= logging
.getLogger('openflow_conn')
90 self
.logger
.setLevel(getattr(logging
, params
.get("of_debug", "ERROR")))
92 def get_of_switches(self
):
94 Obtain a a list of switches or DPID detected by this controller
95 :return: list length, and a list where each element a tuple pair (DPID, IP address), text_error: if fails
97 raise OpenflowConnNotImplemented("Should have implemented this")
99 def obtain_port_correspondence(self
):
101 Obtain the correspondence between physical and openflow port names
102 :return: dictionary: with physical name as key, openflow name as value, error_text: if fails
104 raise OpenflowConnNotImplemented("Should have implemented this")
106 def get_of_rules(self
, translate_of_ports
=True):
108 Obtain the rules inserted at openflow controller
109 :param translate_of_ports: if True it translates ports from openflow index to physical switch name
110 :return: list where each item is a dictionary with the following content:
111 priority: rule priority
112 priority: rule priority
113 name: rule name (present also as the master dict key)
114 ingress_port: match input port of the rule
115 dst_mac: match destination mac address of the rule, can be missing or None if not apply
116 vlan_id: match vlan tag of the rule, can be missing or None if not apply
117 actions: list of actions, composed by a pair tuples:
118 (vlan, None/int): for stripping/setting a vlan tag
119 (out, port): send to this port
123 raise OpenflowConnNotImplemented("Should have implemented this")
125 def del_flow(self
, flow_name
):
127 Delete all existing rules
128 :param flow_name: flow_name, this is the rule name
129 :return: None if ok, text_error if fails
131 raise OpenflowConnNotImplemented("Should have implemented this")
133 def new_flow(self
, data
):
135 Insert a new static rule
136 :param data: dictionary with the following content:
137 priority: rule priority
139 ingress_port: match input port of the rule
140 dst_mac: match destination mac address of the rule, missing or None if not apply
141 vlan_id: match vlan tag of the rule, missing or None if not apply
142 actions: list of actions, composed by a pair tuples with these posibilities:
143 ('vlan', None/int): for stripping/setting a vlan tag
144 ('out', port): send to this port
145 :return: None if ok, text_error if fails
147 raise OpenflowConnNotImplemented("Should have implemented this")
149 def clear_all_flows(self
):
151 Delete all existing rules
152 :return: None if ok, text_error if fails
154 raise OpenflowConnNotImplemented("Should have implemented this")
157 class SdnConnectorOpenFlow(SdnConnectorBase
):
159 This class is the base engine of SDN plugins base on openflow rules
161 flow_fields
= ('priority', 'vlan', 'ingress_port', 'actions', 'dst_mac', 'src_mac', 'net_id')
163 def __init__(self
, wim
, wim_account
, config
=None, logger
=None, of_connector
=None):
164 self
.logger
= logger
or logging
.getLogger('openmano.sdnconn.openflow')
165 self
.of_connector
= of_connector
166 self
.of_controller_nets_with_same_vlan
= config
.get("of_controller_nets_with_same_vlan", False)
168 def check_credentials(self
):
170 self
.openflow_conn
.obtain_port_correspondence()
171 except OpenflowConnException
as e
:
172 raise SdnConnectorError(e
, http_code
=e
.http_code
)
174 def get_connectivity_service_status(self
, service_uuid
, conn_info
=None):
175 conn_info
= conn_info
or {}
177 "sdn_status": conn_info
.get("status", "ERROR"),
178 "error_msg": conn_info
.get("error_msg", "Variable conn_info not provided"),
180 # TODO check rules connectirng to of_connector
182 def create_connectivity_service(self
, service_type
, connection_points
, **kwargs
):
183 net_id
= str(uuid4())
185 for cp
in connection_points
:
187 "uuid": cp
["service_endpoint_id"],
188 "vlan": cp
.get("service_endpoint_encapsulation_info", {}).get("vlan"),
189 "mac": cp
.get("service_endpoint_encapsulation_info", {}).get("mac"),
190 "switch_port": cp
.get("service_endpoint_encapsulation_info", {}).get("switch_port"),
194 created_items
= self
._set
_openflow
_rules
(service_type
, net_id
, ports
, created_items
=None)
195 return net_id
, created_items
196 except (SdnConnectorError
, OpenflowConnException
) as e
:
197 raise SdnConnectorError(e
, http_code
=e
.http_code
)
199 def delete_connectivity_service(self
, service_uuid
, conn_info
=None):
201 service_type
= "ELAN"
203 self
._set
_openflow
_rules
(service_type
, service_uuid
, ports
, created_items
=conn_info
)
205 except (SdnConnectorError
, OpenflowConnException
) as e
:
206 raise SdnConnectorError(e
, http_code
=e
.http_code
)
208 def edit_connectivity_service(self
, service_uuid
, conn_info
=None, connection_points
=None, **kwargs
):
210 for cp
in connection_points
:
212 "uuid": cp
["service_endpoint_id"],
213 "vlan": cp
.get("service_endpoint_encapsulation_info", {}).get("vlan"),
214 "mac": cp
.get("service_endpoint_encapsulation_info", {}).get("mac"),
215 "switch_port": cp
.get("service_endpoint_encapsulation_info", {}).get("switch_port"),
218 service_type
= "ELAN" # TODO. Store at conn_info for later use
220 created_items
= self
._set
_openflow
_rules
(service_type
, service_uuid
, ports
, created_items
=conn_info
)
222 except (SdnConnectorError
, OpenflowConnException
) as e
:
223 raise SdnConnectorError(e
, http_code
=e
.http_code
)
225 def clear_all_connectivity_services(self
):
226 """Delete all WAN Links corresponding to a WIM"""
229 def get_all_active_connectivity_services(self
):
230 """Provide information about all active connections provisioned by a
235 def _set_openflow_rules(self
, net_type
, net_id
, ports
, created_items
=None):
236 ifaces_nb
= len(ports
)
237 if not created_items
:
238 created_items
= {"status": None, "error_msg": None, "installed_rules_ids": []}
239 rules_to_delete
= created_items
.get("installed_rules_ids") or []
240 new_installed_rules_ids
= []
244 step
= "Checking ports and network type compatibility"
247 elif net_type
== 'ELINE':
249 raise SdnConnectorError("'ELINE' type network cannot connect {} interfaces, only 2".format(
251 elif net_type
== 'ELAN':
252 if ifaces_nb
> 2 and self
.of_controller_nets_with_same_vlan
:
253 # check all ports are VLAN (tagged) or none
256 if port
["vlan"] not in vlan_tags
:
257 vlan_tags
.append(port
["vlan"])
258 if len(vlan_tags
) > 1:
259 raise SdnConnectorError("This pluging cannot connect ports with diferent VLAN tags when flag "
260 "'of_controller_nets_with_same_vlan' is active")
262 raise SdnConnectorError('Only ELINE or ELAN network types are supported for openflow')
264 # Get the existing flows at openflow controller
265 step
= "Getting installed openflow rules"
266 existing_flows
= self
.of_connector
.get_of_rules()
267 existing_flows_ids
= [flow
["name"] for flow
in existing_flows
]
269 # calculate new flows to be inserted
270 step
= "Compute needed openflow rules"
271 new_flows
= self
._compute
_net
_flows
(net_id
, ports
)
274 for flow
in new_flows
:
275 # 1 check if an equal flow is already present
276 index
= self
._check
_flow
_already
_present
(flow
, existing_flows
)
278 flow_id
= existing_flows
[index
]["name"]
279 self
.logger
.debug("Skipping already present flow %s", str(flow
))
281 # 2 look for a non used name
282 flow_name
= flow
["net_id"] + "." + str(name_index
)
283 while flow_name
in existing_flows_ids
:
285 flow_name
= flow
["net_id"] + "." + str(name_index
)
286 flow
['name'] = flow_name
287 # 3 insert at openflow
289 self
.of_connector
.new_flow(flow
)
290 flow_id
= flow
["name"]
291 existing_flows_ids
.append(flow_id
)
292 except OpenflowConnException
as e
:
294 error_list
.append("Cannot create rule for ingress_port={}, dst_mac={}: {}"
295 .format(flow
["ingress_port"], flow
["dst_mac"], e
))
297 # 4 insert at database
299 new_installed_rules_ids
.append(flow_id
)
300 if flow_id
in rules_to_delete
:
301 rules_to_delete
.remove(flow_id
)
303 # delete not needed old flows from openflow
304 for flow_id
in rules_to_delete
:
307 self
.of_connector
.del_flow(flow_id
)
308 except OpenflowConnNotFoundException
:
310 except OpenflowConnException
as e
:
311 error_text
= "Cannot remove rule '{}': {}".format(flow_id
, e
)
312 error_list
.append(error_text
)
313 self
.logger
.error(error_text
)
314 created_items
["installed_rules_ids"] = new_installed_rules_ids
316 created_items
["error_msg"] = ";".join(error_list
)[:1000]
317 created_items
["error_msg"] = "ERROR"
319 created_items
["error_msg"] = None
320 created_items
["status"] = "ACTIVE"
322 except (SdnConnectorError
, OpenflowConnException
) as e
:
323 raise SdnConnectorError("Error while {}: {}".format(step
, e
)) from e
324 except Exception as e
:
325 error_text
= "Error while {}: {}".format(step
, e
)
326 self
.logger
.critical(error_text
, exc_info
=True)
327 raise SdnConnectorError(error_text
)
329 def _compute_net_flows(self
, net_id
, ports
):
331 new_broadcast_flows
= {}
332 nb_ports
= len(ports
)
334 # Check switch_port information is right
337 if str(port
['switch_port']) not in self
.of_connector
.pp2ofi
:
338 raise SdnConnectorError("switch port name '{}' is not valid for the openflow controller".
339 format(port
['switch_port']))
340 priority
= 1000 # 1100
342 for src_port
in ports
:
343 # if src_port.get("groups")
344 vlan_in
= src_port
['vlan']
347 broadcast_key
= src_port
['uuid'] + "." + str(vlan_in
)
348 if broadcast_key
in new_broadcast_flows
:
349 flow_broadcast
= new_broadcast_flows
[broadcast_key
]
351 flow_broadcast
= {'priority': priority
,
353 'dst_mac': 'ff:ff:ff:ff:ff:ff',
354 "ingress_port": str(src_port
['switch_port']),
358 new_broadcast_flows
[broadcast_key
] = flow_broadcast
359 if vlan_in
is not None:
360 flow_broadcast
['vlan_id'] = str(vlan_in
)
362 for dst_port
in ports
:
363 vlan_out
= dst_port
['vlan']
364 if src_port
['switch_port'] == dst_port
['switch_port'] and vlan_in
== vlan_out
:
367 "priority": priority
,
369 "ingress_port": str(src_port
['switch_port']),
373 # allow that one port have no mac
374 if dst_port
['mac'] is None or nb_ports
== 2: # point to point or nets with 2 elements
375 flow
['priority'] = priority
- 5 # less priority
377 flow
['dst_mac'] = str(dst_port
['mac'])
381 flow
['actions'].append(('vlan', None))
383 flow
['actions'].append(('vlan', vlan_out
))
384 flow
['actions'].append(('out', str(dst_port
['switch_port'])))
386 if self
._check
_flow
_already
_present
(flow
, new_flows
) >= 0:
387 self
.logger
.debug("Skipping repeated flow '%s'", str(flow
))
390 new_flows
.append(flow
)
393 if nb_ports
<= 2: # point to multipoint or nets with more than 2 elements
395 out
= (vlan_out
, str(dst_port
['switch_port']))
396 if out
not in flow_broadcast
['actions']:
397 flow_broadcast
['actions'].append(out
)
400 for flow_broadcast
in new_broadcast_flows
.values():
401 if len(flow_broadcast
['actions']) == 0:
402 continue # nothing to do, skip
403 flow_broadcast
['actions'].sort()
404 if 'vlan_id' in flow_broadcast
:
405 previous_vlan
= 0 # indicates that a packet contains a vlan, and the vlan
410 for action
in flow_broadcast
['actions']:
411 if action
[0] != previous_vlan
:
412 final_actions
.append(('vlan', action
[0]))
413 previous_vlan
= action
[0]
414 if self
.of_controller_nets_with_same_vlan
and action_number
:
415 raise SdnConnectorError("Cannot interconnect different vlan tags in a network when flag "
416 "'of_controller_nets_with_same_vlan' is True.")
418 final_actions
.append(('out', action
[1]))
419 flow_broadcast
['actions'] = final_actions
421 if self
._check
_flow
_already
_present
(flow_broadcast
, new_flows
) >= 0:
422 self
.logger
.debug("Skipping repeated flow '%s'", str(flow_broadcast
))
425 new_flows
.append(flow_broadcast
)
427 # UNIFY openflow rules with the same input port and vlan and the same output actions
428 # These flows differ at the dst_mac; and they are unified by not filtering by dst_mac
429 # this can happen if there is only two ports. It is converted to a point to point connection
430 flow_dict
= {} # use as key vlan_id+ingress_port and as value the list of flows matching these values
431 for flow
in new_flows
:
432 key
= str(flow
.get("vlan_id")) + ":" + flow
["ingress_port"]
434 flow_dict
[key
].append(flow
)
436 flow_dict
[key
] = [flow
]
438 for flow_list
in flow_dict
.values():
440 if len(flow_list
) >= 2:
443 if f
['actions'] != flow_list
[0]['actions']:
446 if convert2ptp
: # add only one unified rule without dst_mac
447 self
.logger
.debug("Convert flow rules to NON mac dst_address " + str(flow_list
))
448 flow_list
[0].pop('dst_mac')
449 flow_list
[0]["priority"] -= 5
450 new_flows2
.append(flow_list
[0])
451 else: # add all the rules
452 new_flows2
+= flow_list
455 def _check_flow_already_present(self
, new_flow
, flow_list
):
456 '''check if the same flow is already present in the flow list
457 The flow is repeated if all the fields, apart from name, are equal
458 Return the index of matching flow, -1 if not match'''
459 for index
, flow
in enumerate(flow_list
):
460 for f
in self
.flow_fields
:
461 if flow
.get(f
) != new_flow
.get(f
):