Return List<Artifact> in ArtifactDownloadManager
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / notification / EventCallback.java
index 2baa217..6993948 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * ============LICENSE_START=======================================================
+ * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
@@ -22,32 +22,37 @@ package org.onap.aai.modelloader.notification;
 
 import java.util.ArrayList;
 import java.util.List;
+
 import org.onap.aai.cl.api.Logger;
 import org.onap.aai.cl.eelf.LoggerFactory;
 import org.onap.aai.cl.mdc.MdcContext;
-import org.onap.aai.modelloader.config.ModelLoaderConfig;
 import org.onap.aai.modelloader.entity.Artifact;
+import org.onap.aai.modelloader.entity.ArtifactType;
 import org.onap.aai.modelloader.extraction.ArtifactInfoExtractor;
-import org.onap.aai.modelloader.restclient.BabelServiceClientFactory;
+import org.onap.aai.modelloader.service.ArtifactDeploymentManager;
 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
 import org.onap.sdc.api.IDistributionClient;
 import org.onap.sdc.api.consumer.INotificationCallback;
 import org.onap.sdc.api.notification.IArtifactInfo;
 import org.onap.sdc.api.notification.INotificationData;
 import org.slf4j.MDC;
+import org.springframework.stereotype.Component;
 
+@Component
 public class EventCallback implements INotificationCallback {
     private static Logger logger = LoggerFactory.getInstance().getLogger(EventCallback.class.getName());
     private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(EventCallback.class.getName());
 
-    private ArtifactDeploymentManager artifactDeploymentManager;
-    private ArtifactDownloadManager artifactDownloadManager;
-    private IDistributionClient client;
-    private ModelLoaderConfig config;
+    private final ArtifactDeploymentManager artifactDeploymentManager;
+    private final ArtifactDownloadManager artifactDownloadManager;
+    private final NotificationPublisher notificationPublisher;
+    private final IDistributionClient client;
 
-    public EventCallback(IDistributionClient client, ModelLoaderConfig config) {
+    public EventCallback(IDistributionClient client, ArtifactDeploymentManager artifactDeploymentManager, ArtifactDownloadManager artifactDownloadManager, NotificationPublisher notificationPublisher) {
+        this.artifactDeploymentManager = artifactDeploymentManager;
+        this.artifactDownloadManager = artifactDownloadManager;
+        this.notificationPublisher = notificationPublisher;
         this.client = client;
-        this.config = config;
     }
 
     @Override
@@ -56,35 +61,51 @@ public class EventCallback implements INotificationCallback {
         logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received distribution " + data.getDistributionID());
 
         List<IArtifactInfo> artifacts = new ArtifactInfoExtractor().extract(data);
+        boolean success = true;
+        List<Artifact> downloadedArtifacts = new ArrayList<>();
+        try {
+            downloadedArtifacts =
+                    artifactDownloadManager.downloadArtifacts(data, artifacts);
+        } catch (Exception e) {
+            success = false;
+        }
+
         List<Artifact> catalogArtifacts = new ArrayList<>();
         List<Artifact> modelArtifacts = new ArrayList<>();
-
-        boolean success =
-                getArtifactDownloadManager().downloadArtifacts(data, artifacts, modelArtifacts, catalogArtifacts);
+        if(downloadedArtifacts != null) {
+            for(Artifact artifact : downloadedArtifacts) {
+                if(artifact.getType() == ArtifactType.VNF_CATALOG || artifact.getType() == ArtifactType.VNF_CATALOG_XML) {
+                    catalogArtifacts.add(artifact);
+                } else {
+                    modelArtifacts.add(artifact);
+                }
+            }
+        }
 
         if (success) {
-            success = getArtifactDeploymentManager().deploy(data, artifacts, modelArtifacts, catalogArtifacts);
+            success = artifactDeploymentManager.deploy(data.getDistributionID(), modelArtifacts, catalogArtifacts);
         }
 
         String statusString = success ? "SUCCESS" : "FAILURE";
         auditLogger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
                 "Processed distribution " + data.getDistributionID() + "  (" + statusString + ")");
-        MDC.clear();
-    }
 
-    private ArtifactDeploymentManager getArtifactDeploymentManager() {
-        if (artifactDeploymentManager == null) {
-            artifactDeploymentManager = new ArtifactDeploymentManager(client, config);
-        }
+        publishNotifications(data, "TOSCA_CSAR", artifacts, success);
 
-        return artifactDeploymentManager;
+        MDC.clear();
     }
 
-    private ArtifactDownloadManager getArtifactDownloadManager() {
-        if (artifactDownloadManager == null) {
-            artifactDownloadManager = new ArtifactDownloadManager(client, config, new BabelServiceClientFactory());
-        }
 
-        return artifactDownloadManager;
+    private void publishNotifications(INotificationData data, String filterType, List<IArtifactInfo> artifacts,
+            boolean deploymentSuccess) {
+        if (deploymentSuccess) {
+            artifacts.stream().filter(a -> filterType.equalsIgnoreCase(a.getArtifactType()))
+                    .forEach(a -> notificationPublisher.publishDeploySuccess(client, data, a));
+            notificationPublisher.publishComponentSuccess(client, data);
+        } else {
+            artifacts.stream().filter(a -> filterType.equalsIgnoreCase(a.getArtifactType()))
+                    .forEach(a -> notificationPublisher.publishDeployFailure(client, data, a));
+            notificationPublisher.publishComponentFailure(client, data, "deploy failure");
+        }
     }
 }