[SDC-DIST] Upgrade asdc-controller to use client version 2.0.0
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / client / ASDCController.java
index f64adfd..8e3c5f4 100644 (file)
@@ -7,6 +7,8 @@
  * ================================================================================
  * Modifications Copyright (c) 2019 Samsung
  * ================================================================================
+ * Modifications Copyright (c) 2020 Nokia
+ * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -33,10 +35,11 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Paths;
 import java.util.List;
 import java.util.Optional;
-import org.onap.so.logger.LoggingAnchor;
+import org.onap.logging.filter.base.ErrorCode;
 import org.onap.logging.ref.slf4j.ONAPLogConstants;
 import org.onap.sdc.api.IDistributionClient;
 import org.onap.sdc.api.consumer.IDistributionStatusMessage;
@@ -47,6 +50,8 @@ import org.onap.sdc.api.notification.IResourceInstance;
 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
 import org.onap.sdc.api.results.IDistributionClientResult;
 import org.onap.sdc.impl.DistributionClientFactory;
+import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
+import org.onap.sdc.toscaparser.api.elements.Metadata;
 import org.onap.sdc.utils.DistributionActionResultEnum;
 import org.onap.sdc.utils.DistributionStatusEnum;
 import org.onap.so.asdc.activity.DeployActivitySpecs;
@@ -64,11 +69,12 @@ import org.onap.so.asdc.installer.heat.ToscaResourceInstaller;
 import org.onap.so.asdc.tenantIsolation.DistributionStatus;
 import org.onap.so.asdc.tenantIsolation.WatchdogDistribution;
 import org.onap.so.asdc.util.ASDCNotificationLogging;
+import org.onap.so.asdc.util.ZipParser;
 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
 import org.onap.so.db.request.data.repository.WatchdogDistributionStatusRepository;
-import org.onap.so.logger.ErrorCode;
+import org.onap.so.logger.LoggingAnchor;
 import org.onap.so.logger.MessageEnum;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -141,6 +147,10 @@ public class ASDCController {
         this.controllerName = controllerConfigName;
     }
 
+    public ASDCConfiguration getASDCConfiguration() {
+        return asdcConfig;
+    }
+
     public int getNbOfNotificationsOngoing() {
         return nbOfNotificationsOngoing;
     }
@@ -162,12 +172,7 @@ public class ASDCController {
                 break;
 
             case IDLE:
-                if (this.nbOfNotificationsOngoing > 1) {
-                    --this.nbOfNotificationsOngoing;
-                } else {
-                    this.nbOfNotificationsOngoing = 0;
-                    this.controllerStatus = newControllerStatus;
-                }
+                changeOnStatusIDLE(newControllerStatus);
 
                 break;
             default:
@@ -177,6 +182,15 @@ public class ASDCController {
         }
     }
 
+    private void changeOnStatusIDLE(ASDCControllerStatus newControllerStatus) {
+        if (this.nbOfNotificationsOngoing > 1) {
+            --this.nbOfNotificationsOngoing;
+        } else {
+            this.nbOfNotificationsOngoing = 0;
+            this.controllerStatus = newControllerStatus;
+        }
+    }
+
     public ASDCControllerStatus getControllerStatus() {
         return this.controllerStatus;
     }
