MainServiceTemplate multiple location support 08/70808/16
authorshaheen_vz <shaheen.shaik@verizon.com>
Fri, 19 Oct 2018 12:18:18 +0000 (17:48 +0530)
committerOren Kleks <orenkle@amdocs.com>
Tue, 18 Dec 2018 08:43:12 +0000 (08:43 +0000)
Issue-ID: SDC-1744
Signed-off-by: Shaheen Shaik<shaheen.shaik@verizon.com>:
Change-Id: I77e4ede6b2177ba66a4de8da824d9c7fd86f5705

openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/CSARConstants.java
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/OnboardingToscaMetadata.java [new file with mode: 0644]
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/upload/csar/MetadataParsingTest.java [new file with mode: 0644]
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/metadata/Invalidtosca.meta [new file with mode: 0644]
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/metadata/Validtosca.meta [new file with mode: 0644]
openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java

index 182ac04..4047937 100644 (file)
@@ -1,3 +1,23 @@
+/*
+
+ * Copyright (c) 2018 AT&T Intellectual Property.
+
+  * Modifications Copyright (c) 2018 Verizon Property.
+
+ * 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
+ * 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.
+
+ */
+
 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
 
 import org.apache.commons.lang3.tuple.Pair;
@@ -12,6 +32,7 @@ import org.openecomp.sdc.datatypes.error.ErrorMessage;
 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.OnboardingManifest;
+import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.OnboardingToscaMetadata;
 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
 
@@ -55,10 +76,34 @@ public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateH
   private void validateContent(UploadFileResponse uploadFileResponse, FileContentHandler contentMap,
                                List<String> folderList) {
     validateManifest(uploadFileResponse, contentMap);
-    validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
+    validateMetadata(uploadFileResponse, contentMap);
     validateNoExtraFiles(uploadFileResponse, contentMap);
     validateFolders(uploadFileResponse, folderList);
   }
+  
+  private void validateMetadata(UploadFileResponse uploadFileResponse,
+                                FileContentHandler contentMap){
+    if (!validateTOSCAYamlFileInRootExist(contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME)) {
+      try (InputStream metaFileContent = contentMap.getFileContent(TOSCA_META_PATH_FILE_NAME)) {
+
+        OnboardingToscaMetadata onboardingToscaMetadata = new OnboardingToscaMetadata(metaFileContent);
+        String entryDefinitionsPath = onboardingToscaMetadata.getEntryDefinitionsPath();
+        if (entryDefinitionsPath != null) {
+        validateFileExist(uploadFileResponse, contentMap, entryDefinitionsPath);
+        } else {
+        uploadFileResponse.addStructureError(
+            SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR,
+                Messages.METADATA_NO_ENTRY_DEFINITIONS.getErrorMessage()));
+        }
+      } catch (IOException exception) {
+        uploadFileResponse.addStructureError(
+            SdcCommon.UPLOAD_FILE,
+            new ErrorMessage(ErrorLevel.ERROR, Messages.FAILED_TO_VALIDATE_METADATA.getErrorMessage()));
+      }
+    } else {
+    validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
+    }  
+  }
 
   private void validateManifest(UploadFileResponse uploadFileResponse,
                                 FileContentHandler contentMap) {
@@ -115,6 +160,10 @@ public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateH
   private boolean filterFolders(String fileName) {
     return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith);
   }
