From e5b8482c4d2ceddf559da26e532fdbb95a593dc1 Mon Sep 17 00:00:00 2001 From: KrupaNagabhushan Date: Thu, 19 Aug 2021 11:17:36 +0100 Subject: Load model default imports during CSAR Generation Issue-ID: SDC-3674 Signed-off-by: KrupaNagabhushan Change-Id: I89dc555a2a90f1ac44b44655e36a75a79ac38bd0 --- .../java/org/openecomp/sdc/be/tosca/CsarUtils.java | 44 +++++++++++++++++----- .../openecomp/sdc/be/tosca/ToscaExportHandler.java | 27 +++++++++++-- 2 files changed, 58 insertions(+), 13 deletions(-) (limited to 'catalog-be/src/main/java') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java index 3756ac5a3c..823ddc48ff 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java @@ -31,6 +31,7 @@ import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; @@ -76,6 +77,8 @@ import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao; +import org.openecomp.sdc.be.dao.cassandra.ToscaModelImportCassandraDao; +import org.openecomp.sdc.be.data.model.ToscaImportByModel; import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; @@ -169,6 +172,8 @@ public class CsarUtils { private ToscaExportHandler toscaExportUtils; @Autowired(required = false) private List generators; + @Autowired(required = false) + private ToscaModelImportCassandraDao toscaModelImportCassandraDao; private String versionFirstThreeOctets; public CsarUtils() { @@ -426,16 +431,21 @@ public class CsarUtils { if (zipOutputStreamOrResponseFormat != null && zipOutputStreamOrResponseFormat.isRight()) { return zipOutputStreamOrResponseFormat; } - //retrieve SDC.zip from Cassandra - Either latestSchemaFilesFromCassandra = getLatestSchemaFilesFromCassandra(); - if (latestSchemaFilesFromCassandra.isRight()) { - log.error("Error retrieving SDC Schema files from cassandra"); - return Either.right(latestSchemaFilesFromCassandra.right().value()); + if (component.getModel() == null) { + //retrieve SDC.zip from Cassandra + Either latestSchemaFiles = getLatestSchemaFilesFromCassandra(); + if (latestSchemaFiles.isRight()) { + log.error("Error retrieving SDC Schema files from cassandra"); + return Either.right(latestSchemaFiles.right().value()); + } + final byte[] schemaFileZip = latestSchemaFiles.left().value(); + final List nodesFromPackage = findNonRootNodesFromPackage(dependencies); + //add files from retrieved SDC.zip to Definitions folder in CSAR + addSchemaFilesFromCassandra(zip, schemaFileZip, nodesFromPackage); + } else { + //retrieve schema files by model from Cassandra + addSchemaFilesByModel(zip, component.getModel()); } - final byte[] schemaFileZip = latestSchemaFilesFromCassandra.left().value(); - final List nodesFromPackage = findNonRootNodesFromPackage(dependencies); - //add files from retrieved SDC.zip to Definitions folder in CSAR - addSchemaFilesFromCassandra(zip, schemaFileZip, nodesFromPackage); Either collectedComponentCsarDefinition = collectComponentCsarDefinition(component); if (collectedComponentCsarDefinition.isRight()) { return Either.right(collectedComponentCsarDefinition.right().value()); @@ -799,6 +809,22 @@ public class CsarUtils { .bind(iff(List::isEmpty, () -> schemaFileFetchError(fto), s -> Either.left(s.iterator().next().getPayloadAsArray()))); } + private void addSchemaFilesByModel(final ZipOutputStream zipOutputStream, final String model) { + try { + final List schemaImportsByModel = toscaModelImportCassandraDao.findAllByModel(model); + for (ToscaImportByModel toscaImportByModel : schemaImportsByModel) { + final ZipEntry zipEntry = new ZipEntry(DEFINITIONS_PATH + toscaImportByModel.getFullPath()); + zipOutputStream.putNextEntry(zipEntry); + final byte[] content = toscaImportByModel.getContent().getBytes(StandardCharsets.UTF_8); + zipOutputStream.write(content, 0, content.length); + zipOutputStream.closeEntry(); + } + } catch (IOException e) { + log.error("Error while writing the schema files by model to the CSAR " + "{}", e); + throw new ByResponseFormatComponentException(componentsUtils.getResponseFormat(ActionStatus.CSAR_TOSCA_IMPORTS_ERROR)); + } + } + private F schemaFilesFetchDBError(String firstThreeOctets) { return cos -> { log.debug("Failed to get the schema files SDC-Version: {} Conformance-Level {}. Please fix DB table accordingly.", firstThreeOctets, diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index 8b8c3153cf..b70d676200 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -47,6 +47,7 @@ import java.util.stream.Collectors; import lombok.NoArgsConstructor; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.ImmutableTriple; @@ -55,7 +56,9 @@ import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException; import org.openecomp.sdc.be.config.Configuration; import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.dao.cassandra.ToscaModelImportCassandraDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.data.model.ToscaImportByModel; import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition; import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition; @@ -168,6 +171,7 @@ public class ToscaExportHandler { private OutputConverter outputConverter; private InterfaceLifecycleOperation interfaceLifecycleOperation; private InterfacesOperationsConverter interfacesOperationsConverter; + private ToscaModelImportCassandraDao toscaModelImportCassandraDao; @Autowired public ToscaExportHandler(final ApplicationDataTypeCache applicationDataTypeCache, @@ -180,7 +184,8 @@ public class ToscaExportHandler { final InputConverter inputConverter, final OutputConverter outputConverter, final InterfaceLifecycleOperation interfaceLifecycleOperation, - final InterfacesOperationsConverter interfacesOperationsConverter) { + final InterfacesOperationsConverter interfacesOperationsConverter, + final ToscaModelImportCassandraDao toscaModelImportCassandraDao) { this.applicationDataTypeCache = applicationDataTypeCache; this.toscaOperationFacade = toscaOperationFacade; this.capabilityRequirementConverter = capabilityRequirementConverter; @@ -192,6 +197,7 @@ public class ToscaExportHandler { this.outputConverter = outputConverter; this.interfaceLifecycleOperation = interfaceLifecycleOperation; this.interfacesOperationsConverter = interfacesOperationsConverter; + this.toscaModelImportCassandraDao = toscaModelImportCassandraDao; } public static String getInterfaceFilename(String artifactName) { @@ -211,7 +217,7 @@ public class ToscaExportHandler { } public Either exportComponentInterface(final Component component, final boolean isAssociatedComponent) { - final List>> imports = new ArrayList<>(getDefaultToscaImportConfig()); + final List>> imports = new ArrayList<>(getDefaultToscaImports(component.getModel())); if (CollectionUtils.isEmpty(imports)) { log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION); return Either.right(ToscaError.GENERAL_ERROR); @@ -274,7 +280,7 @@ public class ToscaExportHandler { } public Either convertToToscaTemplate(final Component component) { - final List>> defaultToscaImportConfig = getDefaultToscaImportConfig(); + final List>> defaultToscaImportConfig = getDefaultToscaImports(component.getModel()); if (CollectionUtils.isEmpty(defaultToscaImportConfig)) { log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION); return Either.right(ToscaError.GENERAL_ERROR); @@ -297,6 +303,19 @@ public class ToscaExportHandler { } } + public List>> getDefaultToscaImports(final String model) { + if (model == null) { + return getDefaultToscaImportConfig(); + } + final List importsByModel = toscaModelImportCassandraDao.findAllByModel(model); + final List>> importList = new ArrayList<>(); + for(ToscaImportByModel toscaImportByModel: importsByModel) { + final String fileName = FilenameUtils.getBaseName(toscaImportByModel.getFullPath()); + importList.add(Map.of(fileName, Map.of("file", toscaImportByModel.getFullPath()))); + } + return importList; + } + private Either convertToscaTemplate(Component component, ToscaTemplate toscaNode) { Either>, ToscaError> importsRes = fillImports(component, toscaNode); if (importsRes.isRight()) { @@ -508,7 +527,7 @@ public class ToscaExportHandler { } private Either>, ToscaError> fillImports(Component component, ToscaTemplate toscaTemplate) { - final List>> defaultToscaImportConfig = getDefaultToscaImportConfig(); + final List>> defaultToscaImportConfig = getDefaultToscaImports(component.getModel()); if (CollectionUtils.isEmpty(defaultToscaImportConfig)) { log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION); return Either.right(ToscaError.GENERAL_ERROR); -- cgit 1.2.3-korg