Merge "Introduce new entities"
[clamp.git] / src / main / java / org / onap / clamp / clds / sdc / controller / SdcSingleController.java
index 2744ef7..c9405d2 100644 (file)
@@ -17,8 +17,9 @@
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
  * ============LICENSE_END============================================\r
+ * Modifications copyright (c) 2018 Nokia\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,14 +28,18 @@ import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;\r
 \r
 import java.util.Date;\r
+import java.util.Map.Entry;\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
+import org.onap.clamp.clds.exception.policy.PolicyModelException;\r
 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;\r
 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;\r
 import org.onap.clamp.clds.exception.sdc.controller.SdcControllerException;\r
 import org.onap.clamp.clds.exception.sdc.controller.SdcDownloadException;\r
 import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;\r
+import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;\r
 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
@@ -77,18 +82,17 @@ public class SdcSingleController {
         }\r
 \r
         /**\r
-         * This method can be called multiple times at the same moment. The\r
-         * controller must be thread safe !\r
+         * This method can be called multiple times at the same moment. The controller\r
+         * must be thread safe !\r
          */\r
         @Override\r
         public void activateCallback(INotificationData iNotif) {\r
             Date startTime = new Date();\r
-            String event = "Receive a callback notification in SDC, nb of resources: " + iNotif.getResources().size();\r
-            logger.debug(event);\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
+                this.getClass().getName());\r
         }\r
     }\r
 \r
@@ -107,16 +111,16 @@ public class SdcSingleController {
 \r
     protected final synchronized void changeControllerStatus(SdcSingleControllerStatus newControllerStatus) {\r
         switch (newControllerStatus) {\r
-            case BUSY:\r
-                ++this.nbOfNotificationsOngoing;\r
-                this.controllerStatus = newControllerStatus;\r
-                break;\r
-            case IDLE:\r
-                this.changeControllerStatusIdle();\r
-                break;\r
-            default:\r
-                this.controllerStatus = newControllerStatus;\r
-                break;\r
+        case BUSY:\r
+            ++this.nbOfNotificationsOngoing;\r
+            this.controllerStatus = newControllerStatus;\r
+            break;\r
+        case IDLE:\r
+            this.changeControllerStatusIdle();\r
+            break;\r
+        default:\r
+            this.controllerStatus = newControllerStatus;\r
+            break;\r
         }\r
     }\r
 \r
@@ -125,8 +129,9 @@ public class SdcSingleController {
     }\r
 \r
     public SdcSingleController(ClampProperties clampProp, CsarInstaller csarInstaller,\r
-            SdcSingleControllerConfiguration sdcSingleConfig, boolean isClientAutoManaged) {\r
-        this.isSdcClientAutoManaged = isClientAutoManaged;\r
+        SdcSingleControllerConfiguration sdcSingleConfig, IDistributionClient distributionClient) {\r
+        this.distributionClient = distributionClient;\r
+        isSdcClientAutoManaged = (distributionClient == null);\r
         this.sdcConfig = sdcSingleConfig;\r
         this.refProp = clampProp;\r
         this.csarInstaller = csarInstaller;\r
@@ -136,34 +141,36 @@ public class SdcSingleController {
      * This method initializes the SDC Controller and the SDC Client.\r
      *\r
      * @throws SdcControllerException\r
-     *             It throws an exception if the SDC Client cannot be\r
-     *             instantiated or if an init attempt is done when already\r
-     *             initialized\r
+     *         It throws an exception if the SDC Client cannot be instantiated or if\r
+     *         an init attempt is done when already initialized\r
      * @throws SdcParametersException\r
-     *             If there is an issue with the parameters provided\r
+     *         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: " + sdcConfig.getSdcControllerName());\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
+        if (distributionClient == null) {\r
             distributionClient = DistributionClientFactory.createDistributionClient();\r
         }\r
-        IDistributionClientResult result = this.distributionClient.init(sdcConfig, new SdcNotificationCallBack(this));\r
+        IDistributionClientResult result = distributionClient.init(sdcConfig, new SdcNotificationCallBack(this));\r
         if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {\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
+            throw new SdcControllerException(\r
+                "Initialization of the SDC Controller failed with reason: " + result.getDistributionMessageResult());\r
         }\r
+        logger.info("SDC Controller successfully initialized: " + sdcConfig.getSdcControllerName());\r
+        logger.info("Attempt to start the SDC Controller: " + sdcConfig.getSdcControllerName());\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
+                "Startup of the SDC Controller failed with reason: " + result.getDistributionMessageResult());\r
         }\r
+        logger.info("SDC Controller successfully started: " + sdcConfig.getSdcControllerName());\r
         this.changeControllerStatus(SdcSingleControllerStatus.IDLE);\r
     }\r
 \r
