+ def _start_of_db_tasks(self):
+ """
+ Start ofc task for existing ofcs in database
+ :param db_of:
+ :param db_lock:
+ :return:
+ """
+ ofcs = self.get_of_controllers()
+
+ for ofc in ofcs:
+ of_conn = self._load_of_module(ofc)
+ # create ofc thread per of controller
+ self._create_ofc_task(ofc['uuid'], ofc['dpid'], of_conn)
+
+ def _create_ofc_task(self, ofc_uuid, dpid, of_conn):
+ """
+ Create an ofc thread for handle each sdn controllers
+ :param ofc_uuid: sdn controller uuid
+ :param dpid: sdn controller dpid
+ :param of_conn: OF_conn module
+ :return:
+ """
+ if 'ofcs_thread' not in self.config and 'ofcs_thread_dpid' not in self.config:
+ ofcs_threads = {}
+ ofcs_thread_dpid = []
+ else:
+ ofcs_threads = self.config['ofcs_thread']
+ ofcs_thread_dpid = self.config['ofcs_thread_dpid']
+
+ if ofc_uuid not in ofcs_threads:
+ ofc_thread = self._create_ofc_thread(of_conn, ofc_uuid)
+ if ofc_uuid == "Default":
+ self.config['of_thread'] = ofc_thread
+
+ ofcs_threads[ofc_uuid] = ofc_thread
+ self.config['ofcs_thread'] = ofcs_threads
+
+ ofcs_thread_dpid.append({dpid: ofc_thread})
+ self.config['ofcs_thread_dpid'] = ofcs_thread_dpid
+
+ def _start_ofc_default_task(self):
+ """
+ Create default ofc thread
+ """
+ if 'of_controller' not in self.config \
+ and 'of_controller_ip' not in self.config \
+ and 'of_controller_port' not in self.config \
+ and 'of_controller_dpid' not in self.config:
+ return
+
+ # OF THREAD
+ db_config = {}
+ db_config['ip'] = self.config.get('of_controller_ip')
+ db_config['port'] = self.config.get('of_controller_port')
+ db_config['dpid'] = self.config.get('of_controller_dpid')
+ db_config['type'] = self.config.get('of_controller')
+ db_config['user'] = self.config.get('of_user')
+ db_config['password'] = self.config.get('of_password')
+
+ # create connector to the openflow controller
+ # load other parameters starting by of_ from config dict in a temporal dict
+
+ of_conn = self._load_of_module(db_config)
+ # create openflow thread
+ self._create_ofc_task("Default", db_config['dpid'], of_conn)
+
+ def _load_of_module(self, db_config):
+ """
+ import python module for each SDN controller supported
+ :param db_config: SDN dn information
+ :return: Module
+ """
+ if not db_config:
+ raise ovimException("No module found it", HTTP_Internal_Server_Error)
+
+ module_info = None
+
+ 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']})
+ temp_dict = {}
+
+ if db_config:
+ temp_dict['of_ip'] = db_config['ip']
+ temp_dict['of_port'] = db_config['port']
+ temp_dict['of_dpid'] = db_config['dpid']
+ temp_dict['of_controller'] = db_config['type']
+
+ temp_dict['of_debug'] = self.config['log_level_of']
+
+ if temp_dict['of_controller'] == 'opendaylight':
+ module = "ODL"
+ else:
+ module = temp_dict['of_controller']
+
+ if module not in ovim.of_module:
+ module_info = imp.find_module(module)
+ of_conn_module = imp.load_module("OF_conn", *module_info)
+ ovim.of_module[module] = of_conn_module
+ else:
+ of_conn_module = ovim.of_module[module]
+
+ try:
+ return of_conn_module.OF_conn(temp_dict)
+ except Exception as e:
+ self.logger.error("Cannot open the Openflow controller '%s': %s", type(e).__name__, str(e))
+ if module_info and module_info[0]:
+ file.close(module_info[0])
+ raise ovimException("Cannot open the Openflow controller '{}': '{}'".format(type(e).__name__, str(e)),
+ HTTP_Internal_Server_Error)
+ except (IOError, ImportError) as e:
+ if module_info and module_info[0]:
+ file.close(module_info[0])
+ self.logger.error("Cannot open openflow controller module '%s'; %s: %s; revise 'of_controller' "
+ "field of configuration file.", module, type(e).__name__, str(e))
+ raise ovimException("Cannot open openflow controller module '{}'; {}: {}; revise 'of_controller' "
+ "field of configuration file.".format(module, type(e).__name__, str(e)),
+ HTTP_Internal_Server_Error)
+
+ def _create_ofc_thread(self, of_conn, ofc_uuid="Default"):
+ """
+ Create and launch a of thread
+ :return: thread obj
+ """
+ # create openflow thread
+
+ if 'of_controller_nets_with_same_vlan' in self.config:
+ ofc_net_same_vlan = self.config['of_controller_nets_with_same_vlan']
+ else:
+ ofc_net_same_vlan = False
+
+ thread = oft.openflow_thread(ofc_uuid, of_conn, of_test=self.of_test_mode, db=self.db_of, db_lock=self.db_lock,
+ pmp_with_same_vlan=ofc_net_same_vlan, debug=self.config['log_level_of'])
+ #r, c = thread.OF_connector.obtain_port_correspondence()
+ #if r < 0:
+ # raise ovimException("Cannot get openflow information %s", c)
+ thread.start()
+ return thread
+