@@ -296,13 +310,12 @@ public class ASDCController {
     protected void notifyErrorToAsdc(INotificationData iNotif, ToscaResourceStructure toscaResourceStructure,
             DistributionStatusEnum deployStatus, VfResourceStructure resourceStructure, String errorMessage) {
         // do csar lever first
-        this.sendCsarDeployNotification(iNotif, resourceStructure, toscaResourceStructure, deployStatus, errorMessage);
+        this.sendCsarDeployNotification(resourceStructure, toscaResourceStructure, deployStatus, errorMessage);
         // at resource level
         for (IResourceInstance resource : iNotif.getResources()) {
             resourceStructure = new VfResourceStructure(iNotif, resource);
             errorMessage = String.format("Resource with UUID: %s already exists", resource.getResourceUUID());
-            this.sendCsarDeployNotification(iNotif, resourceStructure, toscaResourceStructure, deployStatus,
-                    errorMessage);
+            this.sendCsarDeployNotification(resourceStructure, toscaResourceStructure, deployStatus, errorMessage);
         }
     }
 
@@ -363,8 +376,7 @@ public class ASDCController {
 
         if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult())) {
             logger.info(LoggingAnchor.FOUR, MessageEnum.ASDC_ARTIFACT_DOWNLOAD_SUC.toString(),
-                    artifact.getArtifactURL(), artifact.getArtifactUUID(),
-                    String.valueOf(downloadResult.getArtifactPayload().length));
+                    artifact.getArtifactURL(), artifact.getArtifactUUID(), downloadResult.getArtifactPayload().length);
 
         } else {
             logger.error(LoggingAnchor.SEVEN, MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL.toString(),
@@ -447,7 +459,7 @@ public class ASDCController {
         }
     }
 
-    protected void sendCsarDeployNotification(INotificationData iNotif, ResourceStructure resourceStructure,
+    protected void sendCsarDeployNotification(ResourceStructure resourceStructure,
             ToscaResourceStructure toscaResourceStructure, DistributionStatusEnum statusEnum, String errorReason) {
 
         IArtifactInfo csarArtifact = toscaResourceStructure.getToscaArtifact();
@@ -471,8 +483,8 @@ public class ASDCController {
         } catch (ArtifactInstallerException e) {
             logger.info(LoggingAnchor.SIX, MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL.toString(),
                     resourceStructure.getResourceInstance().getResourceName(),
-                    resourceStructure.getResourceInstance().getResourceUUID(),
-                    String.valueOf(resourceStructure.getNumberOfResources()), "ASDC", "deployResourceStructure");
+                    resourceStructure.getResourceInstance().getResourceUUID(), resourceStructure.getNumberOfResources(),
+                    "ASDC", "deployResourceStructure");
             sendDeployNotificationsForResource(resourceStructure, DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
             throw e;
         }
@@ -480,8 +492,8 @@ public class ASDCController {
         if (resourceStructure.isDeployedSuccessfully() || toscaResourceStructure.isDeployedSuccessfully()) {
             logger.info(LoggingAnchor.SIX, MessageEnum.ASDC_ARTIFACT_DEPLOY_SUC.toString(),
                     resourceStructure.getResourceInstance().getResourceName(),
-                    resourceStructure.getResourceInstance().getResourceUUID(),
-                    String.valueOf(resourceStructure.getNumberOfResources()), "ASDC", "deployResourceStructure");
+                    resourceStructure.getResourceInstance().getResourceUUID(), resourceStructure.getNumberOfResources(),
+                    "ASDC", "deployResourceStructure");
             sendDeployNotificationsForResource(resourceStructure, DistributionStatusEnum.DEPLOY_OK, null);
         }
 
@@ -509,26 +521,10 @@ public class ASDCController {
         try {
             IDistributionStatusMessage message =
                     new DistributionStatusMessage(artifactURL, consumerID, distributionID, status, timestamp);
-
-            switch (notificationType) {
-                case DOWNLOAD:
-                    if (errorReason != null) {
-                        this.distributionClient.sendDownloadStatus(message, errorReason);
-                    } else {
-                        this.distributionClient.sendDownloadStatus(message);
-                    }
-
-                    break;
-                case DEPLOY:
-                    if (errorReason != null) {
-                        this.distributionClient.sendDeploymentStatus(message, errorReason);
-                    } else {
-                        this.distributionClient.sendDeploymentStatus(message);
-                    }
-
-                    break;
-                default:
-                    break;
+            if (errorReason != null) {
+                sendNotificationWithMessageAndErrorReason(notificationType, errorReason, message);
+            } else {
+                sendNotificationWithMessage(notificationType, message);
             }
         } catch (RuntimeException e) {
             logger.warn(LoggingAnchor.FIVE, MessageEnum.ASDC_SEND_NOTIF_ASDC_EXEC.toString(), "ASDC",
@@ -537,6 +533,33 @@ public class ASDCController {
         }
     }
 
+    private void sendNotificationWithMessage(NotificationType notificationType, IDistributionStatusMessage message) {
+        switch (notificationType) {
+            case DOWNLOAD:
+                this.distributionClient.sendDownloadStatus(message);
+                break;
+            case DEPLOY:
+                this.distributionClient.sendDeploymentStatus(message);
+                break;
+            default:
+                break;
+        }
+    }
+
+    private void sendNotificationWithMessageAndErrorReason(NotificationType notificationType, String errorReason,
+            IDistributionStatusMessage message) {
+        switch (notificationType) {
+            case DOWNLOAD:
+                this.distributionClient.sendDownloadStatus(message, errorReason);
+                break;
+            case DEPLOY:
+                this.distributionClient.sendDeploymentStatus(message, errorReason);
+                break;
+            default:
+                break;
+        }
+    }
+
     protected void sendFinalDistributionStatus(String distributionID, DistributionStatusEnum status,
             String errorReason) {
 
@@ -587,8 +610,8 @@ public class ASDCController {
         for (IResourceInstance resource : iNotif.getResources()) {
             noOfArtifacts += resource.getArtifacts().size();
         }
-        logger.info(LoggingAnchor.FOUR, MessageEnum.ASDC_RECEIVE_CALLBACK_NOTIF.toString(),
-                String.valueOf(noOfArtifacts), iNotif.getServiceUUID(), "ASDC");
+        logger.info(LoggingAnchor.FOUR, MessageEnum.ASDC_RECEIVE_CALLBACK_NOTIF.toString(), noOfArtifacts,
+                iNotif.getServiceUUID(), "ASDC");
         try {
 
             if (iNotif.getDistributionID() != null && !iNotif.getDistributionID().isEmpty()) {
@@ -733,6 +756,13 @@ public class ASDCController {
             if (isCsarAlreadyDeployed(iNotif, toscaResourceStructure)) {
                 return;
             }
+            // process NsstResource
+            this.processNsstNotification(iNotif, toscaResourceStructure);
+
+            if (iNotif.getResources().isEmpty()) {
+                logger.error("Service Model contains no resources.");
+                return;
+            }
 
             for (IResourceInstance resource : iNotif.getResources()) {
 
@@ -741,21 +771,11 @@ public class ASDCController {
 
                 logger.info("Processing Resource Type: {}, Model UUID: {}", resourceType, resource.getResourceUUID());
 
-                if ("VF".equals(resourceType)) {
-                    resourceStructure = new VfResourceStructure(iNotif, resource);
-                } else if ("PNF".equals(resourceType)) {
-                    resourceStructure = new PnfResourceStructure(iNotif, resource);
-                } else {
-                    // There are cases where the Service has no VF resources, those are handled here
-                    logger.info("No resources found for Service: {}", iNotif.getServiceUUID());
-                    resourceStructure = new VfResourceStructure(iNotif, new ResourceInstance());
-                    resourceStructure.setResourceType(ResourceType.OTHER);
-                }
+                resourceStructure = getResourceStructure(iNotif, resource, resourceType);
 
                 try {
 
                     if (!this.checkResourceAlreadyDeployed(resourceStructure, serviceDeployed)) {
-
                         logger.debug("Processing Resource Type: " + resourceType + " and Model UUID: "
                                 + resourceStructure.getResourceInstance().getResourceUUID());
 
@@ -765,25 +785,27 @@ public class ASDCController {
                             for (IArtifactInfo artifact : resource.getArtifacts()) {
                                 IDistributionClientDownloadResult resultArtifact =
                                         this.downloadTheArtifact(artifact, iNotif.getDistributionID());
-                                if (resultArtifact != null) {
-
-                                    if (ASDCConfiguration.VF_MODULES_METADATA.equals(artifact.getArtifactType())) {
-                                        logger.debug("VF_MODULE_ARTIFACT: "
-                                                + new String(resultArtifact.getArtifactPayload(), "UTF-8"));
-                                        logger.debug(ASDCNotificationLogging
-                                                .dumpVfModuleMetaDataList(((VfResourceStructure) resourceStructure)
-                                                        .decodeVfModuleArtifact(resultArtifact.getArtifactPayload())));
-                                    }
-                                    if (!ASDCConfiguration.WORKFLOW.equals(artifact.getArtifactType())) {
-                                        resourceStructure.addArtifactToStructure(distributionClient, artifact,
-                                                resultArtifact);
-                                    } else {
-                                        writeArtifactToFile(artifact, resultArtifact);
-                                        logger.debug(
-                                                "Adding workflow artifact to structure: " + artifact.getArtifactName());
-                                        resourceStructure.addWorkflowArtifactToStructure(artifact, resultArtifact);
-                                    }
+                                if (resultArtifact == null) {
+                                    continue;
+                                }
+
+                                if (ASDCConfiguration.VF_MODULES_METADATA.equals(artifact.getArtifactType())) {
+                                    logger.debug("VF_MODULE_ARTIFACT: "
+                                            + new String(resultArtifact.getArtifactPayload(), StandardCharsets.UTF_8));
+                                    logger.debug(ASDCNotificationLogging
+                                            .dumpVfModuleMetaDataList(((VfResourceStructure) resourceStructure)
+                                                    .decodeVfModuleArtifact(resultArtifact.getArtifactPayload())));
+                                }
+                                if (!ASDCConfiguration.WORKFLOW.equals(artifact.getArtifactType())) {
+                                    resourceStructure.addArtifactToStructure(distributionClient, artifact,
+                                            resultArtifact);
+                                } else {
+                                    writeArtifactToFile(artifact, resultArtifact);
+                                    logger.debug(
+                                            "Adding workflow artifact to structure: " + artifact.getArtifactName());
+                                    resourceStructure.addWorkflowArtifactToStructure(artifact, resultArtifact);
                                 }
+
                             }
 
                             // Deploy VF resource and artifacts
@@ -817,8 +839,7 @@ public class ASDCController {
                 }
             }
 
-            this.sendCsarDeployNotification(iNotif, resourceStructure, toscaResourceStructure, deployStatus,
-                    errorMessage);
+            this.sendCsarDeployNotification(resourceStructure, toscaResourceStructure, deployStatus, errorMessage);
 
         } catch (ASDCDownloadException | UnsupportedEncodingException e) {
             logger.error(LoggingAnchor.SIX, MessageEnum.ASDC_GENERAL_EXCEPTION_ARG.toString(),
@@ -827,6 +848,20 @@ public class ASDCController {
         }
     }
 
+    private ResourceStructure getResourceStructure(INotificationData iNotif, IResourceInstance resource,
+            String resourceType) {
+        if ("VF".equals(resourceType)) {
+            return new VfResourceStructure(iNotif, resource);
+        }
+        if ("PNF".equals(resourceType)) {
+            return new PnfResourceStructure(iNotif, resource);
+        }
+        logger.info("No resources found for Service: {}", iNotif.getServiceUUID());
+        ResourceStructure resourceStructure = new VfResourceStructure(iNotif, new ResourceInstance());
+        resourceStructure.setResourceType(ResourceType.OTHER);
+        return resourceStructure;
+    }
+
     private String getMsoConfigPath() {
         String msoConfigPath = System.getProperty("mso.config.path");
         if (msoConfigPath == null) {
@@ -893,6 +928,23 @@ public class ASDCController {
                             "processCsarServiceArtifacts", ErrorCode.BusinessProcessError.getValue(),
                             "Exception in processCsarServiceArtifacts", e);
                 }
+            } else if (artifact.getArtifactType().equals(ASDCConfiguration.OTHER)) {
+                try {
+                    IDistributionClientDownloadResult resultArtifact =
+                            this.downloadTheArtifact(artifact, iNotif.getDistributionID());
+
+                    writeArtifactToFile(artifact, resultArtifact);
+
+                    toscaResourceStructure.setToscaArtifact(artifact);
+
+                    toscaResourceStructure.setServiceVersion(iNotif.getServiceVersion());
+
+                } catch (ASDCDownloadException e) {
+                    logger.error(LoggingAnchor.SIX, MessageEnum.ASDC_GENERAL_EXCEPTION_ARG.toString(),
+                            "Exception caught during processCsarServiceArtifacts", "ASDC",
+                            "processCsarServiceArtifacts", ErrorCode.BusinessProcessError.getValue(),
+                            "Exception in processCsarServiceArtifacts", e);
+                }
             }
 
 
@@ -900,13 +952,12 @@ public class ASDCController {
     }
 
 
-
     /**
      * @return the address of the ASDC we are connected to.
      */
     public String getAddress() {
         if (asdcConfig != null) {
-            return asdcConfig.getAsdcAddress();
+            return asdcConfig.getSdcAddress();
         }
         return UNKNOWN;
     }
@@ -920,4 +971,44 @@ public class ASDCController {
         }
         return UNKNOWN;
     }
+
+    private void processNsstNotification(INotificationData iNotif, ToscaResourceStructure toscaResourceStructure) {
+        Metadata serviceMetadata = toscaResourceStructure.getServiceMetadata();
+        try {
+            String category = serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CATEGORY);
+            boolean isNeedInital = (category.contains("NSST") || category.equalsIgnoreCase("TN Network Requirement"))
+                    && iNotif.getResources().isEmpty();
+            if (isNeedInital) {
+                String artifactContent = null;
+                List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();
+                Optional<IArtifactInfo> artifactOpt = serviceArtifacts.stream()
+                        .filter(e -> e.getArtifactType().equalsIgnoreCase("WORKFLOW")).findFirst();
+                if (artifactOpt.isPresent()) {
+                    IArtifactInfo artifactInfo = artifactOpt.get();
+                    logger.debug("Ready to parse this serviceArtifactUUID:  " + artifactInfo.getArtifactUUID());
+                    String filePath = Paths.get(getMsoConfigPath(), "ASDC", artifactInfo.getArtifactVersion(),
+                            artifactInfo.getArtifactName()).normalize().toString();
+                    ZipParser zipParserInstance = ZipParser.getInstance();
+                    artifactContent = zipParserInstance.parseJsonForZip(filePath);
+                    logger.debug(
+                            "serviceArtifact parsing success! serviceArtifactUUID: " + artifactInfo.getArtifactUUID());
+
+                    ResourceStructure resourceStructure = new VfResourceStructure(iNotif, new ResourceInstance());
+                    resourceStructure.setResourceType(ResourceType.OTHER);
+                    toscaInstaller.installNsstService(toscaResourceStructure, (VfResourceStructure) resourceStructure,
+                            artifactContent);
+                } else {
+                    logger.debug("serviceArtifact is null");
+                    toscaInstaller.installNsstService(toscaResourceStructure, null, null);
+                }
+
+            }
+
+        } catch (IOException e) {
+            logger.error("serviceArtifact parse failure for service uuid:  "
+                    + serviceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_CATEGORY));
+        } catch (Exception e) {
+            logger.error("error NSST process resource failure ", e);
+        }
+    }
 }