# contact with: alaitz.mendiola@ehu.eus or alaitz.mendiola@gmail.com
##
-'''
+"""
ImplementS the pluging for the Open Network Operating System (ONOS) openflow
controller. It creates the class OF_conn to create dataplane connections
with static rules based on packet destination MAC address
-'''
-
-__author__="Alaitz Mendiola"
-__date__ ="$22-nov-2016$"
-
+"""
import json
import requests
import base64
import logging
-from osm_ro_plugin.openflow_conn import OpenflowConn, OpenflowConnException, OpenflowConnConnectionException, \
- OpenflowConnUnexpectedResponse, OpenflowConnAuthException, OpenflowConnNotFoundException, \
- OpenflowConnConflictException, OpenflowConnNotSupportedException, OpenflowConnNotImplemented
+from osm_ro_plugin.openflow_conn import OpenflowConn, OpenflowConnConnectionException, OpenflowConnUnexpectedResponse
+# OpenflowConnException, OpenflowConnAuthException, OpenflowConnNotFoundException, \
+# OpenflowConnConflictException, OpenflowConnNotSupportedException, OpenflowConnNotImplemented
+
+__author__ = "Alaitz Mendiola"
+__date__ = "$22-nov-2016$"
class OfConnOnos(OpenflowConn):
# internal variables
self.name = "onosof"
- self.headers = {'content-type':'application/json','accept':'application/json',}
+ self.headers = {'content-type': 'application/json', 'accept': 'application/json'}
- self.auth="None"
- self.pp2ofi={} # From Physical Port to OpenFlow Index
- self.ofi2pp={} # From OpenFlow Index to Physical Port
+ self.auth = "None"
+ self.pp2ofi = {} # From Physical Port to OpenFlow Index
+ self.ofi2pp = {} # From OpenFlow Index to Physical Port
self.dpid = str(params["of_dpid"])
self.id = 'of:'+str(self.dpid.replace(':', ''))
# TODO This may not be straightforward
if params.get("of_user"):
- of_password=params.get("of_password", "")
+ of_password = params.get("of_password", "")
self.auth = base64.b64encode(bytes(params["of_user"] + ":" + of_password, "utf-8"))
self.auth = self.auth.decode()
self.headers['authorization'] = 'Basic ' + self.auth
self.logger = logging.getLogger('openmano.sdnconn.onosof')
- #self.logger.setLevel( getattr(logging, params.get("of_debug", "ERROR")) )
+ # self.logger.setLevel( getattr(logging, params.get("of_debug", "ERROR")) )
self.logger.debug("onosof plugin initialized")
self.ip_address = None
"get_of_switches. Unexpected response, at 'devices', not found or not a list: %s",
str(type(node_list)))
raise OpenflowConnUnexpectedResponse("Unexpected response, at 'devices', not found "
- "or not a list. Wrong version?")
+ "or not a list. Wrong version?")
switch_list = []
for node in node_list:
self.logger.error("get_of_switches. Unexpected response at 'device':'id', not found: %s",
str(node))
raise OpenflowConnUnexpectedResponse("Unexpected response at 'device':'id', "
- "not found . Wrong version?")
+ "not found . Wrong version?")
node_ip_address = node.get('annotations').get('managementAddress')
if node_ip_address is None:
"obtain_port_correspondence. Unexpected response at 'ports', not found or not a list: %s",
str(node_connector_list))
raise OpenflowConnUnexpectedResponse("Unexpected response at 'ports', not found or not "
- "a list. Wrong version?")
+ "a list. Wrong version?")
for node_connector in node_connector_list:
if node_connector['port'] != "local":
"obtain_port_correspondence. Unexpected response at 'managementAddress', not found: %s",
str(self.id))
raise OpenflowConnUnexpectedResponse("Unexpected response at 'managementAddress', "
- "not found. Wrong version?")
+ "not found. Wrong version?")
self.ip_address = node_ip_address
# print self.name, ": obtain_port_correspondence ports:", self.pp2ofi
if type(info) != dict:
self.logger.error("get_of_rules. Unexpected response, not a dict: %s", str(info))
raise OpenflowConnUnexpectedResponse("Unexpected openflow response, not a dict. "
- "Wrong version?")
+ "Wrong version?")
flow_list = info.get('flows')
"get_of_rules. Unexpected response at 'flows', not a list: %s",
str(type(flow_list)))
raise OpenflowConnUnexpectedResponse("Unexpected response at 'flows', not a list. "
- "Wrong version?")
+ "Wrong version?")
rules = [] # Response list
for flow in flow_list:
- if not ('id' in flow and 'selector' in flow and 'treatment' in flow and \
- 'instructions' in flow['treatment'] and 'criteria' in \
- flow['selector']):
+ if not ('id' in flow and 'selector' in flow and 'treatment' in flow and
+ 'instructions' in flow['treatment'] and 'criteria' in
+ flow['selector']):
raise OpenflowConnUnexpectedResponse("unexpected openflow response, one or more "
- "elements are missing. Wrong version?")
+ "elements are missing. Wrong version?")
rule = dict()
rule['switch'] = self.dpid
if criteria['type'] == 'IN_PORT':
in_port = str(criteria['port'])
if in_port != "CONTROLLER":
- if not in_port in self.ofi2pp:
+ if in_port not in self.ofi2pp:
raise OpenflowConnUnexpectedResponse("Error: Ingress port {} is not "
- "in switch port list".format(in_port))
+ "in switch port list".format(in_port))
if translate_of_ports:
in_port = self.ofi2pp[in_port]
rule['ingress_port'] = in_port
if instruction['type'] == "OUTPUT":
out_port = str(instruction['port'])
if out_port != "CONTROLLER":
- if not out_port in self.ofi2pp:
+ if out_port not in self.ofi2pp:
raise OpenflowConnUnexpectedResponse("Error: Output port {} is not in "
- "switch port list".format(out_port))
+ "switch port list".format(out_port))
if translate_of_ports:
out_port = self.ofi2pp[out_port]
- actions.append( ('out', out_port) )
+ actions.append(('out', out_port))
if instruction['type'] == "L2MODIFICATION" and instruction['subtype'] == "VLAN_POP":
- actions.append( ('vlan', 'None') )
+ actions.append(('vlan', 'None'))
if instruction['type'] == "L2MODIFICATION" and instruction['subtype'] == "VLAN_ID":
- actions.append( ('vlan', instruction['vlanId']) )
+ actions.append(('vlan', instruction['vlanId']))
rule['actions'] = actions
rules.append(rule)
# Build the dictionary with the flow rule information for ONOS
flow = dict()
- #flow['id'] = data['name']
+ # flow['id'] = data['name']
flow['tableId'] = 0
flow['priority'] = data.get('priority')
flow['timeout'] = 0
flow['isPermanent'] = "true"
- flow['appId'] = 10 # FIXME We should create an appId for OSM
+ flow['appId'] = 10 # FIXME We should create an appId for OSM
flow['selector'] = dict()
flow['selector']['criteria'] = list()
for action in data['actions']:
new_action = dict()
- if action[0] == "vlan":
+ if action[0] == "vlan":
new_action['type'] = "L2MODIFICATION"
- if action[1] == None:
+ if action[1] is None:
new_action['subtype'] = "VLAN_POP"
else:
new_action['subtype'] = "VLAN_ID"
elif action[0] == 'out':
new_action['type'] = "OUTPUT"
if not action[1] in self.pp2ofi:
- error_msj = 'Port '+ action[1] + ' is not present in the switch'
+ error_msj = 'Port ' + action[1] + ' is not present in the switch'
raise OpenflowConnUnexpectedResponse(error_msj)
new_action['port'] = self.pp2ofi[action[1]]
else:
self.headers['content-type'] = 'application/json'
path = self.url + "flows/" + self.id
self.logger.debug("new_flow post: {}".format(flow))
- of_response = requests.post(path, headers=self.headers, data=json.dumps(flow) )
+ of_response = requests.post(path, headers=self.headers, data=json.dumps(flow))
error_text = "Openflow response {}: {}".format(of_response.status_code, of_response.text)
if of_response.status_code != 201: