Remove ECOMP in headers
[clamp.git] / src / main / java / org / onap / clamp / clds / sdc / controller / SdcSingleController.java
index f8fef39..c9bbd06 100644 (file)
@@ -18,7 +18,7 @@
  * limitations under the License.\r
  * ============LICENSE_END============================================\r
  * ===================================================================\r
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * \r
  */\r
 \r
 package org.onap.clamp.clds.sdc.controller;\r
@@ -27,6 +27,7 @@ import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;\r
 \r
 import java.util.Date;\r
+import java.util.concurrent.ThreadLocalRandom;\r
 \r
 import org.onap.clamp.clds.config.ClampProperties;\r
 import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfiguration;\r
@@ -38,17 +39,17 @@ import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;\r
 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;\r
 import org.onap.clamp.clds.util.LoggingUtils;\r
-import org.openecomp.sdc.api.IDistributionClient;\r
-import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;\r
-import org.openecomp.sdc.api.consumer.INotificationCallback;\r
-import org.openecomp.sdc.api.notification.IArtifactInfo;\r
-import org.openecomp.sdc.api.notification.INotificationData;\r
-import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;\r
-import org.openecomp.sdc.api.results.IDistributionClientResult;\r
-import org.openecomp.sdc.impl.DistributionClientFactory;\r
-import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;\r
-import org.openecomp.sdc.utils.DistributionActionResultEnum;\r
-import org.openecomp.sdc.utils.DistributionStatusEnum;\r
+import org.onap.sdc.api.IDistributionClient;\r
+import org.onap.sdc.api.consumer.IDistributionStatusMessage;\r
+import org.onap.sdc.api.consumer.INotificationCallback;\r
+import org.onap.sdc.api.notification.IArtifactInfo;\r
+import org.onap.sdc.api.notification.INotificationData;\r
+import org.onap.sdc.api.results.IDistributionClientDownloadResult;\r
+import org.onap.sdc.api.results.IDistributionClientResult;\r
+import org.onap.sdc.impl.DistributionClientFactory;\r
+import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;\r
+import org.onap.sdc.utils.DistributionActionResultEnum;\r
+import org.onap.sdc.utils.DistributionStatusEnum;\r
 \r
 /**\r
  * This class handles one sdc controller defined in the config.\r
@@ -56,20 +57,24 @@ import org.openecomp.sdc.utils.DistributionStatusEnum;
 public class SdcSingleController {\r
 \r
     private static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcSingleController.class);\r
-    protected boolean isAsdcClientAutoManaged = false;\r
-    protected CsarInstaller csarInstaller;\r
-    protected ClampProperties refProp;\r
+    private boolean isSdcClientAutoManaged = false;\r
+    private CsarInstaller csarInstaller;\r
+    private ClampProperties refProp;\r
     public static final String CONFIG_SDC_FOLDER = "sdc.csarFolder";\r
+    private int nbOfNotificationsOngoing = 0;\r
+    private SdcSingleControllerStatus controllerStatus = SdcSingleControllerStatus.STOPPED;\r
+    private SdcSingleControllerConfiguration sdcConfig;\r
+    private IDistributionClient distributionClient;\r
 \r
     /**\r
      * Inner class for Notification callback\r
      */\r
-    private final class ASDCNotificationCallBack implements INotificationCallback {\r
+    private final class SdcNotificationCallBack implements INotificationCallback {\r
 \r
-        private SdcSingleController asdcController;\r
+        private SdcSingleController sdcController;\r
 \r
-        ASDCNotificationCallBack(SdcSingleController controller) {\r
-            asdcController = controller;\r
+        SdcNotificationCallBack(SdcSingleController controller) {\r
+            sdcController = controller;\r
         }\r
 \r
         /**\r
@@ -79,23 +84,26 @@ public class SdcSingleController {
         @Override\r
         public void activateCallback(INotificationData iNotif) {\r
             Date startTime = new Date();\r
-            String event = "Receive a callback notification in ASDC, nb of resources: " + iNotif.getResources().size();\r
-            logger.debug(event);\r
-            asdcController.treatNotification(iNotif);\r
+            logger.info("Receive a callback notification in SDC, nb of resources: " + iNotif.getResources().size());\r
+            sdcController.treatNotification(iNotif);\r
             LoggingUtils.setTimeContext(startTime, new Date());\r
             LoggingUtils.setResponseContext("0", "SDC Notification received and processed successfully",\r
                     this.getClass().getName());\r
         }\r
     }\r
 \r
-    // ***** Controller STATUS code\r
-    protected int nbOfNotificationsOngoing = 0;\r
-\r
     public int getNbOfNotificationsOngoing() {\r
         return nbOfNotificationsOngoing;\r
     }\r
 \r
-    private SdcSingleControllerStatus controllerStatus = SdcSingleControllerStatus.STOPPED;\r
+    private void changeControllerStatusIdle() {\r
+        if (this.nbOfNotificationsOngoing > 1) {\r
+            --this.nbOfNotificationsOngoing;\r
+        } else {\r
+            this.nbOfNotificationsOngoing = 0;\r
+            this.controllerStatus = SdcSingleControllerStatus.IDLE;\r
+        }\r
+    }\r
 \r
     protected final synchronized void changeControllerStatus(SdcSingleControllerStatus newControllerStatus) {\r
         switch (newControllerStatus) {\r
@@ -104,12 +112,7 @@ public class SdcSingleController {
                 this.controllerStatus = newControllerStatus;\r
                 break;\r
             case IDLE:\r
-                if (this.nbOfNotificationsOngoing > 1) {\r
-                    --this.nbOfNotificationsOngoing;\r
-                } else {\r
-                    this.nbOfNotificationsOngoing = 0;\r
-                    this.controllerStatus = newControllerStatus;\r
-                }\r
+                this.changeControllerStatusIdle();\r
                 break;\r
             default:\r
                 this.controllerStatus = newControllerStatus;\r
@@ -121,13 +124,9 @@ public class SdcSingleController {
         return this.controllerStatus;\r
     }\r
 \r
-    // ***** END of Controller STATUS code\r
-    protected SdcSingleControllerConfiguration sdcConfig;\r
-    private IDistributionClient distributionClient;\r
-\r
     public SdcSingleController(ClampProperties clampProp, CsarInstaller csarInstaller,\r
             SdcSingleControllerConfiguration sdcSingleConfig, boolean isClientAutoManaged) {\r
-        this.isAsdcClientAutoManaged = isClientAutoManaged;\r
+        this.isSdcClientAutoManaged = isClientAutoManaged;\r
         this.sdcConfig = sdcSingleConfig;\r
         this.refProp = clampProp;\r
         this.csarInstaller = csarInstaller;\r
@@ -144,23 +143,23 @@ public class SdcSingleController {
      *             If there is an issue with the parameters provided\r
      */\r
     public void initSdc() throws SdcControllerException {\r
-        logger.debug("Attempt to initialize the SDC Controller");\r
+        logger.info("Attempt to initialize the SDC Controller");\r
         if (this.getControllerStatus() != SdcSingleControllerStatus.STOPPED) {\r
             throw new SdcControllerException("The controller is already initialized, call the closeSDC method first");\r
         }\r
         if (this.distributionClient == null) {\r
             distributionClient = DistributionClientFactory.createDistributionClient();\r
         }\r
-        IDistributionClientResult result = this.distributionClient.init(sdcConfig, new ASDCNotificationCallBack(this));\r
+        IDistributionClientResult result = this.distributionClient.init(sdcConfig, new SdcNotificationCallBack(this));\r
         if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {\r
-            logger.error("ASDC distribution client init failed with reason:" + result.getDistributionMessageResult());\r
+            logger.error("SDC distribution client init failed with reason:" + result.getDistributionMessageResult());\r
             this.changeControllerStatus(SdcSingleControllerStatus.STOPPED);\r
             throw new SdcControllerException("Initialization of the SDC Controller failed with reason: "\r
                     + result.getDistributionMessageResult());\r
         }\r
         result = this.distributionClient.start();\r
         if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {\r
-            logger.debug("SDC distribution client start failed with reason:" + result.getDistributionMessageResult());\r
+            logger.error("SDC distribution client start failed with reason:" + result.getDistributionMessageResult());\r
             this.changeControllerStatus(SdcSingleControllerStatus.STOPPED);\r
             throw new SdcControllerException(\r
                     "Startup of the SDC Controller failed with reason: " + result.getDistributionMessageResult());\r
@@ -177,15 +176,15 @@ public class SdcSingleController {
      */\r
     public void closeSdc() throws SdcControllerException {\r
         if (this.getControllerStatus() == SdcSingleControllerStatus.BUSY) {\r
-            throw new SdcControllerException("Cannot close the ASDC controller as it's currently in BUSY state");\r
+            throw new SdcControllerException("Cannot close the SDC controller as it's currently in BUSY state");\r
         }\r
         if (this.distributionClient != null) {\r
             this.distributionClient.stop();\r
             // If auto managed we can set it to Null, SdcController controls it.\r
             // In the other case the client of this class has specified it, so\r
             // we can't reset it\r
-            if (isAsdcClientAutoManaged) {\r
-                // Next init will initialize it with a new Sdc Client\r
+            if (isSdcClientAutoManaged) {\r
+                // Next init will initialize it with a new SDC Client\r
                 this.distributionClient = null;\r
             }\r
         }\r
@@ -201,46 +200,54 @@ public class SdcSingleController {
     public void treatNotification(INotificationData iNotif) {\r
         CsarHandler csar = null;\r
         try {\r
+            // wait for a random time, so that 2 running Clamp will not treat\r
+            // the same Notification at the same time\r
+            long i = ThreadLocalRandom.current().nextInt(1, 5);\r
+            Thread.sleep(i * 1000L);\r
             logger.info("Notification received for service UUID:" + iNotif.getServiceUUID());\r
             this.changeControllerStatus(SdcSingleControllerStatus.BUSY);\r
             csar = new CsarHandler(iNotif, this.sdcConfig.getSdcControllerName(),\r
                     refProp.getStringValue(CONFIG_SDC_FOLDER));\r
+            csar.save(downloadTheArtifact(csar.getArtifactElement()));\r
             if (csarInstaller.isCsarAlreadyDeployed(csar)) {\r
-                csar.save(downloadTheArtifact(csar.getArtifactElement()));\r
-                this.sendASDCNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),\r
+                this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),\r
+                        sdcConfig.getConsumerID(), iNotif.getDistributionID(),\r
+                        DistributionStatusEnum.ALREADY_DOWNLOADED, null, System.currentTimeMillis());\r
+                this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),\r
+                        sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.ALREADY_DEPLOYED,\r
+                        null, System.currentTimeMillis());\r
+            } else {\r
+                this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),\r
                         sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_OK, null,\r
                         System.currentTimeMillis());\r
                 csarInstaller.installTheCsar(csar);\r
-                this.sendASDCNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),\r
+                this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),\r
                         sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_OK, null,\r
                         System.currentTimeMillis());\r
-            } else {\r
-                this.sendASDCNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),\r
-                        sdcConfig.getConsumerID(), iNotif.getDistributionID(),\r
-                        DistributionStatusEnum.ALREADY_DOWNLOADED, null, System.currentTimeMillis());\r
-                this.sendASDCNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),\r
-                        sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.ALREADY_DEPLOYED,\r
-                        null, System.currentTimeMillis());\r
             }\r
         } catch (SdcArtifactInstallerException e) {\r
             logger.error("SdcArtifactInstallerException exception caught during the notification processing", e);\r
-            this.sendASDCNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),\r
+            this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),\r
                     sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR,\r
                     e.getMessage(), System.currentTimeMillis());\r
         } catch (SdcDownloadException e) {\r
             logger.error("SdcDownloadException exception caught during the notification processing", e);\r
-            this.sendASDCNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),\r
+            this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),\r
                     sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR,\r
                     e.getMessage(), System.currentTimeMillis());\r
         } catch (CsarHandlerException e) {\r
             logger.error("CsarHandlerException exception caught during the notification processing", e);\r
-            this.sendASDCNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),\r
-                    sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR,\r
-                    e.getMessage(), System.currentTimeMillis());\r
+            this.sendSdcNotification(NotificationType.DOWNLOAD, null, sdcConfig.getConsumerID(),\r
+                    iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR, e.getMessage(),\r
+                    System.currentTimeMillis());\r
         } catch (SdcToscaParserException e) {\r
-            this.sendASDCNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),\r
+            logger.error("SdcToscaParserException exception caught during the notification processing", e);\r
+            this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),\r
                     sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR,\r
                     e.getMessage(), System.currentTimeMillis());\r
