diff options
author | KrupaNagabhushan <krupa.nagabhushan@est.tech> | 2021-08-19 11:17:36 +0100 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2021-08-23 14:32:23 +0000 |
commit | e5b8482c4d2ceddf559da26e532fdbb95a593dc1 (patch) | |
tree | 9f5c07e281573e40a85fc7f5e3fbbfeedbb35aeb | |
parent | f16cc47e14f2c657f1caae59fa614ee8e21b96cd (diff) |
Load model default imports during CSAR Generation
Issue-ID: SDC-3674
Signed-off-by: KrupaNagabhushan <krupa.nagabhushan@est.tech>
Change-Id: I89dc555a2a90f1ac44b44655e36a75a79ac38bd0
5 files changed, 76 insertions, 22 deletions
diff --git a/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml b/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml index cbbd5eeef6..8f5b2b6355 100644 --- a/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml +++ b/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml @@ -2545,3 +2545,10 @@ errors: messageId: "SVC4153" } + #-----------SVC4154--------------------------- + CSAR_TOSCA_IMPORTS_ERROR: { + code: 500, + message: "Error: An error has occurred while including the default TOSCA imports in the CSAR", + messageId: "SVC4154" + } + 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<CsarEntryGenerator> 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<byte[], ResponseFormat> 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<byte[], ResponseFormat> 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<String> 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<String> nodesFromPackage = findNonRootNodesFromPackage(dependencies); - //add files from retrieved SDC.zip to Definitions folder in CSAR - addSchemaFilesFromCassandra(zip, schemaFileZip, nodesFromPackage); Either<CsarDefinition, ResponseFormat> 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<ToscaImportByModel> 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<CassandraOperationStatus, ResponseFormat> 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<ToscaRepresentation, ToscaError> exportComponentInterface(final Component component, final boolean isAssociatedComponent) { - final List<Map<String, Map<String, String>>> imports = new ArrayList<>(getDefaultToscaImportConfig()); + final List<Map<String, Map<String, String>>> 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<ToscaTemplate, ToscaError> convertToToscaTemplate(final Component component) { - final List<Map<String, Map<String, String>>> defaultToscaImportConfig = getDefaultToscaImportConfig(); + final List<Map<String, Map<String, String>>> 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<Map<String, Map<String, String>>> getDefaultToscaImports(final String model) { + if (model == null) { + return getDefaultToscaImportConfig(); + } + final List<ToscaImportByModel> importsByModel = toscaModelImportCassandraDao.findAllByModel(model); + final List<Map<String, Map<String, String>>> 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<ToscaTemplate, ToscaError> convertToscaTemplate(Component component, ToscaTemplate toscaNode) { Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> importsRes = fillImports(component, toscaNode); if (importsRes.isRight()) { @@ -508,7 +527,7 @@ public class ToscaExportHandler { } private Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> fillImports(Component component, ToscaTemplate toscaTemplate) { - final List<Map<String, Map<String, String>>> defaultToscaImportConfig = getDefaultToscaImportConfig(); + final List<Map<String, Map<String, String>>> defaultToscaImportConfig = getDefaultToscaImports(component.getModel()); if (CollectionUtils.isEmpty(defaultToscaImportConfig)) { log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION); return Either.right(ToscaError.GENERAL_ERROR); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java index 771d52f6b0..074d70bfe0 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java @@ -123,7 +123,7 @@ class InterfacesOperationsConverterTest { addInterfaceTypeElement(component, new ArrayList<>()); ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, - interfacesOperationsConverter); + interfacesOperationsConverter, null); ToscaTemplate template = new ToscaTemplate("test"); template.setInterface_types(interfaceTypeElement); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); @@ -151,7 +151,7 @@ class InterfacesOperationsConverterTest { addInterfaceTypeElement(component, new ArrayList<>()); ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, - interfacesOperationsConverter); + interfacesOperationsConverter, null); ToscaTemplate template = new ToscaTemplate("testService"); template.setInterface_types(interfaceTypeElement); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); @@ -177,7 +177,7 @@ class InterfacesOperationsConverterTest { interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, - interfacesOperationsConverter); + interfacesOperationsConverter, null); ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME); Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put(NODE_TYPE_NAME, nodeType); @@ -207,7 +207,7 @@ class InterfacesOperationsConverterTest { interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, - interfacesOperationsConverter); + interfacesOperationsConverter, null); ToscaTemplate template = new ToscaTemplate("testService"); Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put(NODE_TYPE_NAME, nodeType); @@ -266,7 +266,7 @@ class InterfacesOperationsConverterTest { interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, null, false); ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, - interfacesOperationsConverter); + interfacesOperationsConverter, null); ToscaTemplate template = new ToscaTemplate("test"); Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put("test", nodeType); @@ -300,7 +300,7 @@ class InterfacesOperationsConverterTest { interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, - interfacesOperationsConverter); + interfacesOperationsConverter, null); ToscaTemplate template = new ToscaTemplate("test"); Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put("test", nodeType); @@ -349,7 +349,7 @@ class InterfacesOperationsConverterTest { interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, - interfacesOperationsConverter); + interfacesOperationsConverter, null); ToscaTemplate template = new ToscaTemplate("test"); Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put("test", nodeType); @@ -384,7 +384,7 @@ class InterfacesOperationsConverterTest { final ToscaNodeType nodeType = new ToscaNodeType(); interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); final ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, - interfacesOperationsConverter); + interfacesOperationsConverter, null); final ToscaTemplate template = new ToscaTemplate("testService"); final Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); nodeTypes.put(NODE_TYPE_NAME, nodeType); diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/api/ActionStatus.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/api/ActionStatus.java index 731ff63c4f..6374be1e50 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/api/ActionStatus.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/api/ActionStatus.java @@ -121,5 +121,7 @@ public enum ActionStatus { //Requirement related REQUIREMENT_NOT_FOUND, REQUIREMENT_NAME_MANDATORY, REQUIREMENT_CAPABILITY_MANDATORY, REQUIREMENT_NAME_ALREADY_IN_USE, REQUIREMENT_DELETION_NOT_ALLOWED_USED_IN_COMPOSITION, REQUIREMENT_UPDATE_NOT_ALLOWED_USED_IN_COMPOSITION, INVALID_REQUIREMENT_NAME, //Abstract template related - ABSTRACT, NORMAL + ABSTRACT, NORMAL, + //including default imports in CSAR related + CSAR_TOSCA_IMPORTS_ERROR } |