update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwcm / plugins / rwconman / rift / tasklets / rwconmantasklet / rwconman_conagent.py
index 543e51b..14c3b6f 100644 (file)
@@ -48,16 +48,17 @@ class ConfigAgentVnfrTypeError(Exception):
 
 
 class ConfigAccountHandler(object):
-    def __init__(self, dts, log, loop, on_add_config_agent, on_delete_config_agent):
+    def __init__(self, dts, log, loop, project, on_add_config_agent, on_delete_config_agent):
         self._log = log
         self._dts = dts
         self._loop = loop
+        self._project = project
         self._on_add_config_agent = on_add_config_agent
         self._on_delete_config_agent = on_delete_config_agent
 
         self._log.debug("creating config account handler")
         self.cloud_cfg_handler = rift.mano.config_agent.ConfigAgentSubscriber(
-            self._dts, self._log,
+            self._dts, self._log, self._project,
             rift.mano.config_agent.ConfigAgentCallbacks(
                 on_add_apply=self.on_config_account_added,
                 on_delete_apply=self.on_config_account_deleted,
@@ -77,6 +78,10 @@ class ConfigAccountHandler(object):
     def register(self):
         self.cloud_cfg_handler.register()
 
+    def deregister(self):
+        self.cloud_cfg_handler.deregister()
+
+
 class RiftCMConfigPlugins(object):
     """ NSM Config Agent Plugins """
     def __init__(self):
@@ -117,14 +122,16 @@ class RiftCMConfigAgent(object):
 
         self._config_plugins = RiftCMConfigPlugins()
         self._config_handler = ConfigAccountHandler(
-            self._dts, self._log, self._loop, self._on_config_agent, self._on_config_agent_delete)
+            self._dts, self._log, self._loop, parent._project,
+            self._on_config_agent, self._on_config_agent_delete)
         self._plugin_instances = {}
         self._default_account_added = False
 
     @asyncio.coroutine
     def invoke_config_agent_plugins(self, method, nsr, vnfr, *args):
         # Invoke the methods on all config agent plugins registered
-        rc = False
+        rc = True
+        
         for agent in self._plugin_instances.values():
             if not agent.is_vnfr_managed(vnfr.id):
                 continue
@@ -133,27 +140,26 @@ class RiftCMConfigAgent(object):
                 rc = yield from agent.invoke(method, nsr, vnfr, *args)
                 break
             except Exception as e:
-                self._log.error("Error invoking {} on {} : {}".
-                                format(method, agent.name, e))
-                raise
+                self._log.exception("Error invoking {} on {} : {}".
+                                    format(method, agent.name, e))
+                raise e
 
         self._log.info("vnfr({}), method={}, return rc={}"
                        .format(vnfr.name, method, rc))
         return rc
 
-    def is_vnfr_config_agent_managed(self, vnfr):
-        if (not vnfr.has_field('netconf') and
-            not vnfr.has_field('juju') and
-            not vnfr.has_field('script')):
-            return False
-
+    def get_vnfr_config_agent(self, vnfr):
         for agent in self._plugin_instances.values():
             try:
                 if agent.is_vnfr_managed(vnfr.id):
-                    return True
+                    return agent
             except Exception as e:
                 self._log.debug("Check if VNFR {} is config agent managed: {}".
                                 format(vnfr.name, e))
+
+    def is_vnfr_config_agent_managed(self, vnfr):
+        if self.get_vnfr_config_agent(vnfr):
+            return True
         return False
 
     def _on_config_agent(self, config_agent):
@@ -175,7 +181,8 @@ class RiftCMConfigAgent(object):
         else:
             # Otherwise, instantiate a new plugin using the config agent account
             self._log.debug("Instantiting new config agent using class: %s", cap_inst)
-            new_instance = cap_inst(self._dts, self._log, self._loop, config_agent)
+            new_instance = cap_inst(self._dts, self._log, self._loop,
+                                    self._ConfigManagerConfig._project, config_agent)
             self._plugin_instances[cap_name] = new_instance
 
         # TODO (pjoseph): See why this was added, as this deletes the
@@ -188,7 +195,7 @@ class RiftCMConfigAgent(object):
     def _on_config_agent_delete(self, config_agent):
         self._log.debug("Got nsm plugin config agent delete, account: %s, type: %s",
                 config_agent.name, config_agent.account_type)
-        cap_name = config_agent.account_type
+        cap_name = config_agent.name
         if cap_name in self._plugin_instances:
             self._log.debug("Config agent nsm plugin exists, deleting it.")
             del self._plugin_instances[cap_name]
@@ -200,8 +207,8 @@ class RiftCMConfigAgent(object):
     def register(self):
         self._log.debug("Registering for config agent nsm plugin manager")
         yield from self._config_handler.register()
-
-        account = rwcfg_agent.ConfigAgentAccount()
+                            
+        account = rwcfg_agent.YangData_RwProject_Project_ConfigAgent_Account()
         account.account_type = DEFAULT_CAP_TYPE
         account.name = "RiftCA"
         self._on_config_agent(account)
@@ -212,10 +219,15 @@ class RiftCMConfigAgent(object):
         for account in config_agents:
             self._on_config_agent(account)
 
+    def deregister(self):
+        self._log.debug("De-registering config agent nsm plugin manager".
+                        format(self._ConfigManagerConfig._project))
+        self._config_handler.deregister()
+
     def set_config_agent(self, nsr, vnfr, method):
         if method == 'juju':
             agent_type = 'juju'
-        elif method in ['netconf', 'script']:
+        elif method in ['script']:
             agent_type = DEFAULT_CAP_TYPE
         else:
             msg = "Unsupported configuration method ({}) for VNF:{}/{}". \
@@ -253,7 +265,7 @@ class RiftCMConfigAgent(object):
         for agent in self._plugin_instances:
             if self._plugin_instances[agent].agent_type == agent_type:
                 self._plugin_instances[agent].add_vnfr_managed(vnfr)
-                self._log.debug("Added vnfr {} as config plugin {} managed".
+                self._log.debug("Added vnfr from {} from default CAs as config plugin {} managed".
                                 format(vnfr.name, agent))
                 return