SDC changes for adding dummy base heat-new patch 40/112940/4
authorSithara Nambiar <sitharav.aredath@huawei.com>
Mon, 21 Sep 2020 15:18:56 +0000 (20:48 +0530)
committerChristophe Closset <christophe.closset@intl.att.com>
Tue, 22 Sep 2020 07:55:27 +0000 (07:55 +0000)
Issue-ID: REQ-341

Signed-off-by: Sithara Nambiar <sitharav.aredath@huawei.com>
Change-Id: I1fff1ea7615ed21c769df651e3f1951be802ff65

openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImpl.java
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/onboarding/OnboardingPackageProcessor.java
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessZipHandler.java
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/resources/DummyHeatUse.txt [new file with mode: 0644]
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/resources/base_template.env [new file with mode: 0644]
openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/resources/base_template.yaml [new file with mode: 0644]

index 7de39a4..052bd78 100644 (file)
@@ -24,8 +24,10 @@ import java.io.BufferedOutputStream;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.ByteBuffer;
 import java.nio.file.Path;
@@ -62,8 +64,13 @@ import org.openecomp.sdc.common.errors.CoreException;
 import org.openecomp.sdc.common.errors.ErrorCode;
 import org.openecomp.sdc.common.errors.ValidationErrorBuilder;
 import org.openecomp.sdc.common.utils.CommonUtil;
+import org.openecomp.sdc.common.utils.SdcCommon;
 import org.openecomp.sdc.datatypes.error.ErrorLevel;
 import org.openecomp.sdc.datatypes.error.ErrorMessage;
+import org.openecomp.sdc.heat.datatypes.manifest.FileData;
+import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
 import org.openecomp.sdc.tosca.csar.Manifest;
 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
 import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl;
@@ -134,7 +141,7 @@ import org.openecomp.sdc.versioning.VersioningUtil;
 import org.openecomp.sdc.versioning.dao.types.Version;
 
 public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductManager {
-
+    private static final Logger LOGGER = LoggerFactory.getLogger(VendorSoftwareProductManager.class);
     private VspMergeDao vspMergeDao;
     private OrchestrationTemplateDao orchestrationTemplateDao;
     private OrchestrationTemplateCandidateManager orchestrationTemplateCandidateManager;
@@ -670,6 +677,12 @@ public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductMa
                 OnboardingTypesEnum.getOnboardingTypesEnum(orchestrationTemplate.getFileSuffix()),
                 orchestrationTemplate.getContentData().array());
 
+        try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
+            addDummyHeatBase(zipFileManifest, fileContentMap);
+        } catch (Exception e) {
+            LOGGER.error("Invalid package content", e);
+        }
+
         if (CommonUtil.isFileOriginFromZip(orchestrationTemplate.getFileSuffix())) {
             ValidationManager validationManager = ValidationManagerUtil.initValidationManager(fileContentMap);
             validationErrors.putAll(validationManager.validate());
@@ -679,6 +692,26 @@ public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductMa
                        : validationErrors;
     }
 
