Add openflow-port-mapping CLI command
[osm/openvim.git] / onos.py
diff --git a/onos.py b/onos.py
index 53def9d..338412f 100644 (file)
--- a/onos.py
+++ b/onos.py
@@ -36,11 +36,15 @@ import json
 import requests
 import base64
 import logging
+import openflow_conn
 
-class OF_conn():
-    '''ONOS connector. No MAC learning is used'''
+
+class OF_conn(openflow_conn.OpenflowConn):
+    """
+    ONOS connector. No MAC learning is used
+    """
     def __init__(self, params):
-        ''' Constructor. 
+        """ Constructor.
             Params: dictionary with the following keys:
                 of_dpid:     DPID to use for this controller ?? Does a controller have a dpid?
                 of_ip:       controller IP address
@@ -50,16 +54,16 @@ class OF_conn():
                 of_debug:    debug level for logging. Default to ERROR
                 other keys are ignored
             Raise an exception if same parameter is missing or wrong
-        '''
-        #check params
+        """
+
+        openflow_conn.OpenflowConn.__init__(self, params)
 
+        # check params
         if "of_ip" not in params or params["of_ip"]==None or "of_port" not in params or params["of_port"]==None:
             raise ValueError("IP address and port must be provided")
         #internal variables
         self.name = "onos"
-        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
@@ -78,30 +82,30 @@ class OF_conn():
             self.auth = base64.b64encode(str(params["of_user"])+":"+of_password)
             self.headers['authorization'] = 'Basic ' + self.auth
 
-
         self.logger = logging.getLogger('vim.OF.onos')
         self.logger.setLevel( getattr(logging, params.get("of_debug", "ERROR")) )
+        self.ip_address = None
 
     def get_of_switches(self):
-        ''' Obtain a a list of switches or DPID detected by this controller
-            Return
-                >=0, list:      list length, and a list where each element a tuple pair (DPID, IP address)
-                <0, text_error: if fails
-        '''
+        """
+        Obtain a a list of switches or DPID detected by this controller
+        :return: list where each element a tuple pair (DPID, IP address)
+                 Raise a openflowconnUnexpectedResponse expection in case of failure
+        """
         try:
             self.headers['content-type'] = 'text/plain'
             of_response = requests.get(self.url + "devices", headers=self.headers)
             error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
             if of_response.status_code != 200:
                 self.logger.warning("get_of_switches " + error_text)
-                return -1, error_text
+                raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
 
             self.logger.debug("get_of_switches " + error_text)
             info = of_response.json()
 
             if type(info) != dict:
                 self.logger.error("get_of_switches. Unexpected response, not a dict: %s", str(info))
-                return -1, "Unexpected response, not a dict. Wrong version?"
+                raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response, not a dict. Wrong version?")
 
             node_list = info.get('devices')
 
@@ -109,7 +113,8 @@ class OF_conn():
                 self.logger.error(
                     "get_of_switches. Unexpected response, at 'devices', not found or not a list: %s",
                     str(type(node_list)))
-                return -1, "Unexpected response, at 'devices', not found or not a list. Wrong version?"
+                raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response, at 'devices', not found "
+                                                                   "or not a list. Wrong version?")
 
             switch_list = []
             for node in node_list:
@@ -117,43 +122,46 @@ class OF_conn():
                 if node_id is None:
                     self.logger.error("get_of_switches. Unexpected response at 'device':'id', not found: %s",
                                       str(node))
-                    return -1, "Unexpected response at 'device':'id', not found . Wrong version?"
+                    raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response at 'device':'id', "
+                                                                       "not found . Wrong version?")
 
                 node_ip_address = node.get('annotations').get('managementAddress')
                 if node_ip_address is None:
                     self.logger.error(
                         "get_of_switches. Unexpected response at 'device':'managementAddress', not found: %s",
                         str(node))
-                    return -1, "Unexpected response at 'device':'managementAddress', not found. Wrong version?"
+                    raise openflow_conn.OpenflowconnUnexpectedResponse(
+                        "Unexpected response at 'device':'managementAddress', not found. Wrong version?")
 
                 node_id_hex = hex(int(node_id.split(':')[1])).split('x')[1].zfill(16)
 
                 switch_list.append(
                     (':'.join(a + b for a, b in zip(node_id_hex[::2], node_id_hex[1::2])), node_ip_address))
+            raise switch_list
 
-            return len(switch_list), switch_list
-
-        except (requests.exceptions.RequestException, ValueError) as e:
+        except requests.exceptions.RequestException as e:
+            error_text = type(e).__name__ + ": " + str(e)
+            self.logger.error("get_of_switches " + error_text)
+            raise openflow_conn.OpenflowconnConnectionException(error_text)
+        except ValueError as e:
             # ValueError in the case that JSON can not be decoded
             error_text = type(e).__name__ + ": " + str(e)
             self.logger.error("get_of_switches " + error_text)
-            return -1, error_text
-
+            raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
 
-        
     def obtain_port_correspondence(self):
-        '''Obtain the correspondence between physical and openflow port names
-        return:
-             0, dictionary: with physical name as key, openflow name as value
-            -1, error_text: if fails
-        '''
+        """
+        Obtain the correspondence between physical and openflow port names
+        :return: dictionary with physical name as key, openflow name as value
+                 Raise a openflowconnUnexpectedResponse expection in case of failure
+        """
         try:
             self.headers['content-type'] = 'text/plain'
             of_response = requests.get(self.url + "devices/" + self.id + "/ports", headers=self.headers)
             error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
             if of_response.status_code != 200:
                 self.logger.warning("obtain_port_correspondence " + error_text)
-                return -1, error_text
+                raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
 
             self.logger.debug("obtain_port_correspondence " + error_text)
             info = of_response.json()
@@ -163,10 +171,11 @@ class OF_conn():
                 self.logger.error(
                     "obtain_port_correspondence. Unexpected response at 'ports', not found or not a list: %s",
                     str(node_connector_list))
-                return -1, "Unexpected response at 'ports', not found  or not a list. Wrong version?"
+                raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response at 'ports', not found  or not "
+                                                                   "a list. Wrong version?")
 
             for node_connector in node_connector_list:
-                if (node_connector['port'] != "local"):
+                if node_connector['port'] != "local":
                     self.pp2ofi[str(node_connector['annotations']['portName'])] = str(node_connector['port'])
                     self.ofi2pp[str(node_connector['port'])] = str(node_connector['annotations']['portName'])
 
@@ -175,24 +184,27 @@ class OF_conn():
                 self.logger.error(
                     "obtain_port_correspondence. Unexpected response at 'managementAddress', not found: %s",
                     str(self.id))
-                return -1, "Unexpected response at 'managementAddress', not found. Wrong version?"
+                raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response at 'managementAddress', "
+                                                                   "not found. Wrong version?")
             self.ip_address = node_ip_address
 
             # print self.name, ": obtain_port_correspondence ports:", self.pp2ofi
-            return 0, self.pp2ofi
-
-        except (requests.exceptions.RequestException, ValueError) as e:
+            return self.pp2ofi
+        except requests.exceptions.RequestException as e:
+            error_text = type(e).__name__ + ": " + str(e)
+            self.logger.error("obtain_port_correspondence " + error_text)
+            raise openflow_conn.OpenflowconnConnectionException(error_text)
+        except ValueError as e:
             # ValueError in the case that JSON can not be decoded
             error_text = type(e).__name__ + ": " + str(e)
             self.logger.error("obtain_port_correspondence " + error_text)
-            return -1, error_text
-        
+            raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
+
     def get_of_rules(self, translate_of_ports=True):
-        ''' Obtain the rules inserted at openflow controller
-            Params:
-                translate_of_ports: if True it translates ports from openflow index to physical switch name
-            Return:
-                0, dict if ok: with the rule name as key and value is another dictionary with the following content:
+        """
+        Obtain the rules inserted at openflow controller
+        :param translate_of_ports: if True it translates ports from openflow index to physical switch name
+        :return: dict if ok: with the rule name as key and value is another dictionary with the following content:
                     priority: rule priority
                     name:         rule name (present also as the master dict key)
                     ingress_port: match input port of the rule