@@ -171,8 +178,8 @@ public class SdcSingleController {
      * This method closes the SDC Controller and the SDC Client.\r
      *\r
      * @throws SdcControllerException\r
-     *             It throws an exception if the SDC Client cannot be closed\r
-     *             because it's currently BUSY in processing notifications.\r
+     *         It throws an exception if the SDC Client cannot be closed because\r
+     *         it's currently BUSY in processing notifications.\r
      */\r
     public void closeSdc() throws SdcControllerException {\r
         if (this.getControllerStatus() == SdcSingleControllerStatus.BUSY) {\r
@@ -191,59 +198,80 @@ public class SdcSingleController {
         this.changeControllerStatus(SdcSingleControllerStatus.STOPPED);\r
     }\r
 \r
+    private void sendAllNotificationForCsarHandler(INotificationData iNotif, CsarHandler csar,\r
+        NotificationType notificationType, DistributionStatusEnum distributionStatus, String errorMessage) {\r
+        if (csar != null) {\r
+            // Notify for the CSAR\r
+            this.sendSdcNotification(notificationType, csar.getArtifactElement().getArtifactURL(),\r
+                sdcConfig.getConsumerID(), iNotif.getDistributionID(), distributionStatus, errorMessage,\r
+                System.currentTimeMillis());\r
+            // Notify for all VF resources found\r
+            for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {\r
+                // Normally always 1 artifact in resource for Clamp as we\r
+                // specified\r
+                // only VF_METADATA type\r
+                this.sendSdcNotification(notificationType,\r
+                    blueprint.getValue().getResourceAttached().getArtifacts().get(0).getArtifactURL(),\r
+                    sdcConfig.getConsumerID(), iNotif.getDistributionID(), distributionStatus, errorMessage,\r
+                    System.currentTimeMillis());\r
+            }\r
+        } else {\r
+            this.sendSdcNotification(notificationType, null, sdcConfig.getConsumerID(), iNotif.getDistributionID(),\r
+                distributionStatus, errorMessage, System.currentTimeMillis());\r
+        }\r
+    }\r
+\r
     /**\r
      * This method processes the notification received from Sdc.\r
-     * \r
+     *\r
      * @param iNotif\r
-     *            The INotificationData\r
+     *        The INotificationData\r
      */\r
     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
+            Thread.sleep(ThreadLocalRandom.current().nextInt(1, 10) * 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
+                refProp.getStringValue(CONFIG_SDC_FOLDER));\r
+            csar.save(downloadTheArtifact(csar.getArtifactElement()));\r
             if (csarInstaller.isCsarAlreadyDeployed(csar)) {\r
-                csar.save(downloadTheArtifact(csar.getArtifactElement()));\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.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),\r
-                        sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_OK, null,\r
-                        System.currentTimeMillis());\r
+                sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DOWNLOAD,\r
+                    DistributionStatusEnum.ALREADY_DOWNLOADED, null);\r
+                sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,\r
+                    DistributionStatusEnum.ALREADY_DEPLOYED, null);\r
             } else {\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
+                sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DOWNLOAD,\r
+                    DistributionStatusEnum.DOWNLOAD_OK, null);\r
+                csarInstaller.installTheCsar(csar);\r
+                sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,\r
+                    DistributionStatusEnum.DEPLOY_OK, null);\r
             }\r
