2 # -*- coding: utf-8 -*-
5 # Copyright 2016, I2T Research Group (UPV/EHU)
6 # This file is part of openvim
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact with: alaitz.mendiola@ehu.eus or alaitz.mendiola@gmail.com
26 ImplementS the pluging for the Open Network Operating System (ONOS) openflow
27 controller. It creates the class OF_conn to create dataplane connections
28 with static rules based on packet destination MAC address
31 __author__
="Alaitz Mendiola"
32 __date__
="$22-nov-2016$"
42 class OF_conn(openflow_conn
.OpenflowConn
):
44 ONOS connector. No MAC learning is used
46 def __init__(self
, params
):
48 Params: dictionary with the following keys:
49 of_dpid: DPID to use for this controller ?? Does a controller have a dpid?
50 of_ip: controller IP address
51 of_port: controller TCP port
52 of_user: user credentials, can be missing or None
53 of_password: password credentials
54 of_debug: debug level for logging. Default to ERROR
55 other keys are ignored
56 Raise an exception if same parameter is missing or wrong
59 openflow_conn
.OpenflowConn
.__init
__(self
, params
)
62 if "of_ip" not in params
or params
["of_ip"]==None or "of_port" not in params
or params
["of_port"]==None:
63 raise ValueError("IP address and port must be provided")
66 self
.headers
= {'content-type':'application/json','accept':'application/json',}
69 self
.pp2ofi
={} # From Physical Port to OpenFlow Index
70 self
.ofi2pp
={} # From OpenFlow Index to Physical Port
72 self
.dpid
= str(params
["of_dpid"])
73 self
.id = 'of:'+str(self
.dpid
.replace(':', ''))
74 self
.url
= "http://%s:%s/onos/v1/" %( str(params
["of_ip"]), str(params
["of_port"] ) )
76 # TODO This may not be straightforward
77 if "of_user" in params
and params
["of_user"]!=None:
78 if not params
.get("of_password"):
81 of_password
=str(params
["of_password"])
82 self
.auth
= base64
.b64encode(str(params
["of_user"])+":"+of_password
)
83 self
.headers
['authorization'] = 'Basic ' + self
.auth
85 self
.logger
= logging
.getLogger('vim.OF.onos')
86 self
.logger
.setLevel( getattr(logging
, params
.get("of_debug", "ERROR")) )
87 self
.ip_address
= None
89 def get_of_switches(self
):
91 Obtain a a list of switches or DPID detected by this controller
92 :return: list where each element a tuple pair (DPID, IP address)
93 Raise a openflowconnUnexpectedResponse expection in case of failure
96 self
.headers
['content-type'] = 'text/plain'
97 of_response
= requests
.get(self
.url
+ "devices", headers
=self
.headers
)
98 error_text
= "Openflow response %d: %s" % (of_response
.status_code
, of_response
.text
)
99 if of_response
.status_code
!= 200:
100 self
.logger
.warning("get_of_switches " + error_text
)
101 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_text
)
103 self
.logger
.debug("get_of_switches " + error_text
)
104 info
= of_response
.json()
106 if type(info
) != dict:
107 self
.logger
.error("get_of_switches. Unexpected response, not a dict: %s", str(info
))
108 raise openflow_conn
.OpenflowconnUnexpectedResponse("Unexpected response, not a dict. Wrong version?")
110 node_list
= info
.get('devices')
112 if type(node_list
) is not list:
114 "get_of_switches. Unexpected response, at 'devices', not found or not a list: %s",
115 str(type(node_list
)))
116 raise openflow_conn
.OpenflowconnUnexpectedResponse("Unexpected response, at 'devices', not found "
117 "or not a list. Wrong version?")
120 for node
in node_list
:
121 node_id
= node
.get('id')
123 self
.logger
.error("get_of_switches. Unexpected response at 'device':'id', not found: %s",
125 raise openflow_conn
.OpenflowconnUnexpectedResponse("Unexpected response at 'device':'id', "
126 "not found . Wrong version?")
128 node_ip_address
= node
.get('annotations').get('managementAddress')
129 if node_ip_address
is None:
131 "get_of_switches. Unexpected response at 'device':'managementAddress', not found: %s",
133 raise openflow_conn
.OpenflowconnUnexpectedResponse(
134 "Unexpected response at 'device':'managementAddress', not found. Wrong version?")
136 node_id_hex
= hex(int(node_id
.split(':')[1])).split('x')[1].zfill(16)
139 (':'.join(a
+ b
for a
, b
in zip(node_id_hex
[::2], node_id_hex
[1::2])), node_ip_address
))
142 except requests
.exceptions
.RequestException
as e
:
143 error_text
= type(e
).__name
__ + ": " + str(e
)
144 self
.logger
.error("get_of_switches " + error_text
)
145 raise openflow_conn
.OpenflowconnConnectionException(error_text
)
146 except ValueError as e
:
147 # ValueError in the case that JSON can not be decoded
148 error_text
= type(e
).__name
__ + ": " + str(e
)
149 self
.logger
.error("get_of_switches " + error_text
)
150 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_text
)
152 def obtain_port_correspondence(self
):
154 Obtain the correspondence between physical and openflow port names
155 :return: dictionary with physical name as key, openflow name as value
156 Raise a openflowconnUnexpectedResponse expection in case of failure
159 self
.headers
['content-type'] = 'text/plain'
160 of_response
= requests
.get(self
.url
+ "devices/" + self
.id + "/ports", headers
=self
.headers
)
161 error_text
= "Openflow response %d: %s" % (of_response
.status_code
, of_response
.text
)
162 if of_response
.status_code
!= 200:
163 self
.logger
.warning("obtain_port_correspondence " + error_text
)
164 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_text
)
166 self
.logger
.debug("obtain_port_correspondence " + error_text
)
167 info
= of_response
.json()
169 node_connector_list
= info
.get('ports')
170 if type(node_connector_list
) is not list:
172 "obtain_port_correspondence. Unexpected response at 'ports', not found or not a list: %s",
173 str(node_connector_list
))
174 raise openflow_conn
.OpenflowconnUnexpectedResponse("Unexpected response at 'ports', not found or not "
175 "a list. Wrong version?")
177 for node_connector
in node_connector_list
:
178 if node_connector
['port'] != "local":
179 self
.pp2ofi
[str(node_connector
['annotations']['portName'])] = str(node_connector
['port'])
180 self
.ofi2pp
[str(node_connector
['port'])] = str(node_connector
['annotations']['portName'])
182 node_ip_address
= info
['annotations']['managementAddress']
183 if node_ip_address
is None:
185 "obtain_port_correspondence. Unexpected response at 'managementAddress', not found: %s",
187 raise openflow_conn
.OpenflowconnUnexpectedResponse("Unexpected response at 'managementAddress', "
188 "not found. Wrong version?")
189 self
.ip_address
= node_ip_address
191 # print self.name, ": obtain_port_correspondence ports:", self.pp2ofi
193 except requests
.exceptions
.RequestException
as e
:
194 error_text
= type(e
).__name
__ + ": " + str(e
)
195 self
.logger
.error("obtain_port_correspondence " + error_text
)
196 raise openflow_conn
.OpenflowconnConnectionException(error_text
)
197 except ValueError as e
:
198 # ValueError in the case that JSON can not be decoded
199 error_text
= type(e
).__name
__ + ": " + str(e
)
200 self
.logger
.error("obtain_port_correspondence " + error_text
)
201 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_text
)
203 def get_of_rules(self
, translate_of_ports
=True):
205 Obtain the rules inserted at openflow controller
206 :param translate_of_ports: if True it translates ports from openflow index to physical switch name
207 :return: dict if ok: with the rule name as key and value is another dictionary with the following content:
208 priority: rule priority
209 name: rule name (present also as the master dict key)
210 ingress_port: match input port of the rule
211 dst_mac: match destination mac address of the rule, can be missing or None if not apply
212 vlan_id: match vlan tag of the rule, can be missing or None if not apply
213 actions: list of actions, composed by a pair tuples:
214 (vlan, None/int): for stripping/setting a vlan tag
215 (out, port): send to this port
217 Raise a openflowconnUnexpectedResponse expection in case of failure
222 if len(self
.ofi2pp
) == 0:
223 self
.obtain_port_correspondence()
226 self
.headers
['content-type'] = 'text/plain'
227 of_response
= requests
.get(self
.url
+ "flows/" + self
.id, headers
=self
.headers
)
228 error_text
= "Openflow response %d: %s" % (of_response
.status_code
, of_response
.text
)
230 # The configured page does not exist if there are no rules installed. In that case we return an empty dict
231 if of_response
.status_code
== 404:
234 elif of_response
.status_code
!= 200:
235 self
.logger
.warning("get_of_rules " + error_text
)
236 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_text
)
237 self
.logger
.debug("get_of_rules " + error_text
)
239 info
= of_response
.json()
241 if type(info
) != dict:
242 self
.logger
.error("get_of_rules. Unexpected response, not a dict: %s", str(info
))
243 raise openflow_conn
.OpenflowconnUnexpectedResponse("Unexpected openflow response, not a dict. "
246 flow_list
= info
.get('flows')
248 if flow_list
is None:
251 if type(flow_list
) is not list:
253 "get_of_rules. Unexpected response at 'flows', not a list: %s",
254 str(type(flow_list
)))
255 raise openflow_conn
.OpenflowconnUnexpectedResponse("Unexpected response at 'flows', not a list. "
258 rules
= dict() # Response dictionary
260 for flow
in flow_list
:
261 if not ('id' in flow
and 'selector' in flow
and 'treatment' in flow
and \
262 'instructions' in flow
['treatment'] and 'criteria' in \
264 raise openflow_conn
.OpenflowconnUnexpectedResponse("unexpected openflow response, one or more "
265 "elements are missing. Wrong version?")
268 rule
['switch'] = self
.dpid
269 rule
['priority'] = flow
.get('priority')
270 rule
['name'] = flow
['id']
272 for criteria
in flow
['selector']['criteria']:
273 if criteria
['type'] == 'IN_PORT':
274 in_port
= str(criteria
['port'])
275 if in_port
!= "CONTROLLER":
276 if not in_port
in self
.ofi2pp
:
277 raise openflow_conn
.OpenflowconnUnexpectedResponse("Error: Ingress port {} is not "
278 "in switch port list".format(in_port
))
279 if translate_of_ports
:
280 in_port
= self
.ofi2pp
[in_port
]
281 rule
['ingress_port'] = in_port
283 elif criteria
['type'] == 'VLAN_VID':
284 rule
['vlan_id'] = criteria
['vlanId']
286 elif criteria
['type'] == 'ETH_DST':
287 rule
['dst_mac'] = str(criteria
['mac']).lower()
290 for instruction
in flow
['treatment']['instructions']:
291 if instruction
['type'] == "OUTPUT":
292 out_port
= str(instruction
['port'])
293 if out_port
!= "CONTROLLER":
294 if not out_port
in self
.ofi2pp
:
295 raise openflow_conn
.OpenflowconnUnexpectedResponse("Error: Output port {} is not in "
296 "switch port list".format(out_port
))
298 if translate_of_ports
:
299 out_port
= self
.ofi2pp
[out_port
]
301 actions
.append( ('out', out_port
) )
303 if instruction
['type'] == "L2MODIFICATION" and instruction
['subtype'] == "VLAN_POP":
304 actions
.append( ('vlan', 'None') )
305 if instruction
['type'] == "L2MODIFICATION" and instruction
['subtype'] == "VLAN_ID":
306 actions
.append( ('vlan', instruction
['vlanId']) )
308 rule
['actions'] = actions
309 rules
[flow
['id']] = dict(rule
)
312 except requests
.exceptions
.RequestException
as e
:
313 # ValueError in the case that JSON can not be decoded
314 error_text
= type(e
).__name
__ + ": " + str(e
)
315 self
.logger
.error("get_of_rules " + error_text
)
316 raise openflow_conn
.OpenflowconnConnectionException(error_text
)
317 except ValueError as e
:
318 # ValueError in the case that JSON can not be decoded
319 error_text
= type(e
).__name
__ + ": " + str(e
)
320 self
.logger
.error("get_of_rules " + error_text
)
321 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_text
)
323 def del_flow(self
, flow_name
):
325 Delete an existing rule
327 :return: Raise a openflowconnUnexpectedResponse expection in case of failure
331 self
.headers
['content-type'] = None
332 of_response
= requests
.delete(self
.url
+ "flows/" + self
.id + "/" + flow_name
, headers
=self
.headers
)
333 error_text
= "Openflow response %d: %s" % (of_response
.status_code
, of_response
.text
)
335 if of_response
.status_code
!= 204:
336 self
.logger
.warning("del_flow " + error_text
)
337 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_text
)
339 self
.logger
.debug("del_flow OK " + error_text
)
342 except requests
.exceptions
.RequestException
as e
:
343 error_text
= type(e
).__name
__ + ": " + str(e
)
344 self
.logger
.error("del_flow " + error_text
)
345 raise openflow_conn
.OpenflowconnConnectionException(error_text
)
347 def new_flow(self
, data
):
349 Insert a new static rule
350 :param data: dictionary with the following content:
351 priority: rule priority
353 ingress_port: match input port of the rule
354 dst_mac: match destination mac address of the rule, missing or None if not apply
355 vlan_id: match vlan tag of the rule, missing or None if not apply
356 actions: list of actions, composed by a pair tuples with these posibilities:
357 ('vlan', None/int): for stripping/setting a vlan tag
358 ('out', port): send to this port
359 :return: Raise a openflowconnUnexpectedResponse expection in case of failure
363 if len(self
.pp2ofi
) == 0:
364 self
.obtain_port_correspondence()
366 # Build the dictionary with the flow rule information for ONOS
368 #flow['id'] = data['name']
370 flow
['priority'] = data
.get('priority')
372 flow
['isPermanent'] = "true"
373 flow
['appId'] = 10 # FIXME We should create an appId for OSM
374 flow
['selector'] = dict()
375 flow
['selector']['criteria'] = list()
377 # Flow rule matching criteria
378 if not data
['ingress_port'] in self
.pp2ofi
:
379 error_text
= 'Error. Port ' + data
['ingress_port'] + ' is not present in the switch'
380 self
.logger
.warning("new_flow " + error_text
)
381 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_text
)
383 ingress_port_criteria
= dict()
384 ingress_port_criteria
['type'] = "IN_PORT"
385 ingress_port_criteria
['port'] = self
.pp2ofi
[data
['ingress_port']]
386 flow
['selector']['criteria'].append(ingress_port_criteria
)
388 if 'dst_mac' in data
:
389 dst_mac_criteria
= dict()
390 dst_mac_criteria
["type"] = "ETH_DST"
391 dst_mac_criteria
["mac"] = data
['dst_mac']
392 flow
['selector']['criteria'].append(dst_mac_criteria
)
394 if data
.get('vlan_id'):
395 vlan_criteria
= dict()
396 vlan_criteria
["type"] = "VLAN_VID"
397 vlan_criteria
["vlanId"] = int(data
['vlan_id'])
398 flow
['selector']['criteria'].append(vlan_criteria
)
400 # Flow rule treatment
401 flow
['treatment'] = dict()
402 flow
['treatment']['instructions'] = list()
403 flow
['treatment']['deferred'] = list()
405 for action
in data
['actions']:
407 if action
[0] == "vlan":
408 new_action
['type'] = "L2MODIFICATION"
409 if action
[1] == None:
410 new_action
['subtype'] = "VLAN_POP"
412 new_action
['subtype'] = "VLAN_ID"
413 new_action
['vlanId'] = int(action
[1])
414 elif action
[0] == 'out':
415 new_action
['type'] = "OUTPUT"
416 if not action
[1] in self
.pp2ofi
:
417 error_msj
= 'Port '+ action
[1] + ' is not present in the switch'
418 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_msj
)
419 new_action
['port'] = self
.pp2ofi
[action
[1]]
421 error_msj
= "Unknown item '%s' in action list" % action
[0]
422 self
.logger
.error("new_flow " + error_msj
)
423 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_msj
)
425 flow
['treatment']['instructions'].append(new_action
)
427 self
.headers
['content-type'] = 'application/json'
428 path
= self
.url
+ "flows/" + self
.id
429 of_response
= requests
.post(path
, headers
=self
.headers
, data
=json
.dumps(flow
) )
431 error_text
= "Openflow response %d: %s" % (of_response
.status_code
, of_response
.text
)
432 if of_response
.status_code
!= 201:
433 self
.logger
.warning("new_flow " + error_text
)
434 raise openflow_conn
.OpenflowconnUnexpectedResponse(error_text
)
436 flowId
= of_response
.headers
['location'][path
.__len
__() + 1:]
438 data
['name'] = flowId
440 self
.logger
.debug("new_flow OK " + error_text
)
443 except requests
.exceptions
.RequestException
as e
:
444 error_text
= type(e
).__name
__ + ": " + str(e
)
445 self
.logger
.error("new_flow " + error_text
)
446 raise openflow_conn
.OpenflowconnConnectionException(error_text
)
448 def clear_all_flows(self
):
450 Delete all existing rules
451 :return: Raise a openflowconnUnexpectedResponse expection in case of failure
454 rules
= self
.get_of_rules(True)
459 self
.logger
.debug("clear_all_flows OK ")
462 except requests
.exceptions
.RequestException
as e
:
463 error_text
= type(e
).__name
__ + ": " + str(e
)
464 self
.logger
.error("clear_all_flows " + error_text
)
465 raise openflow_conn
.OpenflowconnConnectionException(error_text
)