Modify http get/clear openflows to handle n ofc_id
[osm/openvim.git] / ovim.py
diff --git a/ovim.py b/ovim.py
index 14bc50d..5b36b73 100644 (file)
--- a/ovim.py
+++ b/ovim.py
@@ -180,7 +180,7 @@ class ovim():
         # check if this bridge is already used (present at database) for a network)
         used_bridge_nets = []
         for brnet in self.config['bridge_nets']:
-            r, nets = self.db_of.get_table(SELECT=('uuid',), FROM='nets', WHERE={'provider': "bridge:" + brnet[0]})
+            r, nets = self.db.get_table(SELECT=('uuid',), FROM='nets', WHERE={'provider': "bridge:" + brnet[0]})
             if r > 0:
                 brnet[3] = nets[0]['uuid']
                 used_bridge_nets.append(brnet[0])
@@ -192,7 +192,7 @@ class ovim():
         # get nets used by dhcp
         if self.config.get("dhcp_server"):
             for net in self.config["dhcp_server"].get("nets", ()):
-                r, nets = self.db_of.get_table(SELECT=('uuid',), FROM='nets', WHERE={'name': net})
+                r, nets = self.db.get_table(SELECT=('uuid',), FROM='nets', WHERE={'name': net})
                 if r > 0:
                     self.config['dhcp_nets'].append(nets[0]['uuid'])
 
@@ -217,7 +217,7 @@ class ovim():
         host_develop_bridge_iface = self.config.get('development_bridge', None)
 
         # get host list from data base before starting threads
-        r, hosts = self.db_of.get_table(SELECT=('name', 'ip_name', 'user', 'uuid'), FROM='hosts', WHERE={'status': 'ok'})
+        r, hosts = self.db.get_table(SELECT=('name', 'ip_name', 'user', 'uuid'), FROM='hosts', WHERE={'status': 'ok'})
         if r < 0:
             raise ovimException("Cannot get hosts from database {}".format(hosts))
 
@@ -316,7 +316,7 @@ class ovim():
     def _load_of_module(self, db_config):
         """
         import python module for each SDN controller supported
-        :param default: SDN dn information
+        :param db_config: SDN dn information
         :return: Module
         """
         if not db_config:
@@ -326,8 +326,8 @@ class ovim():
 
         try:
             if self.of_test_mode:
-                return  oft.of_test_connector({"name": db_config['type'], "dpid": db_config['dpid'],
-                                               "of_debug": self.config['log_level_of']})
+                return oft.of_test_connector({"name": db_config['type'], "dpid": db_config['dpid'],
+                                              "of_debug": self.config['log_level_of']})
             temp_dict = {}
 
             if db_config:
@@ -661,17 +661,24 @@ class ovim():
             # if result > 0 and nbports>0 and 'admin_state_up' in network
             #     and network['admin_state_up'] != network_old[0]['admin_state_up']:
             if result > 0:
-                r, c = self.config['of_thread'].insert_task("update-net", network_id)
-                if r < 0:
-                    raise ovimException("Error while launching openflow rules %s" % c, HTTP_Internal_Server_Error)
+
+                try:
+                    self.net_update_ofc_thread(network_id)
+                except ovimException as e:
+                    raise ovimException("Error while launching openflow rules in network '{}' {}"
+                                        .format(network_id, str(e)), HTTP_Internal_Server_Error)
+                except Exception as e:
+                    raise ovimException("Error while launching openflow rules in network '{}' {}"
+                                        .format(network_id, str(e)), HTTP_Internal_Server_Error)
+
                 if self.config.get("dhcp_server"):
                     if network_id in self.config["dhcp_nets"]:
                         self.config["dhcp_nets"].remove(network_id)
-                    if network.get("name", network_old["name"]) in self.config["dhcp_server"].get("nets", ()):
+                    if network.get("name", network_old[0]["name"]) in self.config["dhcp_server"].get("nets", ()):
                         self.config["dhcp_nets"].append(network_id)
                     else:
