Fix changed instance attribute value not visible in generated tosca 08/129908/5
authorJvD_Ericsson <jeff.van.dam@est.tech>
Wed, 13 Jul 2022 10:49:36 +0000 (11:49 +0100)
committerMichael Morris <michael.morris@est.tech>
Thu, 21 Jul 2022 13:33:07 +0000 (13:33 +0000)
also fixes attributes not being exported/imported with a schema

Issue-ID: SDC-4093
Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech>
Change-Id: I693ae5c4c7717764445b20279bf61a7d3b47b434

catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java
catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java
catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java
catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java
catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandlerTest.java
catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java
catalog-be/src/test/resources/csars/with_groups.csar
catalog-model/src/main/java/org/openecomp/sdc/be/model/UploadAttributeInfo.java [new file with mode: 0644]
catalog-model/src/main/java/org/openecomp/sdc/be/model/UploadComponentInstanceInfo.java
integration-tests/src/test/java/org/onap/sdc/frontend/ci/tests/execute/sanity/ImportVfcUiTest.java

index ed753fd..940363b 100644 (file)
@@ -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<String, Object> nodeTemplateJsonMap) {
+        if (nodeTemplateJsonMap.containsKey(ATTRIBUTES.getElementName())) {
+            Map<String, UploadAttributeInfo> attributes = buildAttributeModuleFromYaml(nodeTemplateJsonMap);
+            if (!attributes.isEmpty()) {
+                nodeTemplateInfo.setAttributes(attributes);
+            }
+        }
+    }
+
     private void setCapabilities(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
         if (nodeTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
             Map<String, List<UploadCapInfo>> eitherCapRes = createCapModuleFromYaml(nodeTemplateJsonMap);
@@ -915,6 +928,26 @@ public class YamlTemplateParsingHandler {
         return regTemplateInfo;
     }
 
+    private Map<String, UploadAttributeInfo> buildAttributeModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
+        Map<String, UploadAttributeInfo> moduleAttribute = new HashMap<>();
+        Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(nodeTemplateJsonMap, ATTRIBUTES);
+        if (toscaAttributes.isLeft()) {
+            Map<String, Object> jsonAttributes = toscaAttributes.left().value();
+            for (Map.Entry<String, Object> 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<String, List<UploadPropInfo>> buildPropModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
         Map<String, List<UploadPropInfo>> moduleProp = new HashMap<>();
         Either<Map<String, Object>, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(nodeTemplateJsonMap, PROPERTIES);
index aa42067..d02a84f 100644 (file)
@@ -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);
index 3c21ae1..225a6c4 100644 (file)
@@ -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<String, List<AttributeDefinition>> instAttributes,
+                                                     Map<String, UploadAttributeInfo> 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<String, List<ComponentInstanceProperty>> instProperties,
index 561480d..40d531d 100644 (file)
@@ -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<String, Object> 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);
         }
index 069e4f3..a8aa6b3 100644 (file)
@@ -1128,10 +1128,7 @@ public class ToscaExportHandler {
                                                   final Map<String, Object> 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));
         }
     }
index a2931a5..edf17b7 100644 (file)
@@ -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<String> expectedSubnetsShowList = new ArrayList<>();
+        expectedSubnetsShowList.add("val1");
+        expectedSubnetsShowList.add("val2");
+
+        HashMap<String, String> 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<String> expectedProp) {
         assertThat(parsedYaml).isNotNull();
         assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
index fcc0ebf..00779d4 100644 (file)
@@ -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<String> 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<String, Object> 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<String, Object> 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());
index 49b5a44..dd7f5df 100644 (file)
Binary files a/catalog-be/src/test/resources/csars/with_groups.csar and b/catalog-be/src/test/resources/csars/with_groups.csar differ
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/UploadAttributeInfo.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/UploadAttributeInfo.java
new file mode 100644 (file)
index 0000000..47da4c6
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2022 Nordix Foundation. 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.be.model;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class UploadAttributeInfo extends UploadInfo {
+
+    private Object value;
+
+}
index 95649a9..57652ee 100644 (file)
@@ -35,6 +35,7 @@ public class UploadComponentInstanceInfo {
     private Map<String, List<UploadReqInfo>> requirements;
     private Map<String, Map<String, UploadArtifactInfo>> artifacts;
     private Map<String, List<UploadPropInfo>> properties;
+    private Map<String, UploadAttributeInfo> attributes;
     private Map<String, String> capabilitiesNamesToUpdate;
     private Map<String, String> requirementsNamesToUpdate;
     private Collection<String> directives;
index ba4b6d7..c8a45de 100644 (file)
@@ -397,10 +397,7 @@ public class ImportVfcUiTest extends SetupCDTest {
         assertFalse(MapUtils.isEmpty(nodeTemplates));
 
         final Map<String, Object> attributes = getMapEntry((Map<String, Object>) nodeTemplates.get(createdComponentInstance.getName()), "attributes");
-        assertFalse(MapUtils.isEmpty(attributes));
-        assertEquals(4, attributes.keySet().stream()
-            .filter(s -> (s.contains("test_1") || s.contains("test_3") || s.contains("test_4") || s.contains("test_9")) && !s.contains("test_2"))
-            .count());
+        assertTrue(MapUtils.isEmpty(attributes));
 
         final Map<String, Object> substitutionMappings = getMapEntry(topologyTemplate, "substitution_mappings");
         assertFalse(MapUtils.isEmpty(substitutionMappings));