* Modifications copyright (c) 2019 Nokia
* ================================================================================
*/
+
package org.openecomp.sdc.be.components.csar;
import static java.util.stream.Collectors.toList;
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;
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;
setCapabilities(nodeTemplateInfo, nodeTemplateJsonMap);
setArtifacts(nodeTemplateInfo, nodeTemplateJsonMap);
updateProperties(nodeTemplateInfo, nodeTemplateJsonMap);
+ updateAttributes(nodeTemplateInfo, nodeTemplateJsonMap);
setDirectives(nodeTemplateInfo, nodeTemplateJsonMap);
setNodeFilter(nodeTemplateInfo, nodeTemplateJsonMap);
setSubstitutions(substitutionMappings, nodeTemplateInfo);
}
}
+ 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);
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);
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;
}
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);
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;
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;
}
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());
}
}
+ 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,
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;
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());
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);
}
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));
}
}
import java.io.File;
import java.net.URISyntaxException;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
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);
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 {
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");
}
@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() {
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());
--- /dev/null
+/*
+ * ============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;
+
+}
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;
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));