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$"
41 '''ONOS connector. No MAC learning is used'''
42 def __init__(self
, params
):
44 Params: dictionary with the following keys:
45 of_dpid: DPID to use for this controller ?? Does a controller have a dpid?
46 of_ip: controller IP address
47 of_port: controller TCP port
48 of_user: user credentials, can be missing or None
49 of_password: password credentials
50 of_debug: debug level for logging. Default to ERROR
51 other keys are ignored
52 Raise an exception if same parameter is missing or wrong
56 if "of_ip" not in params
or params
["of_ip"]==None or "of_port" not in params
or params
["of_port"]==None:
57 raise ValueError("IP address and port must be provided")
60 self
.headers
= {'content-type':'application/json',
61 'accept':'application/json',
65 self
.pp2ofi
={} # From Physical Port to OpenFlow Index
66 self
.ofi2pp
={} # From OpenFlow Index to Physical Port
68 self
.dpid
= str(params
["of_dpid"])
69 self
.id = 'of:'+str(self
.dpid
.replace(':', ''))
70 self
.url
= "http://%s:%s/onos/v1/" %( str(params
["of_ip"]), str(params
["of_port"] ) )
72 # TODO This may not be straightforward
73 if "of_user" in params
and params
["of_user"]!=None:
74 if not params
.get("of_password"):
77 of_password
=str(params
["of_password"])
78 self
.auth
= base64
.b64encode(str(params
["of_user"])+":"+of_password
)
79 self
.headers
['authorization'] = 'Basic ' + self
.auth
82 self
.logger
= logging
.getLogger('vim.OF.onos')
83 self
.logger
.setLevel( getattr(logging
, params
.get("of_debug", "ERROR")) )
85 def get_of_switches(self
):
86 ''' Obtain a a list of switches or DPID detected by this controller
88 >=0, list: list length, and a list where each element a tuple pair (DPID, IP address)
89 <0, text_error: if fails
92 self
.headers
['content-type'] = 'text/plain'
93 of_response
= requests
.get(self
.url
+ "devices", headers
=self
.headers
)
94 error_text
= "Openflow response %d: %s" % (of_response
.status_code
, of_response
.text
)
95 if of_response
.status_code
!= 200:
96 self
.logger
.warning("get_of_switches " + error_text
)
99 self
.logger
.debug("get_of_switches " + error_text
)
100 info
= of_response
.json()
102 if type(info
) != dict:
103 self
.logger
.error("get_of_switches. Unexpected response, not a dict: %s", str(info
))
104 return -1, "Unexpected response, not a dict. Wrong version?"
106 node_list
= info
.get('devices')
108 if type(node_list
) is not list:
110 "get_of_switches. Unexpected response, at 'devices', not found or not a list: %s",
111 str(type(node_list
)))
112 return -1, "Unexpected response, at 'devices', not found or not a list. Wrong version?"
115 for node
in node_list
:
116 node_id
= node
.get('id')
118 self
.logger
.error("get_of_switches. Unexpected response at 'device':'id', not found: %s",
120 return -1, "Unexpected response at 'device':'id', not found . Wrong version?"
122 node_ip_address
= node
.get('annotations').get('managementAddress')
123 if node_ip_address
is None:
125 "get_of_switches. Unexpected response at 'device':'managementAddress', not found: %s",
127 return -1, "Unexpected response at 'device':'managementAddress', not found. Wrong version?"
129 node_id_hex
= hex(int(node_id
.split(':')[1])).split('x')[1].zfill(16)
132 (':'.join(a
+ b
for a
, b
in zip(node_id_hex
[::2], node_id_hex
[1::2])), node_ip_address
))
134 return len(switch_list
), switch_list
136 except (requests
.exceptions
.RequestException
, ValueError) as e
:
137 # ValueError in the case that JSON can not be decoded
138 error_text
= type(e
).__name
__ + ": " + str(e
)
139 self
.logger
.error("get_of_switches " + error_text
)
140 return -1, error_text
144 def obtain_port_correspondence(self
):
145 '''Obtain the correspondence between physical and openflow port names
147 0, dictionary: with physical name as key, openflow name as value
148 -1, error_text: if fails
151 self
.headers
['content-type'] = 'text/plain'
152 of_response
= requests
.get(self
.url
+ "devices/" + self
.id + "/ports", headers
=self
.headers
)
153 error_text
= "Openflow response %d: %s" % (of_response
.status_code
, of_response
.text
)
154 if of_response
.status_code
!= 200:
155 self
.logger
.warning("obtain_port_correspondence " + error_text
)
156 return -1, error_text
158 self
.logger
.debug("obtain_port_correspondence " + error_text
)
159 info
= of_response
.json()
161 node_connector_list
= info
.get('ports')
162 if type(node_connector_list
) is not list:
164 "obtain_port_correspondence. Unexpected response at 'ports', not found or not a list: %s",
165 str(node_connector_list
))
166 return -1, "Unexpected response at 'ports', not found or not a list. Wrong version?"
168 for node_connector
in node_connector_list
:
169 if (node_connector
['port'] != "local"):
170 self
.pp2ofi
[str(node_connector
['annotations']['portName'])] = str(node_connector
['port'])
171 self
.ofi2pp
[str(node_connector
['port'])] = str(node_connector
['annotations']['portName'])
173 node_ip_address
= info
['annotations']['managementAddress']
174 if node_ip_address
is None:
176 "obtain_port_correspondence. Unexpected response at 'managementAddress', not found: %s",
178 return -1, "Unexpected response at 'managementAddress', not found. Wrong version?"
179 self
.ip_address
= node_ip_address
181 # print self.name, ": obtain_port_correspondence ports:", self.pp2ofi
182 return 0, self
.pp2ofi
184 except (requests
.exceptions
.RequestException
, ValueError) as e
:
185 # ValueError in the case that JSON can not be decoded
186 error_text
= type(e
).__name
__ + ": " + str(e
)
187 self
.logger
.error("obtain_port_correspondence " + error_text
)
188 return -1, error_text
190 def get_of_rules(self
, translate_of_ports
=True):
191 ''' Obtain the rules inserted at openflow controller
193 translate_of_ports: if True it translates ports from openflow index to physical switch name
195 0, dict if ok: with the rule name as key and value is another dictionary with the following content:
196 priority: rule priority
197 name: rule name (present also as the master dict key)
198 ingress_port: match input port of the rule
199 dst_mac: match destination mac address of the rule, can be missing or None if not apply
200 vlan_id: match vlan tag of the rule, can be missing or None if not apply
201 actions: list of actions, composed by a pair tuples:
202 (vlan, None/int): for stripping/setting a vlan tag
203 (out, port): send to this port
205 -1, text_error if fails
209 if len(self
.ofi2pp
) == 0:
210 r
, c
= self
.obtain_port_correspondence()
215 self
.headers
['content-type'] = 'text/plain'
216 of_response
= requests
.get(self
.url
+ "flows/" + self
.id, headers
=self
.headers
)
217 error_text
= "Openflow response %d: %s" % (of_response
.status_code
, of_response
.text
)
219 # The configured page does not exist if there are no rules installed. In that case we return an empty dict
220 if of_response
.status_code
== 404:
223 elif of_response
.status_code
!= 200:
224 self
.logger
.warning("get_of_rules " + error_text
)
225 return -1, error_text
226 self
.logger
.debug("get_of_rules " + error_text
)
228 info
= of_response
.json()
230 if type(info
) != dict:
231 self
.logger
.error("get_of_rules. Unexpected response, not a dict: %s", str(info
))
232 return -1, "Unexpected openflow response, not a dict. Wrong version?"
234 flow_list
= info
.get('flows')
236 if flow_list
is None:
239 if type(flow_list
) is not list:
241 "get_of_rules. Unexpected response at 'flows', not a list: %s",
242 str(type(flow_list
)))
243 return -1, "Unexpected response at 'flows', not a list. Wrong version?"
245 rules
= dict() # Response dictionary
247 for flow
in flow_list
:
248 if not ('id' in flow
and 'selector' in flow
and 'treatment' in flow
and \
249 'instructions' in flow
['treatment'] and 'criteria' in \
251 return -1, "unexpected openflow response, one or more elements are missing. Wrong version?"
254 rule
['switch'] = self
.dpid
255 rule
['priority'] = flow
.get('priority')
256 rule
['name'] = flow
['id']
258 for criteria
in flow
['selector']['criteria']:
259 if criteria
['type'] == 'IN_PORT':
260 in_port
= str(criteria
['port'])
261 if in_port
!= "CONTROLLER":
262 if not in_port
in self
.ofi2pp
:
263 return -1, "Error: Ingress port " + in_port
+ " is not in switch port list"
264 if translate_of_ports
:
265 in_port
= self
.ofi2pp
[in_port
]
266 rule
['ingress_port'] = in_port
268 elif criteria
['type'] == 'VLAN_VID':
269 rule
['vlan_id'] = criteria
['vlanId']
271 elif criteria
['type'] == 'ETH_DST':
272 rule
['dst_mac'] = str(criteria
['mac']).lower()
275 for instruction
in flow
['treatment']['instructions']:
276 if instruction
['type'] == "OUTPUT":
277 out_port
= str(instruction
['port'])
278 if out_port
!= "CONTROLLER":
279 if not out_port
in self
.ofi2pp
:
280 return -1, "Error: Output port " + out_port
+ " is not in switch port list"
282 if translate_of_ports
:
283 out_port
= self
.ofi2pp
[out_port
]
285 actions
.append( ('out', out_port
) )
287 if instruction
['type'] == "L2MODIFICATION" and instruction
['subtype'] == "VLAN_POP":
288 actions
.append( ('vlan', 'None') )
289 if instruction
['type'] == "L2MODIFICATION" and instruction
['subtype'] == "VLAN_ID":
290 actions
.append( ('vlan', instruction
['vlanId']) )
292 rule
['actions'] = actions
293 rules
[flow
['id']] = dict(rule
)
297 except (requests
.exceptions
.RequestException
, ValueError) as e
:
298 # ValueError in the case that JSON can not be decoded
299 error_text
= type(e
).__name
__ + ": " + str(e
)
300 self
.logger
.error("get_of_rules " + error_text
)
301 return -1, error_text
303 def del_flow(self
, flow_name
):
304 ''' Delete an existing rule
305 Params: flow_name, this is the rule name
308 -1, text_error if fails
312 self
.headers
['content-type'] = None
313 of_response
= requests
.delete(self
.url
+ "flows/" + self
.id + "/" + flow_name
, headers
=self
.headers
)
314 error_text
= "Openflow response %d: %s" % (of_response
.status_code
, of_response
.text
)
316 if of_response
.status_code
!= 204:
317 self
.logger
.warning("del_flow " + error_text
)
318 return -1 , error_text
319 self
.logger
.debug("del_flow OK " + error_text
)
322 except requests
.exceptions
.RequestException
as e
:
323 error_text
= type(e
).__name
__ + ": " + str(e
)
324 self
.logger
.error("del_flow " + error_text
)
325 return -1, error_text
327 def new_flow(self
, data
):
328 ''' Insert a new static rule
329 Params: data: dictionary with the following content:
330 priority: rule priority
332 ingress_port: match input port of the rule
333 dst_mac: match destination mac address of the rule, missing or None if not apply
334 vlan_id: match vlan tag of the rule, missing or None if not apply
335 actions: list of actions, composed by a pair tuples with these posibilities:
336 ('vlan', None/int): for stripping/setting a vlan tag
337 ('out', port): send to this port
340 -1, text_error if fails
343 if len(self
.pp2ofi
) == 0:
344 r
,c
= self
.obtain_port_correspondence()
348 # Build the dictionary with the flow rule information for ONOS
350 #flow['id'] = data['name']
352 flow
['priority'] = data
.get('priority')
354 flow
['isPermanent'] = "true"
355 flow
['appId'] = 10 # FIXME We should create an appId for OSM
356 flow
['selector'] = dict()
357 flow
['selector']['criteria'] = list()
359 # Flow rule matching criteria
360 if not data
['ingress_port'] in self
.pp2ofi
:
361 error_text
= 'Error. Port ' + data
['ingress_port'] + ' is not present in the switch'
362 self
.logger
.warning("new_flow " + error_text
)
363 return -1, error_text
365 ingress_port_criteria
= dict()
366 ingress_port_criteria
['type'] = "IN_PORT"
367 ingress_port_criteria
['port'] = self
.pp2ofi
[data
['ingress_port']]
368 flow
['selector']['criteria'].append(ingress_port_criteria
)
370 if 'dst_mac' in data
:
371 dst_mac_criteria
= dict()
372 dst_mac_criteria
["type"] = "ETH_DST"
373 dst_mac_criteria
["mac"] = data
['dst_mac']
374 flow
['selector']['criteria'].append(dst_mac_criteria
)
376 if data
.get('vlan_id'):
377 vlan_criteria
= dict()
378 vlan_criteria
["type"] = "VLAN_VID"
379 vlan_criteria
["vlanId"] = int(data
['vlan_id'])
380 flow
['selector']['criteria'].append(vlan_criteria
)
382 # Flow rule treatment
383 flow
['treatment'] = dict()
384 flow
['treatment']['instructions'] = list()
385 flow
['treatment']['deferred'] = list()
387 for action
in data
['actions']:
389 if action
[0] == "vlan":
390 new_action
['type'] = "L2MODIFICATION"
391 if action
[1] == None:
392 new_action
['subtype'] = "VLAN_POP"
394 new_action
['subtype'] = "VLAN_ID"
395 new_action
['vlanId'] = int(action
[1])
396 elif action
[0] == 'out':
397 new_action
['type'] = "OUTPUT"
398 if not action
[1] in self
.pp2ofi
:
399 error_msj
= 'Port '+ action
[1] + ' is not present in the switch'
401 new_action
['port'] = self
.pp2ofi
[action
[1]]
403 error_msj
= "Unknown item '%s' in action list" % action
[0]
404 self
.logger
.error("new_flow " + error_msj
)
407 flow
['treatment']['instructions'].append(new_action
)
409 self
.headers
['content-type'] = 'application/json'
410 path
= self
.url
+ "flows/" + self
.id
411 of_response
= requests
.post(path
, headers
=self
.headers
, data
=json
.dumps(flow
) )
413 error_text
= "Openflow response %d: %s" % (of_response
.status_code
, of_response
.text
)
414 if of_response
.status_code
!= 201:
415 self
.logger
.warning("new_flow " + error_text
)
416 return -1 , error_text
419 flowId
= of_response
.headers
['location'][path
.__len
__() + 1:]
421 data
['name'] = flowId
423 self
.logger
.debug("new_flow OK " + error_text
)
426 except requests
.exceptions
.RequestException
as e
:
427 error_text
= type(e
).__name
__ + ": " + str(e
)
428 self
.logger
.error("new_flow " + error_text
)
429 return -1, error_text
431 def clear_all_flows(self
):
432 ''' Delete all existing rules
435 -1, text_error if fails
438 c
, rules
= self
.get_of_rules(True)
440 return -1, "Error retrieving the flows"
445 self
.logger
.debug("clear_all_flows OK ")
448 except requests
.exceptions
.RequestException
as e
:
449 error_text
= type(e
).__name
__ + ": " + str(e
)
450 self
.logger
.error("clear_all_flows " + error_text
)
451 return -1, error_text