-                        net_bind = network.get("bind", network_old["bind"])
-                        if net_bind and net_bind[:7] == "bridge:" and net_bind[7:] in self.config["dhcp_server"].get(
+                        net_bind = network.get("bind_type", network_old[0]["bind_type"])
+                        if net_bind and net_bind and net_bind[:7] == "bridge:" and net_bind[7:] in self.config["dhcp_server"].get(
                                 "bridge_ifaces", ()):
                             self.config["dhcp_nets"].append(network_id)
             return network_id
@@ -712,9 +719,8 @@ class ovim():
             where_ = {}
         else:
             where_ = {"net_id": network_id}
-
         result, content = self.db.get_table(
-            SELECT=("name", "net_id", "priority", "vlan_id", "ingress_port", "src_mac", "dst_mac", "actions"),
+            SELECT=("name", "net_id", "ofc_id", "priority", "vlan_id", "ingress_port", "src_mac", "dst_mac", "actions"),
             WHERE=where_, FROM='of_flows')
 
         if result < 0:
@@ -744,29 +750,56 @@ class ovim():
             if net["type"] != "ptp" and net["type"] != "data":
                 result -= 1
                 continue
-            r, c = self.config['of_thread'].insert_task("update-net", net['uuid'])
-            if r < 0:
-                raise ovimException(str(c), -r)
+
+            try:
+                self.net_update_ofc_thread(net['uuid'])
+            except ovimException as e:
+                raise ovimException("Error updating network'{}' {}".format(net['uuid'], str(e)),
+                                    HTTP_Internal_Server_Error)
+            except Exception as e:
+                raise ovimException("Error updating network '{}' {}".format(net['uuid'], str(e)),
+                                    HTTP_Internal_Server_Error)
+
         return result
 
-    def delete_openflow_rules(self):
+    def delete_openflow_rules(self, ofc_id=None):
         """
         To make actions over the net. The action is to delete ALL openflow rules
         :return: return operation result
         """
-        # ignore input data
-        r, c = self.config['of_thread'].insert_task("clear-all")
-        if r < 0:
-            raise ovimException(str(c), -r)
+
+        if not ofc_id:
+            if 'Default' in self.config['ofcs_thread']:
+                r, c = self.config['ofcs_thread']['Default'].insert_task("clear-all")
+            else:
+                raise ovimException("Default Openflow controller not not running", HTTP_Not_Found)
+
+        elif ofc_id in self.config['ofcs_thread']:
+            r, c = self.config['ofcs_thread'][ofc_id].insert_task("clear-all")
+
+            # ignore input data
+            if r < 0:
+                raise ovimException(str(c), -r)
+        else:
+            raise ovimException("Openflow controller not found with ofc_id={}".format(ofc_id), HTTP_Not_Found)
         return r
 
-    def get_openflow_ports(self):
+    def get_openflow_ports(self, ofc_id=None):
         """
         Obtain switch ports names of openflow controller
         :return: Return flow ports in DB
         """
-        data = {'ports': self.config['of_thread'].OF_connector.pp2ofi}
-        return data
+        if not ofc_id:
+            if 'Default' in self.config['ofcs_thread']:
+                conn = self.config['ofcs_thread']['Default'].OF_connector
+            else:
+                raise ovimException("Default Openflow controller not not running", HTTP_Not_Found)
+
+        if ofc_id in self.config['ofcs_thread']:
+            conn = self.config['ofcs_thread'][ofc_id].OF_connector
+        else:
+            raise ovimException("Openflow controller not found with ofc_id={}".format(ofc_id), HTTP_Not_Found)
+        return conn.pp2ofi
 
     def get_ports(self, columns=None, filter={}, limit=None):
         # result, content = my.db.get_ports(where_)
@@ -789,10 +822,15 @@ class ovim():
         result, uuid = self.db.new_row('ports', port_data, True, True)
         if result > 0:
             if 'net_id' in port_data:
-                r, c = self.config['of_thread'].insert_task("update-net", port_data['net_id'])
-                if r < 0:
-                    self.logger.error("Cannot insert a task for updating network '$s' %s", port_data['net_id'], c)
-                    #TODO put network in error status
+                try:
+                    self.net_update_ofc_thread(port_data['net_id'])
+                except ovimException as e:
+                    raise ovimException("Cannot insert a task for updating network '{}' {}"
+                                        .format(port_data['net_id'], str(e)), HTTP_Internal_Server_Error)
+                except Exception as e:
+                    raise ovimException("Cannot insert a task for updating network '{}' {}"
+                                        .format(port_data['net_id'], str(e)), HTTP_Internal_Server_Error)
+
             return uuid
         else:
             raise ovimException(str(uuid), -result)
@@ -853,16 +891,59 @@ class ovim():
 
         result, uuid = self.db.new_row('ports', port_data, True, True)
         if result > 0:
-            if 'net_id' in port_data and port_data['ofc_id'] in self.config['ofcs_thread']:
-                r, c = self.config['ofcs_thread'][port_data['ofc_id']].insert_task("update-net", port_data['net_id'])
-                if r < 0:
-                    message = "Cannot insert a task for updating network '$s' %s", port_data['net_id'], c
-                    self.logger.error(message)
-                    raise ovimException(message, HTTP_Internal_Server_Error)
+            try:
+                self.net_update_ofc_thread(port_data['net_id'], port_data['ofc_id'])
+            except ovimException as e:
+                raise ovimException("Cannot insert a task for updating network '{}' {}".
+                                    format(port_data['net_id'], str(e)), HTTP_Internal_Server_Error)
+            except Exception as e:
+                raise ovimException("Cannot insert a task for updating network '{}' {}"
+                                    .format(port_data['net_id'], e), HTTP_Internal_Server_Error)
             return uuid
         else:
             raise ovimException(str(uuid), -result)
 
+    def net_update_ofc_thread(self, net_id, ofc_id=None):
+        """
+        Insert a update net task by net id or ofc_id for each ofc thread
+        :param net_id: network id
+        :param ofc_id: openflow controller id
+        :return:
+        """
+        if not net_id:
+            raise ovimException("No net_id received", HTTP_Internal_Server_Error)
+
+        switch_dpid = None
+        r = -1
+        c = 'No valid ofc_id or switch_dpid received'
+
+        if not ofc_id:
+            ports = self.get_ports(filter={"net_id": net_id})
+            for port in ports:
+                port_ofc_id = port.get('ofc_id', None)
+                if port_ofc_id:
+                    ofc_id = port['ofc_id']
+                    switch_dpid = port['switch_dpid']
+                    break
+
+        # If no ofc_id found it, default ofc_id is used.
+        if not ofc_id and not switch_dpid:
+            ofc_id = "Default"
+
+        if ofc_id and ofc_id in self.config['ofcs_thread']:
+            r, c = self.config['ofcs_thread'][ofc_id].insert_task("update-net", net_id)
+        elif switch_dpid:
+
+            ofcs_dpid_list = self.config['ofcs_thread_dpid']
+            for ofc_t in ofcs_dpid_list:
+                if switch_dpid in ofc_t:
+                    r, c = ofc_t[switch_dpid].insert_task("update-net", net_id)
+
+        if r < 0:
+            message = "Cannot insert a task for updating network '$s', %s", net_id, c
+            self.logger.error(message)
+            raise ovimException(message, HTTP_Internal_Server_Error)
+
     def delete_port(self, port_id):
         # Look for the previous port data
         result, ports = self.db.get_table(WHERE={'uuid': port_id, "type": "external"}, FROM='ports')
@@ -878,9 +959,16 @@ class ovim():
         network = ports[0].get('net_id', None)
         if network:
             # change of net.
-            r, c = self.config['of_thread'].insert_task("update-net", network)
-            if r < 0:
-                self.logger.error("Cannot insert a task for updating network '$s' %s", network, c)
+
+            try:
+                self.net_update_ofc_thread(network)
+            except ovimException as e:
+                raise ovimException("Cannot insert a task for delete network '{}' {}".format(network, str(e)),
+                                    HTTP_Internal_Server_Error)
+            except Exception as e:
+                raise ovimException("Cannot insert a task for delete network '{}' {}".format(network, str(e)),
+                                    HTTP_Internal_Server_Error)
+
         return content
 
     def edit_port(self, port_id, port_data, admin=True):
@@ -931,10 +1019,15 @@ class ovim():
         # Insert task to complete actions
         if result > 0:
             for net_id in nets:
-                r, v = self.config['of_thread'].insert_task("update-net", net_id)
-                if r < 0:
-                    self.logger.error("Error updating network '{}' {}".format(r,v))
-                    # TODO Do something if fails
+                try:
+                    self.net_update_ofc_thread(net_id)
+                except ovimException as e:
+                    raise ovimException("Error updating network'{}' {}".format(net_id, str(e)),
+                                        HTTP_Internal_Server_Error)
+                except Exception as e:
+                    raise ovimException("Error updating network '{}' {}".format(net_id, str(e)),
+                                        HTTP_Internal_Server_Error)
+
             if host_id:
                 r, v = self.config['host_threads'][host_id].insert_task("edit-iface", port_id, old_net, new_net)
                 if r < 0:
@@ -1213,8 +1306,8 @@ class ovim():
         host_develop_mode = True if self.config['mode'] == 'development' else False
 
         dhcp_host = ht.host_thread(name='openvim_controller', user=ovs_controller_user, host=controller_ip,
-                                   db=self.config['db'],
-                                   db_lock=self.config['db_lock'], test=host_test_mode,
+                                   db=self.db_of,
+                                   db_lock=self.db_lock, test=host_test_mode,
                                    image_path=self.config['image_path'], version=self.config['version'],
                                    host_id='openvim_controller', develop_mode=host_develop_mode,
                                    develop_bridge_iface=bridge_ifaces)