+  
+  private boolean validateTOSCAYamlFileInRootExist(FileContentHandler contentMap, String fileName) {
+    return contentMap.containsFile(fileName);
+  }
 
   private boolean validateFileExist(UploadFileResponse uploadFileResponse,
                                     FileContentHandler contentMap, String fileName) {
index 8bb6798..ee81527 100644 (file)
@@ -26,6 +26,8 @@ public class CSARConstants {
 
     public static final String MAIN_SERVICE_TEMPLATE_MF_FILE_NAME = "MainServiceTemplate.mf";
     public static final String MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME = "MainServiceTemplate.yaml";
+    public static final String TOSCA_META_PATH_FILE_NAME="TOSCA-Metadata/TOSCA.meta";
+    public static final String TOSCA_META_ENTRY_DEFINITIONS="Entry-Definitions";
     public static final ImmutableSet<String> ELIGIBLE_FILES =
             of(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME,MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME);
 
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/OnboardingToscaMetadata.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/OnboardingToscaMetadata.java
new file mode 100644 (file)
index 0000000..037e76a
--- /dev/null
@@ -0,0 +1,70 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ * 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=========================================================
+ */
+
+package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar;
+
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
+import java.io.InputStream;
+import java.io.IOException;
+import org.apache.commons.io.IOUtils;
+import java.util.List;
+
+import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
+import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.SEPERATOR_MF_ATTRIBUTE;
+
+public class OnboardingToscaMetadata {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(OnboardingToscaMetadata.class);
+  private String entryDefinitionsPath;
+
+  public OnboardingToscaMetadata(InputStream is) throws IOException {
+      parseToscaMetadataFile(is);
+  }
+
+  private void parseToscaMetadataFile(InputStream st) throws IOException {
+
+    try {
+      List<String> metadataLines = IOUtils.readLines(st,"utf-8");
+
+      for (String line : metadataLines) {
+        line = line.trim();
+        if (line.startsWith(TOSCA_META_ENTRY_DEFINITIONS + SEPERATOR_MF_ATTRIBUTE)) {
+
+          entryDefinitionsPath = line.replaceAll(TOSCA_META_ENTRY_DEFINITIONS + SEPERATOR_MF_ATTRIBUTE, "")
+              .trim();
+          break;
+
+        }
+      }
+
+    } catch (IOException e) {
+      LOGGER.error(e.getMessage(), e);
+      throw new IOException("Invalid TOSCA Metadata file", e);
+
+    }
+
+  }
+
+  public String getEntryDefinitionsPath() {
+    return entryDefinitionsPath;
+  }
+}
+
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/upload/csar/MetadataParsingTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/upload/csar/MetadataParsingTest.java
new file mode 100644 (file)
index 0000000..46a563a
--- /dev/null
@@ -0,0 +1,32 @@
+package org.openecomp.sdc.vendorsoftwareproduct.upload.csar;
+
+import org.junit.Test;
+import org.openecomp.sdc.common.errors.Messages;
+import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.OnboardingToscaMetadata;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.junit.Assert.assertEquals;
+
+public class MetadataParsingTest {
+
+  @Test
+  public void testNoEntryDefinitions() throws IOException {
+    try (InputStream is = getClass()
+        .getResourceAsStream("/vspmanager.csar/metadata/Invalidtosca.meta")) {
+      OnboardingToscaMetadata onboardingToscaMetadata = new OnboardingToscaMetadata(is);
+      assertEquals(onboardingToscaMetadata.getEntryDefinitionsPath(), null);
+    }
+  }
+
+  @Test
+  public void testValidMetadataFile() throws IOException {
+    try (InputStream is = getClass()
+        .getResourceAsStream("/vspmanager.csar/metadata/Validtosca.meta")) {
+      OnboardingToscaMetadata onboardingToscaMetadata = new OnboardingToscaMetadata(is);
+      assertEquals(onboardingToscaMetadata.getEntryDefinitionsPath(), "Definitions/MainServiceTemplate.yaml");
+    }
+
+  }
+}
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/metadata/Invalidtosca.meta b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/metadata/Invalidtosca.meta
new file mode 100644 (file)
index 0000000..e9f0802
--- /dev/null
@@ -0,0 +1,3 @@
+TOSCA-Meta-File-Version: 1.0
+CSAR-Version: 1.1
+Created-By: Feng yuanxing
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/metadata/Validtosca.meta b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/metadata/Validtosca.meta
new file mode 100644 (file)
index 0000000..1f39805
--- /dev/null
@@ -0,0 +1,4 @@
+TOSCA-Meta-File-Version: 1.0
+CSAR-Version: 1.1
+Created-By: Feng yuanxing
+Entry-Definitions: Definitions/MainServiceTemplate.yaml
index 650eb06..f1f8423 100644 (file)
@@ -33,6 +33,9 @@ public enum Messages {
   MANIFEST_NO_METADATA("Manifest must contain metadata"),
   MANIFEST_NO_SOURCES("Manifest must contain Source"),
   MANIFEST_PARSER_INTERNAL("Invalid manifest file"),
+  METADATA_PARSER_INTERNAL("Invalid Metadata file"),
+  METADATA_NO_ENTRY_DEFINITIONS("TOSCA.meta must contain Entry Definitions"),
+  FAILED_TO_VALIDATE_METADATA("Failed to validate metadata file"),
   FAILED_TO_TRANSLATE_ZIP_FILE("Failed to translate zip file"),
   ZIP_NOT_EXIST("Zip file doesn't exist"),