Merge from OSM SO master
[osm/SO.git] / rwlaunchpad / plugins / rwnsm / rift / tasklets / rwnsmtasklet / cloud.py
index 6be3761..ef7856d 100644 (file)
@@ -40,6 +40,9 @@ class RwNsPlugin(rwnsmplugin.NsmPluginBase):
         self._log = log
         self._loop = loop
 
+    def set_state(self, nsr_id, state):
+        pass
+
     def create_nsr(self, nsr_msg, nsd,key_pairs=None):
         """
         Create Network service record
@@ -125,15 +128,17 @@ class NsmPlugins(object):
 
 
 class CloudAccountConfigSubscriber:
-    def __init__(self, log, dts, log_hdl):
+    def __init__(self, log, dts, log_hdl, project):
         self._dts = dts
         self._log = log
         self._log_hdl = log_hdl
+        self._project = project
 
         self._cloud_sub = rift.mano.cloud.CloudAccountConfigSubscriber(
                 self._dts,
                 self._log,
                 self._log_hdl,
+                self._project,
                 rift.mano.cloud.CloudAccountConfigCallbacks())
 
     def get_cloud_account_sdn_name(self, account_name):
@@ -151,6 +156,9 @@ class CloudAccountConfigSubscriber:
     def register(self):
        self._cloud_sub.register()
 
+    def deregister(self):
+       self._cloud_sub.deregister()
+
 
 class ROAccountPluginSelector(object):
     """
@@ -163,10 +171,11 @@ class ROAccountPluginSelector(object):
     """
     DEFAULT_PLUGIN = RwNsPlugin
 
-    def __init__(self, dts, log, loop, records_publisher):
+    def __init__(self, dts, log, loop, project, records_publisher):
         self._dts = dts
         self._log = log
         self._loop = loop
+        self._project = project
         self._records_publisher = records_publisher
 
         self._nsm_plugins = NsmPlugins()
@@ -175,24 +184,37 @@ class ROAccountPluginSelector(object):
                 self._log,
                 self._dts,
                 self._loop,
+                self._project,
                 callback=self.on_ro_account_change
                 )
+        self._nsr_sub = mano_dts.NsrCatalogSubscriber(
+                self._log,
+                self._dts,
+                self._loop,
+                self._project,
+                self.handle_nsr)
 
         # The default plugin will be RwNsPlugin
-        self._plugin_instances = {}
         self._ro_plugin = self._create_plugin(self.DEFAULT_PLUGIN, None)
+        self.live_instances = 0
 
     @property
     def ro_plugin(self):
         return self._ro_plugin
 
-    def on_ro_account_change(self, ro_account, action):
+    def handle_nsr(self, nsr, action):
         if action == rwdts.QueryAction.CREATE:
-            self._on_ro_account_added(ro_account)
+            self.live_instances += 1
+        elif action == rwdts.QueryAction.DELETE:
+            self.live_instances -= 1
+
+    def on_ro_account_change(self, ro_account, action):
+        if action in [rwdts.QueryAction.CREATE, rwdts.QueryAction.UPDATE]:
+            self._on_ro_account_change(ro_account)
         elif action == rwdts.QueryAction.DELETE:
             self._on_ro_account_deleted(ro_account)
 
-    def _on_ro_account_added(self, ro_account):
+    def _on_ro_account_change(self, ro_account):
         self._log.debug("Got nsm plugin RO account: %s", ro_account)
         try:
             nsm_cls = self._nsm_plugins.class_by_plugin_name(
@@ -205,25 +227,29 @@ class ROAccountPluginSelector(object):
                 )
             nsm_cls = self.DEFAULT_PLUGIN
 
-        self._ro_plugin = self._create_plugin(nsm_cls, ro_account)
+        ro_plugin = self._create_plugin(nsm_cls, ro_account)
+        if self.live_instances == 0:
+            self._ro_plugin = ro_plugin
+        else:
+            raise ValueError("Unable to change the plugin when live NS instances exists!")
 
     def _on_ro_account_deleted(self, ro_account):
         self._ro_plugin = None
 
     def _create_plugin(self, nsm_cls, ro_account):
-        # Check to see if the plugin was already instantiated
-        if nsm_cls in self._plugin_instances:
-            self._log.debug("RO account nsm plugin already instantiated.  Using existing.")
-            return self._plugin_instances[nsm_cls]
 
-        # Otherwise, instantiate a new plugin using the cloud account
         self._log.debug("Instantiating new RO account using class: %s", nsm_cls)
         nsm_instance = nsm_cls(self._dts, self._log, self._loop,
                                self._records_publisher, ro_account)
 
-        self._plugin_instances[nsm_cls] = nsm_instance
         return nsm_instance
 
     @asyncio.coroutine
     def register(self):
         yield from self._ro_sub.register()
+        yield from self._nsr_sub.register()
+
+    def deregister(self):
+        self._log.debug("Project {} de-register".format(self._project.name))
+        self._ro_sub.deregister()
+        self._nsr_sub.deregister()