fix 1150 limit error length of a failing SDN connector
[osm/RO.git] / RO / osm_ro / wim / wim_thread.py
index 13502b9..7d57d55 100644 (file)
@@ -45,7 +45,7 @@ from contextlib import contextmanager
 from functools import partial
 from itertools import islice, chain, takewhile
 from operator import itemgetter, attrgetter
 from functools import partial
 from itertools import islice, chain, takewhile
 from operator import itemgetter, attrgetter
-from sys import exc_info
+from sys import exc_info
 from time import time, sleep
 
 import queue
 from time import time, sleep
 
 import queue
@@ -59,11 +59,9 @@ from .errors import (
     InvalidParameters as Invalid,
     UndefinedAction,
 )
     InvalidParameters as Invalid,
     UndefinedAction,
 )
-from .failing_connector import FailingConnector
-from .wimconn import WimConnectorError
-from .wimconn_dynpac import DynpacConnector
-from .wimconn_fake import FakeConnector
-from .wimconn_ietfl2vpn import WimconnectorIETFL2VPN
+from osm_ro_plugin.sdn_failing import SdnFailingConnector
+from osm_ro_plugin.sdnconn import SdnConnectorError
+from osm_ro_plugin.sdn_dummy import SdnDummyConnector
 
 ACTIONS = {
     'instance_wim_nets': wan_link_actions.ACTIONS
 
 ACTIONS = {
     'instance_wim_nets': wan_link_actions.ACTIONS
@@ -71,10 +69,8 @@ ACTIONS = {
 
 CONNECTORS = {
     # "odl": wimconn_odl.OdlConnector,
 
 CONNECTORS = {
     # "odl": wimconn_odl.OdlConnector,
-    "dynpac": DynpacConnector,
-    "fake": FakeConnector,
-    "tapi": WimconnectorIETFL2VPN,
-    # Add extra connectors here
+    "dummy": SdnDummyConnector,
+    # Add extra connectors here not managed via plugins
 }
 
 
 }
 
 
@@ -101,17 +97,21 @@ class WimThread(threading.Thread):
     MAX_RECOVERY_TIME = 180
     WAITING_TIME = 1      # Wait 1s for taks to arrive, when there are none
 
     MAX_RECOVERY_TIME = 180
     WAITING_TIME = 1      # Wait 1s for taks to arrive, when there are none
 
-    def __init__(self, persistence, wim_account, logger=None, ovim=None):
+    def __init__(self, persistence, plugins, wim_account, logger=None, ovim=None):
         """Init a thread.
 
         Arguments:
             persistence: Database abstraction layer
         """Init a thread.
 
         Arguments:
             persistence: Database abstraction layer
+            plugins: dictionary with the vim/sdn plugins
             wim_account: Record containing wim_account, tenant and wim
                 information.
         """
         name = '{}.{}.{}'.format(wim_account['wim']['name'],
                                  wim_account['name'], wim_account['uuid'])
         super(WimThread, self).__init__(name=name)
             wim_account: Record containing wim_account, tenant and wim
                 information.
         """
         name = '{}.{}.{}'.format(wim_account['wim']['name'],
                                  wim_account['name'], wim_account['uuid'])
         super(WimThread, self).__init__(name=name)
+        self.plugins = plugins
+        if "rosdn_dummy" not in self.plugins:
+            self.plugins["rosdn_dummy"] = SdnDummyConnector
 
         self.name = name
         self.connector = None
 
         self.name = name
         self.connector = None
@@ -160,9 +160,11 @@ class WimThread(threading.Thread):
             mapping = self.persist.query('wim_port_mappings',
                                          WHERE={'wim_id': wim['uuid']},
                                          error_if_none=False)
             mapping = self.persist.query('wim_port_mappings',
                                          WHERE={'wim_id': wim['uuid']},
                                          error_if_none=False)
-            return CONNECTORS[wim['type']](wim, account, {
-                'service_endpoint_mapping': mapping or []
-            })
+            if wim["type"] in CONNECTORS:
+                return CONNECTORS[wim['type']](wim, account, {'service_endpoint_mapping': mapping or []})
+            else:    # load a plugin
+                return self.plugins["rosdn_" + wim["type"]](
+                    wim, account, {'service_endpoint_mapping': mapping or []})
         except DbBaseException as ex:
             error_msg = ('Error when retrieving WIM account ({})\n'
                          .format(account_id)) + str(ex)
         except DbBaseException as ex:
             error_msg = ('Error when retrieving WIM account ({})\n'
                          .format(account_id)) + str(ex)
@@ -170,8 +172,8 @@ class WimThread(threading.Thread):
         except KeyError as ex:
             error_msg = ('Unable to find the WIM connector for WIM ({})\n'
                          .format(wim['type'])) + str(ex)
         except KeyError as ex:
             error_msg = ('Unable to find the WIM connector for WIM ({})\n'
                          .format(wim['type'])) + str(ex)
-            self.logger.error(error_msg, exc_info=True)
-        except (WimConnectorError, Exception) as ex:
+            self.logger.error(error_msg)
+        except (SdnConnectorError, Exception) as ex:
             # TODO: Remove the Exception class here when the connector class is
             # ready
             error_msg = ('Error when loading WIM connector for WIM ({})\n'
             # TODO: Remove the Exception class here when the connector class is
             # ready
             error_msg = ('Error when loading WIM connector for WIM ({})\n'
@@ -181,7 +183,7 @@ class WimThread(threading.Thread):
         error_msg_extra = ('Any task targeting WIM account {} ({}) will fail.'
                            .format(account_id, self.wim_account.get('name')))
         self.logger.warning(error_msg_extra)
         error_msg_extra = ('Any task targeting WIM account {} ({}) will fail.'
                            .format(account_id, self.wim_account.get('name')))
         self.logger.warning(error_msg_extra)
-        return FailingConnector(error_msg + '\n' + error_msg_extra)
+        return SdnFailingConnector(error_msg + '\n' + error_msg_extra)
 
     @contextmanager
     def avoid_exceptions(self):
 
     @contextmanager
     def avoid_exceptions(self):
@@ -386,7 +388,7 @@ class WimThread(threading.Thread):
                            self.process_list('refresh')):
                         sleep(self.WAITING_TIME)
 
                            self.process_list('refresh')):
                         sleep(self.WAITING_TIME)
 
-                    if isinstance(self.connector, FailingConnector):
+                    if isinstance(self.connector, SdnFailingConnector):
                         # Wait sometime to try instantiating the connector
                         # again and restart
                         # Increase the recovery time if restarting is not
                         # Wait sometime to try instantiating the connector
                         # again and restart
                         # Increase the recovery time if restarting is not