@@ -200,46 +212,48 @@ class OF_conn():
                     vlan_id:      match vlan tag of the rule, can be missing or None if not apply
                     actions:      list of actions, composed by a pair tuples:
                         (vlan, None/int): for stripping/setting a vlan tag
-                        (out, port):      send to this port 
-                    switch:       DPID, all 
-                -1, text_error if fails
-        '''
-
+                        (out, port):      send to this port
+                    switch:       DPID, all
+                 Raise a openflowconnUnexpectedResponse expection in case of failure
+        """
 
-        if len(self.ofi2pp) == 0:
-            r, c = self.obtain_port_correspondence()
-            if r < 0:
-                return r, c
-        # get rules
         try:
+
+            if len(self.ofi2pp) == 0:
+                self.obtain_port_correspondence()
+
+            # get rules
+            self.headers['content-type'] = 'text/plain'
             of_response = requests.get(self.url + "flows/" + self.id, headers=self.headers)
             error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
 
             # The configured page does not exist if there are no rules installed. In that case we return an empty dict
             if of_response.status_code == 404:
-                return 0, {}
+                return {}
 
             elif of_response.status_code != 200:
                 self.logger.warning("get_of_rules " + error_text)
-                return -1, error_text
+                raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
             self.logger.debug("get_of_rules " + error_text)
 
             info = of_response.json()
 
             if type(info) != dict:
                 self.logger.error("get_of_rules. Unexpected response, not a dict: %s", str(info))
-                return -1, "Unexpected openflow response, not a dict. Wrong version?"
+                raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected openflow response, not a dict. "
+                                                                   "Wrong version?")
 
             flow_list = info.get('flows')
 
             if flow_list is None:
-                return 0, {}
+                return {}
 
             if type(flow_list) is not list:
                 self.logger.error(
                     "get_of_rules. Unexpected response at 'flows', not a list: %s",
                     str(type(flow_list)))
-                return -1, "Unexpected response at 'flows', not a list. Wrong version?"
+                raise openflow_conn.OpenflowconnUnexpectedResponse("Unexpected response at 'flows', not a list. "
+                                                                   "Wrong version?")
 
             rules = dict() # Response dictionary
 
@@ -247,7 +261,8 @@ class OF_conn():
                 if not ('id' in flow and 'selector' in flow and 'treatment' in flow and \
                                     'instructions' in flow['treatment'] and 'criteria' in \
                                     flow['selector']):
-                    return -1, "unexpected openflow response, one or more elements are missing. Wrong version?"
+                    raise openflow_conn.OpenflowconnUnexpectedResponse("unexpected openflow response, one or more "
+                                                                       "elements are missing. Wrong version?")
 
                 rule = dict()
                 rule['switch'] = self.dpid
@@ -259,7 +274,8 @@ class OF_conn():
                         in_port = str(criteria['port'])
                         if in_port != "CONTROLLER":
                             if not in_port in self.ofi2pp:
-                                return -1, "Error: Ingress port " + in_port + " is not in switch port list"
+                                raise openflow_conn.OpenflowconnUnexpectedResponse("Error: Ingress port {} is not "
+                                                                                   "in switch port list".format(in_port))
                             if translate_of_ports:
                                 in_port = self.ofi2pp[in_port]
                         rule['ingress_port'] = in_port
@@ -276,7 +292,8 @@ class OF_conn():
                         out_port = str(instruction['port'])
                         if out_port != "CONTROLLER":
                             if not out_port in self.ofi2pp:
-                                return -1, "Error: Output port " + out_port + " is not in switch port list"
+                                raise openflow_conn.OpenflowconnUnexpectedResponse("Error: Output port {} is not in "
+                                                                                   "switch port list".format(out_port))
 
                             if translate_of_ports:
                                 out_port = self.ofi2pp[out_port]
@@ -290,22 +307,25 @@ class OF_conn():
 
                 rule['actions'] = actions
                 rules[flow['id']] = dict(rule)
+            return rules
 
-            return 0, rules
-
-        except (requests.exceptions.RequestException, ValueError) as e:
+        except requests.exceptions.RequestException as e:
+            # ValueError in the case that JSON can not be decoded
+            error_text = type(e).__name__ + ": " + str(e)
+            self.logger.error("get_of_rules " + error_text)
+            raise openflow_conn.OpenflowconnConnectionException(error_text)
+        except ValueError as e:
             # ValueError in the case that JSON can not be decoded
             error_text = type(e).__name__ + ": " + str(e)
             self.logger.error("get_of_rules " + error_text)
-            return -1, error_text
+            raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
 
     def del_flow(self, flow_name):
-        ''' Delete an existing rule
-            Params: flow_name, this is the rule name
-            Return
-                0, None if ok
-                -1, text_error if fails
-        '''
+        """
+        Delete an existing rule
+        :param flow_name:
+        :return: Raise a openflowconnUnexpectedResponse expection in case of failure
+        """
 
         try:
             self.headers['content-type'] = None
@@ -314,18 +334,20 @@ class OF_conn():
 
             if of_response.status_code != 204:
                 self.logger.warning("del_flow " + error_text)
-                return -1 , error_text
+                raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
+
             self.logger.debug("del_flow OK " + error_text)
-            return 0, None
+            return None
 
         except requests.exceptions.RequestException as e:
             error_text = type(e).__name__ + ": " + str(e)
             self.logger.error("del_flow " + error_text)
-            return -1, error_text
+            raise openflow_conn.OpenflowconnConnectionException(error_text)
 
     def new_flow(self, data):
-        ''' Insert a new static rule
-            Params: data: dictionary with the following content:
+        """
+        Insert a new static rule
+        :param data: dictionary with the following content:
                 priority:     rule priority
                 name:         rule name
                 ingress_port: match input port of the rule
@@ -334,16 +356,13 @@ class OF_conn():
                 actions:      list of actions, composed by a pair tuples with these posibilities:
                     ('vlan', None/int): for stripping/setting a vlan tag
                     ('out', port):      send to this port
-            Return
-                0, None if ok
-                -1, text_error if fails
-        '''
-
-        if len(self.pp2ofi) == 0:
-            r,c = self.obtain_port_correspondence()
-            if r<0:
-                return r,c
+        :return: Raise a openflowconnUnexpectedResponse expection in case of failure
+        """
         try:
