Bug 172 Fix service primitive exec failure
[osm/SO.git] / rwcm / plugins / rwconman / rift / tasklets / rwconmantasklet / jujuconf.py
index 1c32fc9..add6a29 100644 (file)
 #
 
 import asyncio
+import os
 import re
 import tempfile
+import time
 import yaml
-import os
 
 import rift.mano.utils.juju_api as juju
 from . import riftcm_config_plugin
@@ -79,6 +80,16 @@ class JujuConfigPlugin(riftcm_config_plugin.RiftCMConfigPluginBase):
         return self._api
 
     @property
+    def agent_data(self):
+        return dict(
+            type=self.agent_type,
+            name=self.name,
+            host=self._ip_address,
+            port=self._port,
+            user=self._user,
+            secret=self._secret
+        )
+
     def vnfr(self, vnfr_id):
         try:
             vnfr = self._juju_vnfs[vnfr_id].vnfr
@@ -88,6 +99,12 @@ class JujuConfigPlugin(riftcm_config_plugin.RiftCMConfigPluginBase):
 
         return vnfr
 
+    def get_service_name(self, vnfr_id):
+        vnfr = self.vnfr(vnfr_id)
+        if vnfr and 'vnf_juju_name' in vnfr:
+            return vnfr['vnf_juju_name']
+        return None
+
     def juju_log(self, level, name, log_str, *args):
         if name is not None:
             g_log_str = 'jujuCA:({}) {}'.format(name, log_str)
@@ -165,7 +182,7 @@ class JujuConfigPlugin(riftcm_config_plugin.RiftCMConfigPluginBase):
         try:
             path = os.path.join(self._rift_artif_dir,
                                 'launchpad/libs',
-                                agent_vnfr.vnfr_msg.vnfd_ref,
+                                agent_vnfr.vnfr_msg.vnfd.id,
                                 'charms/trusty',
                                 charm)
             self._log.debug("jujuCA: Charm dir is {}".format(path))
@@ -284,10 +301,6 @@ class JujuConfigPlugin(riftcm_config_plugin.RiftCMConfigPluginBase):
     def vnf_config_primitive(self, nsr_id, vnfr_id, primitive, output):
         self._log.debug("jujuCA: VNF config primititve {} for nsr {}, vnfr_id {}".
                         format(primitive, nsr_id, vnfr_id))
-        output.execution_status = "failed"
-        output.execution_id = ''
-        output.execution_error_details = ''
-
         try:
             vnfr = self._juju_vnfs[vnfr_id].vnfr
         except KeyError:
@@ -295,6 +308,10 @@ class JujuConfigPlugin(riftcm_config_plugin.RiftCMConfigPluginBase):
                             vnfr_id)
             return
 
+        output.execution_status = "failed"
+        output.execution_id = ''
+        output.execution_error_details = ''
+
         try:
             service = vnfr['vnf_juju_name']
             vnf_config = vnfr['config']
@@ -309,40 +326,48 @@ class JujuConfigPlugin(riftcm_config_plugin.RiftCMConfigPluginBase):
                         if parameter.value:
                             val = self.xlate(parameter.value, vnfr['tags'])
                             # TBD do validation of the parameters
-                            data_type = 'string'
+                            data_type = 'STRING'
                             found = False
                             for ca_param in config.parameter:
                                 if ca_param.name == parameter.name:
                                     data_type = ca_param.data_type
                                     found = True
                                     break
-                                if data_type == 'integer':
-                                    val = int(parameter.value)
+                            try:
+                                if data_type == 'INTEGER':
+                                    tmp = int(val)
+                                    val = tmp
+                            except Exception as e:
+                                pass
+
                             if not found:
                                 self._log.warn("jujuCA: Did not find parameter {} for {}".
                                                format(parameter, config.name))
                             params.update({parameter.name: val})
 
                     if config.name == 'config':
+                        output.execution_id = 'config'
                         if len(params):
                             self._log.debug("jujuCA: applying config with params {} for service {}".
                                             format(params, service))
 
-                            rc = yield from self.api.apply_config(params, service=service)
+                            rc = yield from self.api.apply_config(params, service=service, wait=False)
 
                             if rc:
-                                output.execution_status = "completed"
+                                # Mark as pending and check later for the status
+                                output.execution_status = "pending"
                                 self._log.debug("jujuCA: applied config {} on {}".
                                                 format(params, service))
                             else:
                                 output.execution_status = 'failed'
-                                output.execution_error_Details = \
+                                output.execution_error_details = \
                                     'Failed to apply config: {}'.format(params)
                                 self._log.error("jujuCA: Error applying config {} on service {}".
                                                 format(params, service))
                         else:
-                            self._log.warn("jujuCA: Did not find valid paramaters for config : {}".
+                            self._log.warn("jujuCA: Did not find valid parameters for config : {}".
                                            format(primitive.parameter))
+                            output.execution_status = "completed"
                     else:
                         self._log.debug("jujuCA: Execute action {} on service {} with params {}".
                                         format(config.name, service, params))
@@ -531,11 +556,11 @@ class JujuConfigPlugin(riftcm_config_plugin.RiftCMConfigPluginBase):
                     self._log.info("jujuCA:(%s) Action %s with params %s",
                                    vnfr['vnf_juju_name'], action, params)
 
-                    resp = yield from self.api.execute_actions(action, params,
-                                                               service=service)
+                    resp = yield from self.api.execute_action(action, params,
+                                                              service=service)
                     if 'error' in resp:
-                        self._log.error("Applying initial config failed: {}".
-                                        format(resp))
+                        self._log.error("Applying initial config on {} failed for {} with {}: {}".
+                                        format(vnfr['vnf_juju_name'], action, params, resp))
                         return False
 
                     action_ids.append(resp['action']['tag'])
@@ -554,7 +579,7 @@ class JujuConfigPlugin(riftcm_config_plugin.RiftCMConfigPluginBase):
         while pending:
             pending = False
             for act in action_ids:
-                resp = yield from self.api.get_action_status(act, service=service)
+                resp = yield from self.api.get_action_status(act)
                 if 'error' in resp:
                     self._log.error("Initial config failed: {}".format(resp))
                     return False
@@ -647,9 +672,27 @@ class JujuConfigPlugin(riftcm_config_plugin.RiftCMConfigPluginBase):
         '''
 
         try:
-            return self.api._get_action_status(execution_id)
+            self._log.debug("jujuCA: Get action status for {}".format(execution_id))
+            resp = self.api._get_action_status(execution_id)
+            self._log.debug("jujuCA: Action status: {}".format(resp))
+            return resp
         except Exception as e:
             self._log.error("jujuCA: Error fetching execution status for %s",
                             execution_id)
             self._log.exception(e)
             raise e
+
+    def get_service_status(self, vnfr_id):
+        '''Get the service status, used by job status handle
+           Make sure this is NOT a coroutine
+        '''
+        service = self.get_service_name(vnfr_id)
+        if service is None:
+            self._log.error("jujuCA: VNFR {} not managed by this Juju agent".
+                            format(vnfr_id))
+            return None
+
+        # Delay for 3 seconds before checking as config apply takes a
+        # few seconds to transfer to the service
+        time.sleep(3)
+        return self.api._get_service_status(service=service)