+    private FileContentHandler addDummyHeatBase(InputStream zipFileManifest, FileContentHandler fileContentMap) {
+        ManifestContent manifestContent =
+                JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
+        for (FileData fileData : manifestContent.getData()) {
+            if ((fileData.getFile()).contains("dummy_ignore.yaml")) {
+                String filePath = new File("").getAbsolutePath();
+                File envFilePath = new File(filePath + "/base_template.env");
+                File baseFilePath = new File(filePath + "/base_template.yaml");
+                try (
+                        InputStream envStream = new FileInputStream(envFilePath);
+                        InputStream baseStream = new FileInputStream(baseFilePath);) {
+                    fileContentMap.addFile("base_template_dummy_ignore.env", envStream);
+                    fileContentMap.addFile("base_template_dummy_ignore.yaml", baseStream);
+                } catch (Exception e) {
+                    LOGGER.error("File not found error {}", e);
+                }
+            }
+        }
+        return fileContentMap;
+    }
     private QuestionnaireValidationResult validateQuestionnaire(String vspId, Version version,
             String onboardingMethod) {
         // The apis of CompositionEntityDataManager used here are stateful!
index 20d8e26..d1b64e2 100644 (file)
@@ -28,20 +28,32 @@ import static org.openecomp.sdc.common.errors.Messages.PACKAGE_PROCESS_INTERNAL_
 import static org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager.ALLOWED_CERTIFICATE_EXTENSIONS;
 import static org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager.ALLOWED_SIGNATURE_EXTENSIONS;
 
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
 import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.io.FilenameUtils;
 import org.openecomp.core.utilities.file.FileContentHandler;
+import org.openecomp.core.utilities.json.JsonUtil;
 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
+import org.openecomp.sdc.common.utils.SdcCommon;
 import org.openecomp.sdc.common.zip.exception.ZipException;
 import org.openecomp.sdc.common.utils.CommonUtil;
 import org.openecomp.sdc.datatypes.error.ErrorLevel;
 import org.openecomp.sdc.datatypes.error.ErrorMessage;
+import org.openecomp.sdc.heat.datatypes.manifest.FileData;
+import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
 import org.openecomp.sdc.logging.api.Logger;
 import org.openecomp.sdc.logging.api.LoggerFactory;
 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage;
@@ -52,6 +64,7 @@ public class OnboardingPackageProcessor {
     private static final Logger LOGGER = LoggerFactory.getLogger(OnboardingPackageProcessor.class);
     private static final String CSAR_EXTENSION = "csar";
     private static final String ZIP_EXTENSION = "zip";
+    private static  boolean helmBase = false;
 
     private final String packageFileName;
     private final byte[] packageFileContent;
@@ -96,6 +109,7 @@ public class OnboardingPackageProcessor {
                     ByteBuffer.wrap(packageFileContent), new OnboardingPackageContentHandler(onboardPackageContentHandler));
                 return new OnboardPackageInfo(onboardPackage, OnboardingTypesEnum.CSAR);
             } else if (packageExtension.equalsIgnoreCase(ZIP_EXTENSION)) {
+                addDummyHeat();
                 final OnboardPackage onboardPackage = new OnboardPackage(packageName, packageExtension,
                     ByteBuffer.wrap(packageFileContent), onboardPackageContentHandler);
                 return new OnboardPackageInfo(onboardPackage, OnboardingTypesEnum.ZIP);
@@ -106,6 +120,53 @@ public class OnboardingPackageProcessor {
         return null;
     }
 
+    private void addDummyHeat() {
+        // temporary fix for adding dummy base
+        List<FileData> newfiledata = new ArrayList<>();
+        try (InputStream zipFileManifest = onboardPackageContentHandler.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
+            ManifestContent manifestContent =
+                    JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
+            for (FileData fileData : manifestContent.getData()) {
+                if (Objects.nonNull(fileData.getType()) &&
+                        fileData.getType().equals(FileData.Type.HELM) && fileData.getBase()) {
+                    helmBase = true;
+                    fileData.setBase(false);
+                    FileData dummyHeat = new FileData();
+                    dummyHeat.setBase(true);
+                    dummyHeat.setFile("base_template_dummy_ignore.yaml");
+                    dummyHeat.setType(FileData.Type.HEAT);
+                    FileData dummyEnv = new FileData();
+                    dummyEnv.setBase(false);
+                    dummyEnv.setFile("base_template_dummy_ignore.env");
+                    dummyEnv.setType(FileData.Type.HEAT_ENV);
+                    List<FileData> dataEnvList = new ArrayList<>();
+                    dataEnvList.add(dummyEnv);
+                    dummyHeat.setData(dataEnvList);
+                    newfiledata.add(dummyHeat);
+                    String filePath = new File("").getAbsolutePath();
+                    File envFilePath = new File(filePath + "/base_template.env");
+                    File baseFilePath = new File(filePath + "/base_template.yaml");
+                    try (
+                            InputStream envStream = new FileInputStream(envFilePath);
+                            InputStream baseStream = new FileInputStream(baseFilePath);) {
+                        onboardPackageContentHandler.addFile("base_template_dummy_ignore.env", envStream);
+                        onboardPackageContentHandler.addFile("base_template_dummy_ignore.yaml", baseStream);
+                    } catch (Exception e) {
+                        LOGGER.error("Failed creating input stream {}", e);
+                    }
+                }
+            }
+            if (helmBase) {
+                manifestContent.getData().addAll(newfiledata);
+                InputStream manifestContentStream = new ByteArrayInputStream((JsonUtil.object2Json(manifestContent)).getBytes(StandardCharsets.UTF_8));
+                onboardPackageContentHandler.remove(SdcCommon.MANIFEST_NAME);
+                onboardPackageContentHandler.addFile(SdcCommon.MANIFEST_NAME, manifestContentStream);
+            }
+        } catch (Exception e) {
+            final String message = PACKAGE_INVALID_ERROR.formatMessage(packageFileName);
+            LOGGER.error(message, e);
+        }
+    }
     private boolean hasValidExtension() {
         final String packageExtension = FilenameUtils.getExtension(packageFileName);
         return packageExtension.equalsIgnoreCase(CSAR_EXTENSION) || packageExtension.equalsIgnoreCase(ZIP_EXTENSION);
index bff9791..45dc5fa 100644 (file)
@@ -27,8 +27,12 @@ import org.openecomp.sdc.common.errors.Messages;
 import org.openecomp.sdc.common.utils.SdcCommon;
 import org.openecomp.sdc.datatypes.error.ErrorLevel;
 import org.openecomp.sdc.datatypes.error.ErrorMessage;
+import org.openecomp.sdc.heat.datatypes.manifest.FileData;
+import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
@@ -43,9 +47,13 @@ import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
 import org.openecomp.sdc.versioning.dao.types.Version;
 
 import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
 import java.util.*;
 
 public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler {
+  private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class);
 
   private final CandidateService candidateService =
       CandidateServiceFactory.getInstance().createInterface();
@@ -67,6 +75,11 @@ public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemp
 
     Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors();
     FileContentHandler fileContentMap = fileContent.get();
+    try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
+      addDummyHeatBase(zipFileManifest ,fileContentMap);
+    } catch (Exception e) {
+      LOGGER.error("Invalid package content", e);
+    }
     FilesDataStructure structure =
         JsonUtil.json2Object(candidateData.getFilesDataStructure(), FilesDataStructure.class);
 
@@ -131,6 +144,28 @@ public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemp
     return response;
   }
 
+  private FileContentHandler addDummyHeatBase(InputStream zipFileManifest, FileContentHandler fileContentMap) {
+    ManifestContent manifestContent =
+            JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
+    for (FileData fileData : manifestContent.getData()) {
+      if (Objects.nonNull(fileData.getType()) &&
+              fileData.getType().equals(FileData.Type.HELM) && fileData.getBase()) {
+        String filePath = new File("").getAbsolutePath();
+        File envFilePath = new File(filePath + "/base_template.env");
+        File baseFilePath = new File(filePath + "/base_template.yaml");
+        try (
+                InputStream envStream = new FileInputStream(envFilePath);
+                InputStream baseStream = new FileInputStream(baseFilePath);) {
+          fileContentMap.addFile("base_template_dummy_ignore.env", envStream);
+          fileContentMap.addFile("base_template_dummy_ignore.yaml", baseStream);
+        } catch (Exception e) {
+          LOGGER.error("File not found error {}", e);
+        }
+      }
+    }
+    return fileContentMap;
+  }
+
   private Map<String, List<ErrorMessage>> getErrors(OrchestrationTemplateActionResponse
                                                         orchestrationTemplateActionResponse) {
     Map<String, List<ErrorMessage>> errors =
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/resources/DummyHeatUse.txt b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/resources/DummyHeatUse.txt
new file mode 100644 (file)
index 0000000..ff8c3e9
--- /dev/null
@@ -0,0 +1,5 @@
+In case we are uploading a zip file which has a non heat type base (temporary fix by creating a dummy heat):
+
+Copy the base_template.yaml and base_template.env from this folder to the onboard-be pod , where its running in the environment.
+
+The onboard-be pod is run in the path, /var/lib/jetty. Copy the env and yaml to this path.
\ No newline at end of file
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/resources/base_template.env b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/resources/base_template.env
new file mode 100644 (file)
index 0000000..1b96230
--- /dev/null
@@ -0,0 +1,4 @@
+parameters:
+  vnf_name: demo_vnf_ignore
+  vnf_id: demo_app
+  vf_module_id: demo_module_id
\ No newline at end of file
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/resources/base_template.yaml b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/resources/base_template.yaml
new file mode 100644 (file)
index 0000000..eb6962b
--- /dev/null
@@ -0,0 +1,49 @@
+
+
+heat_template_version: 2013-05-23
+
+description: Dummy base heat template
+
+##############
+#            #
+# PARAMETERS #
+#            #
+##############
+
+parameters:
+  vnf_name:
+    type: string
+    description: VNF_NAME
+  vnf_id:
+    type: string
+    label: VNF ID
+    description: The VNF ID is provided by ONAP
+  vf_module_id:
+    type: string
+    label: VF Module ID
+    description: The VF Module ID is provided by ONAP
+
+#############
+#           #
+# RESOURCES #
+#           #
+#############
+
+resources:
+  dummy:
+    type: OS::Heat::None
+    properties:
+      vnf_name:
+        get_param: vnf_name
+      vnf_id:
+        get_param: vnf_id
+      vf_module_id:
+        get_param: vf_module_id
+
+  #SDC won't allow too dummy resource as it has to find 'topology_template' TOSCA equivalent in heat templates
+  dummy_base:
+    type: OS::Nova::Server
+    properties:
+      name: test
+      image: test
+      flavor: test
\ No newline at end of file