+
+            if len(self.pp2ofi) == 0:
+                self.obtain_port_correspondence()
+
             # Build the dictionary with the flow rule information for ONOS
             flow = dict()
             #flow['id'] = data['name']
@@ -359,7 +378,7 @@ class OF_conn():
             if not data['ingress_port'] in self.pp2ofi:
                 error_text = 'Error. Port ' + data['ingress_port'] + ' is not present in the switch'
                 self.logger.warning("new_flow " + error_text)
-                return -1, error_text
+                raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
 
             ingress_port_criteria = dict()
             ingress_port_criteria['type'] = "IN_PORT"
@@ -396,12 +415,12 @@ class OF_conn():
                     new_action['type'] = "OUTPUT"
                     if not action[1] in self.pp2ofi:
                         error_msj = 'Port '+ action[1] + ' is not present in the switch'
-                        return -1, error_msj
+                        raise openflow_conn.OpenflowconnUnexpectedResponse(error_msj)
                     new_action['port'] = self.pp2ofi[action[1]]
                 else:
                     error_msj = "Unknown item '%s' in action list" % action[0]
                     self.logger.error("new_flow " + error_msj)
-                    return -1, error_msj
+                    raise openflow_conn.OpenflowconnUnexpectedResponse(error_msj)
 
                 flow['treatment']['instructions'].append(new_action)
 
