Fix sdc notif when already_deployed
[clamp.git] / src / main / java / org / onap / clamp / clds / sdc / controller / SdcSingleController.java
index 729ef49..f9b43ab 100644 (file)
@@ -33,17 +33,17 @@ import java.util.concurrent.ThreadLocalRandom;
 
 import org.onap.clamp.clds.config.ClampProperties;
 import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfiguration;
-import org.onap.clamp.clds.exception.policy.PolicyModelException;
+import org.onap.clamp.clds.exception.sdc.controller.BlueprintParserException;
 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
 import org.onap.clamp.clds.exception.sdc.controller.SdcControllerException;
 import org.onap.clamp.clds.exception.sdc.controller.SdcDownloadException;
-import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
-import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
 import org.onap.clamp.clds.util.LoggingUtils;
+import org.onap.clamp.loop.CsarInstaller;
 import org.onap.sdc.api.IDistributionClient;
+import org.onap.sdc.api.consumer.IComponentDoneStatusMessage;
 import org.onap.sdc.api.consumer.IDistributionStatusMessage;
 import org.onap.sdc.api.consumer.INotificationCallback;
 import org.onap.sdc.api.notification.IArtifactInfo;
@@ -173,8 +173,9 @@ public class SdcSingleController {
     /**
      * This method initializes the SDC Controller and the SDC Client.
      *
-     * @throws SdcControllerException It throws an exception if the SDC Client cannot be instantiated or if
-     *                                an init attempt is done when already initialized
+     * @throws SdcControllerException It throws an exception if the SDC Client
+     *                                cannot be instantiated or if an init attempt
+     *                                is done when already initialized
      */
     public void initSdc() throws SdcControllerException {
         logger.info("Attempt to initialize the SDC Controller: " + sdcConfig.getSdcControllerName());
@@ -184,14 +185,12 @@ public class SdcSingleController {
         if (distributionClient == null) {
             distributionClient = DistributionClientFactory.createDistributionClient();
         }
-        IDistributionClientResult result = distributionClient.init(sdcConfig,
-                new SdcNotificationCallBack(this));
+        IDistributionClientResult result = distributionClient.init(sdcConfig, new SdcNotificationCallBack(this));
         if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
             logger.error("SDC distribution client init failed with reason:" + result.getDistributionMessageResult());
             this.changeControllerStatus(SdcSingleControllerStatus.STOPPED);
-            throw new SdcControllerException(
-                    "Initialization of the SDC Controller failed with reason: "
-                            + result.getDistributionMessageResult());
+            throw new SdcControllerException("Initialization of the SDC Controller failed with reason: "
+                    + result.getDistributionMessageResult());
         }
         logger.info("SDC Controller successfully initialized: " + sdcConfig.getSdcControllerName());
         logger.info("Attempt to start the SDC Controller: " + sdcConfig.getSdcControllerName());
@@ -207,11 +206,12 @@ public class SdcSingleController {
     }
 
     /**
-    * This method closes the SDC Controller and the SDC Client.
-    *
-    * @throws SdcControllerException It throws an exception if the SDC Client cannot be closed because
-    *                                it's currently BUSY in processing notifications.
-    */
+     * This method closes the SDC Controller and the SDC Client.
+     *
+     * @throws SdcControllerException It throws an exception if the SDC Client
+     *                                cannot be closed because it's currently BUSY
+     *                                in processing notifications.
+     */
     public void closeSdc() throws SdcControllerException {
         if (this.getControllerStatus() == SdcSingleControllerStatus.BUSY) {
             throw new SdcControllerException("Cannot close the SDC controller as it's currently in BUSY state");
@@ -245,13 +245,11 @@ public class SdcSingleController {
                 this.sendSdcNotification(notificationType,
                         blueprint.getValue().getResourceAttached().getArtifacts().get(0).getArtifactURL(),
                         sdcConfig.getConsumerID(), notificationData.getDistributionID(), distributionStatus,
-                        errorMessage,
-                        System.currentTimeMillis());
+                        errorMessage, System.currentTimeMillis());
             }
         } else {
             this.sendSdcNotification(notificationType, null, sdcConfig.getConsumerID(),
-                    notificationData.getDistributionID(),
-                    distributionStatus, errorMessage, System.currentTimeMillis());
+                    notificationData.getDistributionID(), distributionStatus, errorMessage, System.currentTimeMillis());
         }
     }
 
@@ -283,28 +281,34 @@ public class SdcSingleController {
                 sendAllNotificationForCsarHandler(notificationData, csar, NotificationType.DEPLOY,
                         DistributionStatusEnum.DEPLOY_OK, null);
             }
