diff options
Diffstat (limited to 'common')
3 files changed, 76 insertions, 4 deletions
diff --git a/common/onap-tosca-datatype/pom.xml b/common/onap-tosca-datatype/pom.xml index 9a621e02f1..e6c6bc5b28 100644 --- a/common/onap-tosca-datatype/pom.xml +++ b/common/onap-tosca-datatype/pom.xml @@ -33,7 +33,7 @@ <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> - <version>1.17</version> + <version>${snakeyaml.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> @@ -90,4 +90,12 @@ <scope>test</scope> </dependency> </dependencies> + <build> + <testResources> + <testResource> + <directory>src/test/resources</directory> + <filtering>true</filtering> + </testResource> + </testResources> + </build> </project> diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeTemplate.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeTemplate.java index 82f1581fa9..ae1ca01673 100644 --- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeTemplate.java +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeTemplate.java @@ -20,16 +20,23 @@ package org.onap.sdc.tosca.datatypes.model; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; + import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; + +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.onap.sdc.tosca.error.ToscaRuntimeException; +import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil; import org.onap.sdc.tosca.services.YamlUtil; +import org.yaml.snakeyaml.constructor.ConstructorException; @Getter @@ -37,6 +44,7 @@ import org.onap.sdc.tosca.services.YamlUtil; @EqualsAndHashCode public class NodeTemplate implements Template, Cloneable { + private static final String INVALID_TOSCA_REQUIREMENT_SECTION = "Invalid TOSCA requirement section"; private String type; private String description; private Map<String, String> metadata; @@ -50,6 +58,62 @@ public class NodeTemplate implements Template, Cloneable { private NodeFilter node_filter; private String copy; + public void setRequirements(List requirementAssignmentObj) { + this.requirements = convertToscaRequirementAssignment(requirementAssignmentObj); + } + + public static List<Map<String, RequirementAssignment>> convertToscaRequirementAssignment(List<?> requirementAssignmentObj) { + + List<Map<String, RequirementAssignment>> convertedRequirements = new ArrayList<>(); + if (CollectionUtils.isEmpty(requirementAssignmentObj)) { + return null; + } + for (Object requirementEntry : requirementAssignmentObj) { + convertToscaRequirementAssignmentEntry(convertedRequirements, requirementEntry); + } + return convertedRequirements; + } + + private static void convertToscaRequirementAssignmentEntry(List<Map<String, RequirementAssignment>> convertedRequirements, Object requirementEntry) { + if (requirementEntry instanceof Map) { + try { + Set<Map.Entry<String, RequirementAssignment>> requirementEntries = ((Map)requirementEntry).entrySet(); + for (Map.Entry<String, RequirementAssignment> toscaRequirements : requirementEntries) { + String key = toscaRequirements.getKey(); + Object requirementValue = toscaRequirements.getValue(); + if (requirementValue instanceof Map) { + RequirementAssignment requirementObject; + try { + YamlUtil yamlUtil = new YamlUtil(); + requirementObject = yamlUtil + .yamlToObject(yamlUtil.objectToYaml(requirementValue), RequirementAssignment.class); + } catch (ConstructorException ex) { + // The requirement might contains extended attribute, so try to parse it into RequirementAssignmentExt as well + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + requirementObject = toscaExtensionYamlUtil + .yamlToObject(toscaExtensionYamlUtil.objectToYaml(requirementValue), RequirementAssignment.class); + } + Map<String, RequirementAssignment> convertedToscaRequirement = new HashMap<>(); + convertedToscaRequirement.put(key, requirementObject); + convertedRequirements.add(convertedToscaRequirement); + } else if (requirementValue instanceof RequirementAssignment) { + Map<String, RequirementAssignment> convertedToscaRequirement = new HashMap<>(); + convertedToscaRequirement.put(key, (RequirementAssignment)requirementValue); + convertedRequirements.add(convertedToscaRequirement); + } + } + } catch (Exception ex) { + throw new ToscaRuntimeException(INVALID_TOSCA_REQUIREMENT_SECTION, ex); + } + } + } + + public void addRequirements(Map<String, RequirementAssignment> newRequirement) { + if (CollectionUtils.isEmpty(this.requirements)) { + this.requirements = new ArrayList<Map<String, RequirementAssignment>>(); + } + this.requirements.add(newRequirement); + } public Map<String, InterfaceDefinitionTemplate> getNormalizeInterfaces() { if (MapUtils.isEmpty(interfaces)) { diff --git a/common/onap-tosca-datatype/src/test/java/org/onap/sdc/tosca/datatypes/model/NodeTemplateTest.java b/common/onap-tosca-datatype/src/test/java/org/onap/sdc/tosca/datatypes/model/NodeTemplateTest.java index b9f2b838c2..e265d4bd8b 100644 --- a/common/onap-tosca-datatype/src/test/java/org/onap/sdc/tosca/datatypes/model/NodeTemplateTest.java +++ b/common/onap-tosca-datatype/src/test/java/org/onap/sdc/tosca/datatypes/model/NodeTemplateTest.java @@ -75,17 +75,17 @@ public class NodeTemplateTest { @Test public void shouldHaveValidGettersAndSetters() { - assertThat(NodeTemplate.class, hasValidGettersAndSettersExcluding("normalizeInterfaces")); + assertThat(NodeTemplate.class, hasValidGettersAndSettersExcluding("requirements", "normalizeInterfaces")); } @Test public void shouldHaveValidEquals() { - assertThat(NodeTemplate.class, hasValidBeanEqualsExcluding("normalizeInterfaces")); + assertThat(NodeTemplate.class, hasValidBeanEqualsExcluding("requirements", "normalizeInterfaces")); } @Test public void shouldHaveValidHashCode() { - assertThat(NodeTemplate.class, hasValidBeanHashCodeExcluding("normalizeInterfaces")); + assertThat(NodeTemplate.class, hasValidBeanHashCodeExcluding("requirements", "normalizeInterfaces")); } private InterfaceDefinitionTemplate createInterfaceDefinitionTemplate() { |