+        } catch (InterruptedException e) {\r
+            logger.error("Interrupt exception caught during the notification processing", e);\r
+            Thread.currentThread().interrupt();\r
         } catch (RuntimeException e) {\r
             logger.error("Unexpected exception caught during the notification processing", e);\r
         } finally {\r
@@ -253,7 +260,7 @@ public class SdcSingleController {
     }\r
 \r
     private IDistributionClientDownloadResult downloadTheArtifact(IArtifactInfo artifact) throws SdcDownloadException {\r
-        logger.debug("Trying to download the artifact : " + artifact.getArtifactURL() + " UUID: "\r
+        logger.info("Trying to download the artifact : " + artifact.getArtifactURL() + " UUID: "\r
                 + artifact.getArtifactUUID());\r
         IDistributionClientDownloadResult downloadResult;\r
         try {\r
@@ -270,17 +277,17 @@ public class SdcSingleController {
                     + artifact.getArtifactUUID() + "Size of payload " + downloadResult.getArtifactPayload().length);\r
         } else {\r
             throw new SdcDownloadException("Artifact " + artifact.getArtifactName()\r
-                    + " could not be downloaded from ASDC URL " + artifact.getArtifactURL() + " UUID "\r
+                    + " could not be downloaded from SDC URL " + artifact.getArtifactURL() + " UUID "\r
                     + artifact.getArtifactUUID() + ")" + System.lineSeparator() + "Error message is "\r
                     + downloadResult.getDistributionMessageResult() + System.lineSeparator());\r
         }\r
         return downloadResult;\r
     }\r
 \r
-    private void sendASDCNotification(NotificationType notificationType, String artifactURL, String consumerID,\r
+    private void sendSdcNotification(NotificationType notificationType, String artifactURL, String consumerID,\r
             String distributionID, DistributionStatusEnum status, String errorReason, long timestamp) {\r
         String event = "Sending " + notificationType.name() + "(" + status.name() + ")"\r
-                + " notification to ASDC for artifact:" + artifactURL;\r
+                + " notification to SDC for artifact:" + artifactURL;\r
         if (errorReason != null) {\r
             event = event + "(" + errorReason + ")";\r
         }\r
@@ -291,27 +298,35 @@ public class SdcSingleController {
                     status, timestamp);\r
             switch (notificationType) {\r
                 case DOWNLOAD:\r
-                    if (errorReason != null) {\r
-                        this.distributionClient.sendDownloadStatus(message, errorReason);\r
-                    } else {\r
-                        this.distributionClient.sendDownloadStatus(message);\r
-                    }\r
+                    this.sendDownloadStatus(message, errorReason);\r
                     action = "sendDownloadStatus";\r
                     break;\r
                 case DEPLOY:\r
-                    if (errorReason != null) {\r
-                        this.distributionClient.sendDeploymentStatus(message, errorReason);\r
-                    } else {\r
-                        this.distributionClient.sendDeploymentStatus(message);\r
-                    }\r
+                    this.sendDeploymentStatus(message, errorReason);\r
                     action = "sendDeploymentdStatus";\r
                     break;\r
                 default:\r
                     break;\r
             }\r
         } catch (RuntimeException e) {\r
-            logger.warn("Unable to send the Sdc Notification (" + action + ") due to an exception", e);\r
+            logger.warn("Unable to send the SDC Notification (" + action + ") due to an exception", e);\r
+        }\r
+        logger.info("SDC Notification sent successfully(" + action + ")");\r
+    }\r
+\r
+    private void sendDownloadStatus(IDistributionStatusMessage message, String errorReason) {\r
+        if (errorReason != null) {\r
+            this.distributionClient.sendDownloadStatus(message, errorReason);\r
+        } else {\r
+            this.distributionClient.sendDownloadStatus(message);\r
+        }\r
+    }\r
+\r
+    private void sendDeploymentStatus(IDistributionStatusMessage message, String errorReason) {\r
+        if (errorReason != null) {\r
+            this.distributionClient.sendDeploymentStatus(message, errorReason);\r
+        } else {\r
+            this.distributionClient.sendDeploymentStatus(message);\r
         }\r
-        logger.info("Sdc Notification sent successfully(" + action + ")");\r
     }\r
 }\r