+            this.sendComponentStatus(notificationData, DistributionStatusEnum.COMPONENT_DONE_OK, null);
         } catch (SdcArtifactInstallerException | SdcToscaParserException e) {
             logger.error("SdcArtifactInstallerException exception caught during the notification processing", e);
             sendAllNotificationForCsarHandler(notificationData, csar, NotificationType.DEPLOY,
                     DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
+            this.sendComponentStatus(notificationData, DistributionStatusEnum.COMPONENT_DONE_ERROR, e.getMessage());
         } catch (SdcDownloadException | CsarHandlerException e) {
             logger.error("SdcDownloadException exception caught during the notification processing", e);
             sendAllNotificationForCsarHandler(notificationData, csar, NotificationType.DOWNLOAD,
                     DistributionStatusEnum.DOWNLOAD_ERROR, e.getMessage());
-        } catch (PolicyModelException e) {
-            logger.error("PolicyModelException exception caught during the notification processing", e);
-            sendAllNotificationForCsarHandler(notificationData, csar, NotificationType.DEPLOY,
-                    DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
+            this.sendComponentStatus(notificationData, DistributionStatusEnum.COMPONENT_DONE_ERROR, e.getMessage());
         } catch (InterruptedException e) {
             logger.error("Interrupt exception caught during the notification processing", e);
             sendAllNotificationForCsarHandler(notificationData, csar, NotificationType.DEPLOY,
                     DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
+            this.sendComponentStatus(notificationData, DistributionStatusEnum.COMPONENT_DONE_ERROR, e.getMessage());
             Thread.currentThread().interrupt();
+        } catch (BlueprintParserException e) {
+            logger.error("BlueprintParser exception caught during the notification processing", e);
+            sendAllNotificationForCsarHandler(notificationData, csar, NotificationType.DEPLOY,
+                    DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
+            this.sendComponentStatus(notificationData, DistributionStatusEnum.COMPONENT_DONE_ERROR, e.getMessage());
         } catch (RuntimeException e) {
             logger.error("Unexpected exception caught during the notification processing", e);
             sendAllNotificationForCsarHandler(notificationData, csar, NotificationType.DEPLOY,
                     DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
-        }  finally {
+            this.sendComponentStatus(notificationData, DistributionStatusEnum.COMPONENT_DONE_ERROR, e.getMessage());
+        } finally {
             this.changeControllerStatus(SdcSingleControllerStatus.IDLE);
         }
     }
@@ -321,9 +325,8 @@ public class SdcSingleController {
     }
 
     private IDistributionClientDownloadResult downloadTheArtifact(IArtifactInfo artifact) throws SdcDownloadException {
-        logger.info(
-                "Trying to download the artifact : " + artifact.getArtifactURL() + " UUID: "
-                        + artifact.getArtifactUUID());
+        logger.info("Trying to download the artifact : " + artifact.getArtifactURL() + " UUID: "
+                + artifact.getArtifactUUID());
         IDistributionClientDownloadResult downloadResult;
         try {
             downloadResult = distributionClient.download(artifact);
@@ -338,18 +341,16 @@ public class SdcSingleController {
             logger.info("Successfully downloaded the artifact " + artifact.getArtifactURL() + " UUID "
                     + artifact.getArtifactUUID() + "Size of payload " + downloadResult.getArtifactPayload().length);
         } else {
-            throw new SdcDownloadException(
-                    "Artifact " + artifact.getArtifactName() + " could not be downloaded from SDC URL "
-                            + artifact.getArtifactURL() + " UUID " + artifact.getArtifactUUID() + ")"
-                            + System.lineSeparator()
-                            + "Error message is " + downloadResult.getDistributionMessageResult()
-                            + System.lineSeparator());
+            throw new SdcDownloadException("Artifact " + artifact.getArtifactName()
+                    + " could not be downloaded from SDC URL " + artifact.getArtifactURL() + " UUID "
+                    + artifact.getArtifactUUID() + ")" + System.lineSeparator() + "Error message is "
+                    + downloadResult.getDistributionMessageResult() + System.lineSeparator());
         }
         return downloadResult;
     }
 
-    private void sendSdcNotification(NotificationType notificationType, String artifactUrl, String consumerID,
-                                     String distributionID, DistributionStatusEnum status, String errorReason,
+    private void sendSdcNotification(NotificationType notificationType, String artifactUrl, String consumerId,
+                                     String distributionId, DistributionStatusEnum status, String errorReason,
                                      long timestamp) {
         String event = "Sending " + notificationType.name() + "(" + status.name() + ")"
                 + " notification to SDC for artifact:" + artifactUrl;
@@ -359,7 +360,7 @@ public class SdcSingleController {
         logger.info(event);
         String action = "";
         try {
-            IDistributionStatusMessage message = new DistributionStatusMessage(artifactUrl, consumerID, distributionID,
+            IDistributionStatusMessage message = new DistributionStatusMessage(artifactUrl, consumerId, distributionId,
                     status, timestamp);
             switch (notificationType) {
                 case DOWNLOAD:
@@ -379,6 +380,43 @@ public class SdcSingleController {
         logger.info("SDC Notification sent successfully(" + action + ")");
     }
 
+    private void sendComponentStatus(INotificationData notificationData, DistributionStatusEnum status,
+                                     String errorReason) {
+        try {
+            IComponentDoneStatusMessage message = new IComponentDoneStatusMessage() {
+
+                @Override public String getDistributionID() {
+                    return notificationData.getDistributionID();
+                }
+
+                @Override public String getConsumerID() {
+                    return sdcConfig.getConsumerID();
+                }
+
+                @Override public long getTimestamp() {
+                    return System.currentTimeMillis();
+                }
+
+                @Override public DistributionStatusEnum getStatus() {
+                    return status;
+                }
+
+                @Override public String getComponentName() {
+                    return sdcConfig.getUser();
+                }
+            };
+
+            if (errorReason != null) {
+                this.distributionClient.sendComponentDoneStatus(message, errorReason);
+            } else {
+                this.distributionClient.sendComponentDoneStatus(message);
+            }
+        } catch (RuntimeException e) {
+            logger.warn("Unable to send the SDC Notification (" + status.name() + ") due to an exception", e);
+        }
+        logger.info("SDC Notification sent successfully(" + status.name() + ")");
+    }
+
     private void sendDownloadStatus(IDistributionStatusMessage message, String errorReason) {
         if (errorReason != null) {
             this.distributionClient.sendDownloadStatus(message, errorReason);