-        } catch (SdcArtifactInstallerException e) {\r
+        } catch (SdcArtifactInstallerException | SdcToscaParserException e) {\r
             logger.error("SdcArtifactInstallerException 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 (SdcDownloadException e) {\r
+            sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,\r
+                DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());\r
+        } catch (SdcDownloadException | CsarHandlerException e) {\r
             logger.error("SdcDownloadException exception caught during the notification processing", e);\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.sendSdcNotification(NotificationType.DOWNLOAD, null, sdcConfig.getConsumerID(),\r
-                    iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR, e.getMessage(),\r
-                    System.currentTimeMillis());\r
-        } catch (SdcToscaParserException e) {\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
+            sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DOWNLOAD,\r
+                DistributionStatusEnum.DOWNLOAD_ERROR, e.getMessage());\r
+        } catch (PolicyModelException e) {\r
+            logger.error("PolicyModelException exception caught during the notification processing", e);\r
+            sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,\r
+                DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());\r
+        } catch (InterruptedException e) {\r
+            logger.error("Interrupt exception caught during the notification processing", e);\r
+            sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,\r
+                DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());\r
+            Thread.currentThread().interrupt();\r
         } catch (RuntimeException e) {\r
             logger.error("Unexpected exception caught during the notification processing", e);\r
-        } finally {\r
+            sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,\r
+                DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());\r
+        }  finally {\r
             this.changeControllerStatus(SdcSingleControllerStatus.IDLE);\r
         }\r
     }\r
@@ -253,8 +281,8 @@ public class SdcSingleController {
     }\r
 \r
     private IDistributionClientDownloadResult downloadTheArtifact(IArtifactInfo artifact) throws SdcDownloadException {\r
-        logger.debug("Trying to download the artifact : " + artifact.getArtifactURL() + " UUID: "\r
-                + artifact.getArtifactUUID());\r
+        logger.info(\r
+            "Trying to download the artifact : " + artifact.getArtifactURL() + " UUID: " + artifact.getArtifactUUID());\r
         IDistributionClientDownloadResult downloadResult;\r
         try {\r
             downloadResult = distributionClient.download(artifact);\r
@@ -267,20 +295,20 @@ public class SdcSingleController {
         }\r
         if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult())) {\r
             logger.info("Successfully downloaded the artifact " + artifact.getArtifactURL() + " UUID "\r
-                    + artifact.getArtifactUUID() + "Size of payload " + downloadResult.getArtifactPayload().length);\r
+                + artifact.getArtifactUUID() + "Size of payload " + downloadResult.getArtifactPayload().length);\r
         } else {\r
-            throw new SdcDownloadException("Artifact " + artifact.getArtifactName()\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
+            throw new SdcDownloadException(\r
+                "Artifact " + artifact.getArtifactName() + " could not be downloaded from SDC URL "\r
+                    + artifact.getArtifactURL() + " UUID " + artifact.getArtifactUUID() + ")" + System.lineSeparator()\r
+                    + "Error message is " + downloadResult.getDistributionMessageResult() + System.lineSeparator());\r
         }\r
         return downloadResult;\r
     }\r
 \r
     private void sendSdcNotification(NotificationType notificationType, String artifactURL, String consumerID,\r
-            String distributionID, DistributionStatusEnum status, String errorReason, long timestamp) {\r
+        String distributionID, DistributionStatusEnum status, String errorReason, long timestamp) {\r
         String event = "Sending " + notificationType.name() + "(" + status.name() + ")"\r
-                + " notification to SDC for artifact:" + artifactURL;\r
+            + " notification to SDC for artifact:" + artifactURL;\r
         if (errorReason != null) {\r
             event = event + "(" + errorReason + ")";\r
         }\r
@@ -288,18 +316,18 @@ public class SdcSingleController {
         String action = "";\r
         try {\r
             IDistributionStatusMessage message = new DistributionStatusMessage(artifactURL, consumerID, distributionID,\r
-                    status, timestamp);\r
+                status, timestamp);\r
             switch (notificationType) {\r
-                case DOWNLOAD:\r
-                    this.sendDownloadStatus(message, errorReason);\r
-                    action = "sendDownloadStatus";\r
-                    break;\r
-                case DEPLOY:\r
-                    this.sendDeploymentStatus(message, errorReason);\r
-                    action = "sendDeploymentdStatus";\r
-                    break;\r
-                default:\r
-                    break;\r
+            case DOWNLOAD:\r
+                this.sendDownloadStatus(message, errorReason);\r
+                action = "sendDownloadStatus";\r
+                break;\r
+            case DEPLOY:\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