Add support for loading VNF Catalog XML files
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / notification / ArtifactDownloadManager.java
index bdd101e..58bb074 100644 (file)
@@ -1,22 +1,22 @@
 /**
- * ============LICENSE_START==========================================
+ * ============LICENSE_START=======================================================
  * org.onap.aai
- * ===================================================================
+ * ================================================================================
  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017-2018 Amdocs
- * ===================================================================
+ * Copyright © 2017-2018 European Software Marketing Ltd.
+ * ================================================================================
  * 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
  *
- *        http://www.apache.org/licenses/LICENSE-2.0
+ *       http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- * ============LICENSE_END============================================
+ * ============LICENSE_END=========================================================
  */
 package org.onap.aai.modelloader.notification;
 
@@ -26,7 +26,6 @@ import java.util.Base64;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
-
 import org.onap.aai.babel.service.data.BabelArtifact;
 import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType;
 import org.onap.aai.cl.api.Logger;
@@ -39,14 +38,17 @@ import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
 import org.onap.aai.modelloader.entity.model.IModelParser;
 import org.onap.aai.modelloader.entity.model.NamedQueryArtifactParser;
 import org.onap.aai.modelloader.extraction.InvalidArchiveException;
+import org.onap.aai.modelloader.extraction.VnfCatalogExtractor;
 import org.onap.aai.modelloader.restclient.BabelServiceClient;
+import org.onap.aai.modelloader.restclient.BabelServiceClientException;
+import org.onap.aai.modelloader.service.BabelServiceClientFactory;
 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
-import org.openecomp.sdc.api.IDistributionClient;
-import org.openecomp.sdc.api.notification.IArtifactInfo;
-import org.openecomp.sdc.api.notification.INotificationData;
-import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
-import org.openecomp.sdc.utils.ArtifactTypeEnum;
-import org.openecomp.sdc.utils.DistributionActionResultEnum;
+import org.onap.sdc.api.IDistributionClient;
+import org.onap.sdc.api.notification.IArtifactInfo;
+import org.onap.sdc.api.notification.INotificationData;
+import org.onap.sdc.api.results.IDistributionClientDownloadResult;
+import org.onap.sdc.utils.ArtifactTypeEnum;
+import org.onap.sdc.utils.DistributionActionResultEnum;
 
 /**
  * This class is responsible for downloading the artifacts from the ASDC.
@@ -66,10 +68,14 @@ public class ArtifactDownloadManager {
     private NotificationPublisher notificationPublisher;
     private BabelArtifactConverter babelArtifactConverter;
     private ModelLoaderConfig config;
+    private BabelServiceClientFactory clientFactory;
+    private VnfCatalogExtractor vnfCatalogExtractor;
 
-    public ArtifactDownloadManager(IDistributionClient client, ModelLoaderConfig config) {
+    public ArtifactDownloadManager(IDistributionClient client, ModelLoaderConfig config,
+            BabelServiceClientFactory clientFactory) {
         this.client = client;
         this.config = config;
+        this.clientFactory = clientFactory;
     }
 
     /**
@@ -144,13 +150,33 @@ public class ArtifactDownloadManager {
     }
 
     public void processToscaArtifacts(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts, byte[] payload,
+            IArtifactInfo artifactInfo, String distributionId, String serviceVersion)
+            throws ProcessToscaArtifactsException, InvalidArchiveException {
+        // Get translated artifacts from Babel Service
+        invokeBabelService(modelArtifacts, catalogArtifacts, payload, artifactInfo, distributionId, serviceVersion);
+
+        // Get VNF Catalog artifacts directly from CSAR
+        List<Artifact> csarCatalogArtifacts = getVnfCatalogExtractor().extract(payload, artifactInfo.getArtifactName());
+
+        // Throw an error if VNF Catalog data is present in the Babel payload and directly in the CSAR
+        if (!catalogArtifacts.isEmpty() && !csarCatalogArtifacts.isEmpty()) {
+            logger.error(ModelLoaderMsgs.DUPLICATE_VNFC_DATA_ERROR, artifactInfo.getArtifactName());
+            throw new InvalidArchiveException("CSAR: " + artifactInfo.getArtifactName()
+                    + " contains VNF Catalog data in the format of both TOSCA and XML files. Only one format can be used for each CSAR file.");
+        } else if (!csarCatalogArtifacts.isEmpty()) {
+            catalogArtifacts.addAll(csarCatalogArtifacts);
+        }
+    }
+
+    public void invokeBabelService(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts, byte[] payload,
             IArtifactInfo artifactInfo, String distributionId, String serviceVersion)
             throws ProcessToscaArtifactsException {
         try {
             BabelServiceClient babelClient = createBabelServiceClient(artifactInfo, serviceVersion);
 
-            logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT,
-                    "Posting artifact: " + artifactInfo.getArtifactName() + ", version: " + serviceVersion);
+            logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
+                    "Posting artifact: " + artifactInfo.getArtifactName() + ", service version: " + serviceVersion
+                            + ", artifact version: " + artifactInfo.getArtifactVersion());
 
             List<BabelArtifact> babelArtifacts =
                     babelClient.postArtifact(payload, artifactInfo.getArtifactName(), serviceVersion, distributionId);
@@ -193,13 +219,13 @@ public class ArtifactDownloadManager {
         }
     }
 
-    private BabelServiceClient createBabelServiceClient(IArtifactInfo artifact, String serviceVersion)
+    BabelServiceClient createBabelServiceClient(IArtifactInfo artifact, String serviceVersion)
             throws ProcessToscaArtifactsException {
         BabelServiceClient babelClient;
         try {
             logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Creating Babel client");
-            babelClient = new BabelServiceClient(config);
-        } catch (Exception e) {
+            babelClient = clientFactory.create(config);
+        } catch (BabelServiceClientException e) {
             logger.error(ModelLoaderMsgs.BABEL_REST_REQUEST_ERROR, e, "POST", config.getBabelBaseUrl(),
                     "Error posting artifact " + artifact.getArtifactName() + " " + serviceVersion + " to Babel: "
                             + e.getLocalizedMessage());
@@ -247,4 +273,12 @@ public class ArtifactDownloadManager {
 
         return babelArtifactConverter;
     }
+
+    private VnfCatalogExtractor getVnfCatalogExtractor() {
+        if (vnfCatalogExtractor == null) {
+            vnfCatalogExtractor = new VnfCatalogExtractor();
+        }
+
+        return vnfCatalogExtractor;
+    }
 }