From 63966da5c7a9bd6ba3fa9e97807447d7759e8ace Mon Sep 17 00:00:00 2001 From: JvD_Ericsson Date: Wed, 13 Jul 2022 11:49:36 +0100 Subject: Fix changed instance attribute value not visible in generated tosca also fixes attributes not being exported/imported with a schema Issue-ID: SDC-4093 Signed-off-by: JvD_Ericsson Change-Id: I693ae5c4c7717764445b20279bf61a7d3b47b434 --- .../csar/YamlTemplateParsingHandler.java | 33 ++++++++++++++++++ .../be/components/impl/ResourceImportManager.java | 6 ++++ .../impl/ServiceImportBusinessLogic.java | 20 +++++++++++ .../openecomp/sdc/be/tosca/AttributeConverter.java | 12 +++++-- .../openecomp/sdc/be/tosca/ToscaExportHandler.java | 5 +-- .../csar/YamlTemplateParsingHandlerTest.java | 37 +++++++++++++++++++++ .../sdc/be/tosca/AttributeConverterTest.java | 32 +++++++++++++----- .../src/test/resources/csars/with_groups.csar | Bin 66187 -> 66267 bytes 8 files changed, 130 insertions(+), 15 deletions(-) (limited to 'catalog-be') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java index ed753fdd18..940363bce4 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java @@ -19,6 +19,7 @@ * Modifications copyright (c) 2019 Nokia * ================================================================================ */ + package org.openecomp.sdc.be.components.csar; import static java.util.stream.Collectors.toList; @@ -29,6 +30,7 @@ import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaMap import static org.openecomp.sdc.be.components.impl.ImportUtils.findToscaElement; import static org.openecomp.sdc.be.components.impl.ImportUtils.loadYamlAsStrictMap; import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ATTRIBUTES; import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.CAPABILITIES; import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.CAPABILITY; import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE; @@ -100,6 +102,7 @@ import org.openecomp.sdc.be.model.PolicyDefinition; import org.openecomp.sdc.be.model.PolicyTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.UploadArtifactInfo; +import org.openecomp.sdc.be.model.UploadAttributeInfo; import org.openecomp.sdc.be.model.UploadCapInfo; import org.openecomp.sdc.be.model.UploadComponentInstanceInfo; import org.openecomp.sdc.be.model.UploadPropInfo; @@ -645,6 +648,7 @@ public class YamlTemplateParsingHandler { setCapabilities(nodeTemplateInfo, nodeTemplateJsonMap); setArtifacts(nodeTemplateInfo, nodeTemplateJsonMap); updateProperties(nodeTemplateInfo, nodeTemplateJsonMap); + updateAttributes(nodeTemplateInfo, nodeTemplateJsonMap); setDirectives(nodeTemplateInfo, nodeTemplateJsonMap); setNodeFilter(nodeTemplateInfo, nodeTemplateJsonMap); setSubstitutions(substitutionMappings, nodeTemplateInfo); @@ -682,6 +686,15 @@ public class YamlTemplateParsingHandler { } } + private void updateAttributes(UploadComponentInstanceInfo nodeTemplateInfo, Map nodeTemplateJsonMap) { + if (nodeTemplateJsonMap.containsKey(ATTRIBUTES.getElementName())) { + Map attributes = buildAttributeModuleFromYaml(nodeTemplateJsonMap); + if (!attributes.isEmpty()) { + nodeTemplateInfo.setAttributes(attributes); + } + } + } + private void setCapabilities(UploadComponentInstanceInfo nodeTemplateInfo, Map nodeTemplateJsonMap) { if (nodeTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) { Map> eitherCapRes = createCapModuleFromYaml(nodeTemplateJsonMap); @@ -915,6 +928,26 @@ public class YamlTemplateParsingHandler { return regTemplateInfo; } + private Map buildAttributeModuleFromYaml(Map nodeTemplateJsonMap) { + Map moduleAttribute = new HashMap<>(); + Either, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(nodeTemplateJsonMap, ATTRIBUTES); + if (toscaAttributes.isLeft()) { + Map jsonAttributes = toscaAttributes.left().value(); + for (Map.Entry jsonAttributeObj : jsonAttributes.entrySet()) { + UploadAttributeInfo attributeDef = buildAttribute(jsonAttributeObj.getKey(), jsonAttributeObj.getValue()); + moduleAttribute.put(attributeDef.getName(), attributeDef); + } + } + return moduleAttribute; + } + + private UploadAttributeInfo buildAttribute(String attributeName, Object attributeValue) { + UploadAttributeInfo attributeDef = new UploadAttributeInfo(); + attributeDef.setValue(attributeValue); + attributeDef.setName(attributeName); + return attributeDef; + } + private Map> buildPropModuleFromYaml(Map nodeTemplateJsonMap) { Map> moduleProp = new HashMap<>(); Either, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(nodeTemplateJsonMap, PROPERTIES); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java index aa420673c7..d02a84f491 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java @@ -66,6 +66,7 @@ import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; @@ -630,6 +631,11 @@ public class ResourceImportManager { } final AttributeDefinition attributeDefinition = entry.getValue(); attributeDefinition.setName(name); + if (attributeDefinition.getEntry_schema() != null && attributeDefinition.getEntry_schema().getType() != null) { + attributeDefinition.setSchema(new SchemaDefinition()); + attributeDefinition.getSchema().setProperty(new PropertyDataDefinition()); + attributeDefinition.getSchema().getProperty().setType(entry.getValue().getEntry_schema().getType()); + } attributeDefinitionList.add(attributeDefinition); } resource.setAttributes(attributeDefinitionList); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java index 3c21ae16a7..225a6c4408 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java @@ -23,6 +23,7 @@ import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaStr import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue; import static org.openecomp.sdc.be.tosca.CsarUtils.VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN; +import com.google.gson.Gson; import fj.data.Either; import java.util.ArrayList; import java.util.Collection; @@ -107,6 +108,7 @@ import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; import org.openecomp.sdc.be.model.RequirementDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.UploadAttributeInfo; import org.openecomp.sdc.be.model.UploadComponentInstanceInfo; import org.openecomp.sdc.be.model.UploadNodeFilterInfo; import org.openecomp.sdc.be.model.UploadPropInfo; @@ -1447,6 +1449,7 @@ public class ServiceImportBusinessLogic { } if (originResource.getAttributes() != null && !originResource.getAttributes().isEmpty()) { instAttributes.put(resourceInstanceId, originResource.getAttributes()); + addAttributeValueToResourceInstance(instAttributes, uploadComponentInstanceInfo.getAttributes()); } if (uploadComponentInstanceInfo.getUploadNodeFilterInfo() != null) { instNodeFilter.put(resourceInstanceId, uploadComponentInstanceInfo.getUploadNodeFilterInfo()); @@ -1514,6 +1517,23 @@ public class ServiceImportBusinessLogic { } } + private void addAttributeValueToResourceInstance(Map> instAttributes, + Map attributeMap) { + if (attributeMap == null) { + return; + } + attributeMap.forEach((attributeName, attributeValue) -> instAttributes.values() + .forEach(value -> value.stream().filter(attr -> attr.getName().equals(attributeName)).forEach(attr -> { + if (attributeValue.getValue() instanceof Collection || attributeValue.getValue() instanceof Map) { + Gson gson = new Gson(); + String json = gson.toJson(attributeValue.getValue()); + attr.setValue(json); + } else { + attr.setValue(String.valueOf(attributeValue.getValue())); + } + }))); + } + protected ResponseFormat addPropertyValuesToRi(UploadComponentInstanceInfo uploadComponentInstanceInfo, Component component, Resource originResource, ComponentInstance currentCompInstance, Map> instProperties, diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java index 561480dd64..40d531d788 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java @@ -27,6 +27,7 @@ import java.io.StringReader; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.onap.sdc.tosca.datatypes.model.EntrySchema; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; @@ -73,7 +74,14 @@ public class AttributeConverter { final ToscaAttribute toscaAttribute = new ToscaAttribute(); LOGGER.trace("Converting attribute '{}' from type '{}' with default value '{}'", attributeDefinition.getName(), attributeDefinition.getType(), attributeDefinition.getDefaultValue()); - toscaAttribute.setEntrySchema(convert(attributeDefinition.getEntry_schema())); + SchemaDefinition schema = attributeDefinition.getSchema(); + if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null + && StringUtils.isNotEmpty(schema.getProperty().getType())) { + final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition(); + toscaSchemaDefinition.setType(schema.getProperty().getType()); + toscaSchemaDefinition.setDescription(schema.getProperty().getDescription()); + toscaAttribute.setEntrySchema(toscaSchemaDefinition); + } toscaAttribute.setType(attributeDefinition.getType()); toscaAttribute.setDescription(attributeDefinition.getDescription()); toscaAttribute.setStatus(attributeDefinition.getStatus()); @@ -173,7 +181,7 @@ public class AttributeConverter { public void convertAndAddValue(final Map attribs, final AttributeDefinition attribute) { - final Object convertedValue = convertToToscaObject(attribute, attribute.getDefaultValue(), false); + final Object convertedValue = convertToToscaObject(attribute, attribute.getValue(), false); if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) { attribs.put(attribute.getName(), convertedValue); } 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 069e4f366e..a8aa6b33a9 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 @@ -1128,10 +1128,7 @@ public class ToscaExportHandler { final Map attribs) { if (isNotEmpty(componentInstancesAttributes) && componentInstancesAttributes.containsKey(instanceUniqueId)) { - componentInstancesAttributes.get(instanceUniqueId).stream() - // Filters out Attributes with empty default values - .filter(attributeDefinition -> StringUtils.isNotEmpty(attributeDefinition.getDefaultValue())) - // Converts and adds each value to attribute map + componentInstancesAttributes.get(instanceUniqueId) .forEach(attributeDefinition -> attributeConverter.convertAndAddValue(attribs, attributeDefinition)); } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandlerTest.java index a2931a515b..edf17b7e61 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandlerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandlerTest.java @@ -31,6 +31,7 @@ import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS; import java.io.File; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -260,6 +261,42 @@ public class YamlTemplateParsingHandlerTest { validateParsedYamlWithPolicies(parsedYaml); } + @Test + void parseResourceInstanceWithAttributesTest() { + stubGetGroupType(); + stubGetPolicyType(); + Resource resource = new Resource(); + ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(), + new HashMap<>(), "", resource, null); + validateParsedYamlWithAttributes(parsedYaml); + } + + private void validateParsedYamlWithAttributes(ParsedToscaYamlInfo parsedYaml) { + ArrayList expectedSubnetsShowList = new ArrayList<>(); + expectedSubnetsShowList.add("val1"); + expectedSubnetsShowList.add("val2"); + + HashMap expectedSubnetsNameMap = new HashMap<>(); + expectedSubnetsNameMap.put("name1", "name_val1"); + expectedSubnetsNameMap.put("name2", "name_val2"); + + + assertThat(parsedYaml.getInstances().get("resource_instance_with_attributes")).isNotNull(); + UploadComponentInstanceInfo resourceInstanceWithAttributes = parsedYaml.getInstances().get("resource_instance_with_attributes"); + assertEquals(5, resourceInstanceWithAttributes.getAttributes().size()); + + assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("fq_name")); + assertEquals(resourceInstanceWithAttributes.getAttributes().get("fq_name").getValue(), "fq_name_value"); + assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("tosca_name")); + assertEquals(resourceInstanceWithAttributes.getAttributes().get("tosca_name").getValue(), "tosca_name_value"); + assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_show")); + assertEquals(resourceInstanceWithAttributes.getAttributes().get("subnets_show").getValue(), expectedSubnetsShowList); + assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("subnets_name")); + assertEquals(resourceInstanceWithAttributes.getAttributes().get("subnets_name").getValue(), expectedSubnetsNameMap); + assertTrue(resourceInstanceWithAttributes.getAttributes().containsKey("new_attribute")); + assertEquals(resourceInstanceWithAttributes.getAttributes().get("new_attribute").getValue(), "new_attribute_value"); + } + private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List expectedProp) { assertThat(parsedYaml).isNotNull(); assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java index fcc0ebfffd..00779d40e2 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java @@ -33,12 +33,15 @@ import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; import org.onap.sdc.tosca.datatypes.model.EntrySchema; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.tosca.exception.ToscaConversionException; import org.openecomp.sdc.be.tosca.model.ToscaAttribute; +import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition; class AttributeConverterTest { @@ -80,10 +83,10 @@ class AttributeConverterTest { attributeDefinition.setType(ToscaPropertyType.LIST.getType()); attributeDefinition.setDescription("aDescription"); attributeDefinition.setStatus("aStatus"); - final EntrySchema entrySchema = new EntrySchema(); - entrySchema.setDescription("anEntrySchemaDescription"); - entrySchema.setType(ToscaPropertyType.LIST.getType()); - attributeDefinition.setEntry_schema(entrySchema); + attributeDefinition.setSchema(new SchemaDefinition()); + attributeDefinition.getSchema().setProperty(new PropertyDataDefinition()); + attributeDefinition.getSchema().getProperty().setType(ToscaPropertyType.LIST.getType()); + attributeDefinition.getSchema().getProperty().setDescription("anEntrySchemaDescription"); final List defaultValueList = new ArrayList<>(); defaultValueList.add("entry1"); defaultValueList.add("entry2"); @@ -143,15 +146,26 @@ class AttributeConverterTest { } @Test - void testConvertAndAddValue() throws ToscaConversionException { + void testConvertAndAddDefaultValue() throws ToscaConversionException { final AttributeConverter converter = createTestSubject(); final AttributeDefinition attribute = new AttributeDefinition(); attribute.setName("attrib"); attribute.setDefaultValue("default"); final Map attribs = new HashMap<>(); converter.convertAndAddValue(attribs, attribute); + assertEquals(0, attribs.size()); + } + + @Test + void testConvertAndAddValue() throws ToscaConversionException { + final AttributeConverter converter = createTestSubject(); + final AttributeDefinition attribute = new AttributeDefinition(); + attribute.setName("attrib"); + attribute.setValue("value"); + final Map attribs = new HashMap<>(); + converter.convertAndAddValue(attribs, attribute); assertEquals(1, attribs.size()); - assertEquals("default", attribs.get("attrib")); + assertEquals("value", attribs.get("attrib")); } private AttributeConverter createTestSubject() { @@ -167,13 +181,13 @@ class AttributeConverterTest { assertEquals(expectedAttributeDefinition.getType(), actualToscaAttribute.getType()); assertEquals(expectedAttributeDefinition.getDescription(), actualToscaAttribute.getDescription()); assertEquals(expectedAttributeDefinition.getStatus(), actualToscaAttribute.getStatus()); - if (expectedAttributeDefinition.getEntry_schema() == null) { + if (expectedAttributeDefinition.getSchema() == null) { assertNull(actualToscaAttribute.getEntrySchema()); } else { - assertEquals(expectedAttributeDefinition.getEntry_schema().getType(), + assertEquals(expectedAttributeDefinition.getSchema().getProperty().getType(), actualToscaAttribute.getEntrySchema().getType()); assertEquals( - expectedAttributeDefinition.getEntry_schema().getDescription(), + expectedAttributeDefinition.getSchema().getProperty().getDescription(), actualToscaAttribute.getEntrySchema().getDescription()); } assertEquals(expectedDefault, actualToscaAttribute.getDefault()); diff --git a/catalog-be/src/test/resources/csars/with_groups.csar b/catalog-be/src/test/resources/csars/with_groups.csar index 49b5a440ea..dd7f5dfb69 100644 Binary files a/catalog-be/src/test/resources/csars/with_groups.csar and b/catalog-be/src/test/resources/csars/with_groups.csar differ -- cgit 1.2.3-korg