@@ -412,41 +431,40 @@ class OF_conn():
             error_text = "Openflow response %d: %s" % (of_response.status_code, of_response.text)
             if of_response.status_code != 201:
                 self.logger.warning("new_flow " + error_text)
-                return -1 , error_text
-
+                raise openflow_conn.OpenflowconnUnexpectedResponse(error_text)
 
             flowId = of_response.headers['location'][path.__len__() + 1:]
 
             data['name'] = flowId
 
             self.logger.debug("new_flow OK " + error_text)
-            return 0, None
+            return None
 
         except requests.exceptions.RequestException as e:
             error_text = type(e).__name__ + ": " + str(e)
             self.logger.error("new_flow " + error_text)
-            return -1, error_text
+            raise openflow_conn.OpenflowconnConnectionException(error_text)
 
     def clear_all_flows(self):
-        ''' Delete all existing rules
-            Return:
-                0, None if ok
-                -1, text_error if fails
-        '''           
+        """
+        Delete all existing rules
+        :return: Raise a openflowconnUnexpectedResponse expection in case of failure
+        """
         try:
-            c, rules = self.get_of_rules(True)
-            if c < 0:
-                return -1, "Error retrieving the flows"
+            rules = self.get_of_rules(True)
 
             for rule in rules:
                 self.del_flow(rule)
 
             self.logger.debug("clear_all_flows OK ")
-            return 0, None
+            return None
 
         except requests.exceptions.RequestException as e:
             error_text = type(e).__name__ + ": " + str(e)
             self.logger.error("clear_all_flows " + error_text)
-            return -1, error_text
+            raise openflow_conn.OpenflowconnConnectionException(error_text)
+
+
+