From f0c3f38fc7d23b7d6fcda939aafe57d429b8f41a Mon Sep 17 00:00:00 2001 From: "katy.rotman" Date: Wed, 23 May 2018 15:50:38 +0300 Subject: new node filter implementation Issue-ID: SDC-1363 Change-Id: I0bd8b482e8e55f92ac3bccf8725d64f1b4929a43 Signed-off-by: katy.rotman --- .../tosca/datatypes/model/CapabilityFilter.java | 46 ++ .../model/CapabilityFilterDefinition.java | 37 - .../onap/sdc/tosca/datatypes/model/NodeFilter.java | 26 +- .../sdc/tosca/datatypes/ToscaModelTest.java | 823 ++++++++++----------- .../sdc/tosca/datatypes/model/NodeFilterTest.java | 144 ++++ .../test/resources/mock/model/serviceTemplate.yaml | 6 +- .../mock/model/serviceTemplateHeatExtend.yaml | 18 +- 7 files changed, 618 insertions(+), 482 deletions(-) create mode 100644 common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/CapabilityFilter.java delete mode 100644 common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/CapabilityFilterDefinition.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/model/NodeFilterTest.java diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/CapabilityFilter.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/CapabilityFilter.java new file mode 100644 index 0000000000..0453325aa2 --- /dev/null +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/CapabilityFilter.java @@ -0,0 +1,46 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.onap.sdc.tosca.datatypes.model; + +import java.util.List; +import java.util.Map; + +/** + * @author KATYR + * @since May 13, 2018 + */ + +public class CapabilityFilter { + + List>> properties; + + public List>> getProperties() { + return properties; + } + + public void setProperties(List>> properties) { + this.properties = properties; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("CapabilityFilter{"); + sb.append("properties=").append(properties); + sb.append('}'); + return sb.toString(); + } +} diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/CapabilityFilterDefinition.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/CapabilityFilterDefinition.java deleted file mode 100644 index 83f9d2576b..0000000000 --- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/CapabilityFilterDefinition.java +++ /dev/null @@ -1,37 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. 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.onap.sdc.tosca.datatypes.model; - -import java.util.List; - - - -public class CapabilityFilterDefinition { - List properties; - - public List getProperties() { - return properties; - } - - public void setProperties(List properties) { - this.properties = properties; - } -} diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeFilter.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeFilter.java index 692d7e47ee..d1d07b56e6 100644 --- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeFilter.java +++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/NodeFilter.java @@ -7,9 +7,9 @@ * 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. @@ -24,26 +24,24 @@ import java.util.List; import java.util.Map; - public class NodeFilter { - Map> properties; - Map capabilities; - - public Map> getProperties() { + List>> properties; + List> capabilities; - return properties; + public List> getCapabilities() { + return capabilities; } - public void setProperties(Map> properties) { - this.properties = properties; + public void setCapabilities(List> capabilities) { + this.capabilities = capabilities; } - public Map getCapabilities() { - return capabilities; + public List>> getProperties() { + return properties; } - public void setCapabilities(Map capabilities) { - this.capabilities = capabilities; + public void setProperties(List>> properties) { + this.properties = properties; } } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java index 7405f50829..6fb10ac1e0 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/ToscaModelTest.java @@ -1,25 +1,28 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ +/* + * Copyright © 2016-2018 European Support Limited + * * 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.tosca.datatypes; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; import org.junit.Assert; import org.junit.Test; import org.onap.sdc.tosca.datatypes.model.ArtifactType; @@ -49,260 +52,254 @@ import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping; import org.onap.sdc.tosca.datatypes.model.TopologyTemplate; import org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt; -import org.openecomp.sdc.tosca.services.DataModelUtil; -import org.openecomp.sdc.tosca.services.ToscaConstants; import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil; import org.onap.sdc.tosca.services.YamlUtil; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import org.openecomp.sdc.tosca.services.DataModelUtil; +import org.openecomp.sdc.tosca.services.ToscaConstants; public class ToscaModelTest { - private YamlUtil yamlUtil = new YamlUtil(); - private static final String INTERFACE_ID = "inter_1"; - private static final String NODE_TEMPLATE_ID = "firstNodeTemplate"; - private static final String NODE_TYPE_ID = "compute_node_type"; - private static final String BASE_DIR = "/mock/model"; - private static final String ST = "/serviceTemplate.yaml"; - private static final String ST_WITH_INTERFACE = "/serviceTemplateWithInterface.yaml"; - private static final String ST_WITH_OPERATIONS = "/serviceTemplateWithInterfaceAndOperation.yaml"; - private static final String ST_WITH_INTERFACE_DEF = - "/serviceTemplateWithNodeTemplateInterface.yaml"; - private static final String ST_WITH_NODE_INTERFACE_DEF = - "/serviceTemplateWithNodeTypeInterface.yaml"; - private static final String INTERFACE_TYPE_VALUE = "tosca.interfaces.node.lifecycle.Standard"; - private static final String OPERATION_START = "start"; - private static final String OPERATION_DESC = "start operation"; - private static final String IMPLEMENTATION_NAME = "startWorkFlow.json"; - private static final String PRIMARY_IMPL = "myImpl.yaml"; - private static final String DEPENDENCY_NAME = "script1.sh"; - private static final String STRING_TYPE = "string"; - @Test - public void testServiceTemplateJavaToYaml() { - ServiceTemplate serviceTemplate = new ServiceTemplate(); - Map metadata = new HashMap<>(); - metadata.put("Template_author", "OPENECOMP"); - metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "Test"); - metadata.put("Template_version", "1.0.0"); - serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0"); - serviceTemplate.setDescription("testing desc tosca service template"); - - Import fileImport1 = new Import(); - fileImport1.setFile("path1/path2/file1.yaml"); - Import fileImport2 = new Import(); - fileImport2.setFile("path1/path2/file2.yaml"); - List> imports = new ArrayList<>(); - Map importsMap = new HashMap<>(); - importsMap.put("myfile1", fileImport1); - imports.add(importsMap); - importsMap = new HashMap<>(); - importsMap.put("myfile2", fileImport2); - imports.add(importsMap); - serviceTemplate.setImports(imports); - - OperationDefinition operationDefinition = new OperationDefinition(); - operationDefinition.setDescription("test operation"); - InterfaceType interfaceType = new InterfaceType(); - interfaceType.setDerived_from("derived_from"); - interfaceType.setDescription("desc"); - interfaceType.addOperation("test", operationDefinition); - serviceTemplate.addInterfaceType("test_interface", interfaceType); - - ArtifactType artifact = new ArtifactType(); - artifact.setMime_type("application/java-archive"); - ArrayList ext = new ArrayList<>(); - ext.add("yaml"); - ext.add("xml"); - artifact.setFile_ext(ext); - Map artifactTypes = new HashMap<>(); - artifactTypes.put("one_artifact", artifact); - serviceTemplate.setArtifact_types(artifactTypes); - - NodeType nodeType = new NodeType(); - nodeType.setDerived_from("tosca.nodes.Root"); - nodeType.setVersion("1.0.0"); - nodeType.setDescription("tosca compute test"); - - PropertyDefinition propDef1 = new PropertyDefinition(); - propDef1.setType("integer"); - propDef1.setDescription("Number of CPUs requested for a software node instance"); - propDef1.setRequired(true); - propDef1.set_default(1); - - Constraint graterOrEqual = new Constraint(); - graterOrEqual.setGreater_or_equal((float) 5.0); - Constraint constraintEqual = new Constraint(); - constraintEqual.setEqual(5); - Constraint greater_than = new Constraint(); - greater_than.setGreater_than(6.02); - Constraint inRange = new Constraint(); - inRange.setIn_range(new Object[2]); - inRange.getIn_range()[0] = 0; - inRange.getIn_range()[1] = ToscaConstants.UNBOUNDED; - - List constraints = new ArrayList<>(); - constraints.add(graterOrEqual); - constraints.add(constraintEqual); - constraints.add(greater_than); - constraints.add(inRange); - propDef1.setConstraints(constraints); - - Map properties = new HashMap<>(); - properties.put("cpu_num", propDef1); - nodeType.setProperties(properties); - - Map attributesDef = new HashMap<>(); - AttributeDefinition attDef = new AttributeDefinition(); - attDef.setType(PropertyType.STRING.getDisplayName()); - attDef.set_default("hi"); - attributesDef.put("attDef1", attDef); - nodeType.setAttributes(attributesDef); - - Map reqsDef = new HashMap<>(); - RequirementDefinition reqDef = new RequirementDefinition(); - reqDef.setCapability("tosca.cap1"); - reqDef.getOccurrences()[0] = 5; - reqsDef.put("re1", reqDef); - List> reqList = new ArrayList<>(); - reqList.add(reqsDef); - nodeType.setRequirements(reqList); - - - Map capsDef = new HashMap<>(); - CapabilityDefinition capdef = new CapabilityDefinition(); - capdef.setType("tosca.cap"); - List vvSource = new ArrayList<>(); - vvSource.add("node1"); - vvSource.add("node2"); - capdef.setValid_source_types(vvSource); - capsDef.put("cap1", capdef); - nodeType.setCapabilities(capsDef); - - Map nodeTypes = new HashMap<>(); - nodeTypes.put("compute_node_type", nodeType); - serviceTemplate.setNode_types(nodeTypes); - - TopologyTemplate topologyTemplate = new TopologyTemplate(); - topologyTemplate.setDescription("topologi template descroption"); - Map inputs = new HashMap<>(); - ParameterDefinition paramDef = new ParameterDefinition(); - paramDef.setType(PropertyType.STRING.getDisplayName()); - paramDef.setDescription("desc"); - paramDef.set_default("my default val"); - paramDef.setRequired(false); - paramDef.setEntry_schema(DataModelUtil.createEntrySchema("tosca.myType", null, null)); - List paramConstraint = new ArrayList<>(); - Constraint paramConst1 = new Constraint(); - paramConst1.setGreater_than(6); - Constraint paramConst2 = new Constraint(); - paramConst2.setGreater_or_equal(9); - paramConstraint.add(paramConst1); - paramConstraint.add(paramConst2); - paramDef.setConstraints(paramConstraint); - inputs.put("inParam1", paramDef); - topologyTemplate.setInputs(inputs); - - Map nodeTemplates = new HashMap<>(); - NodeTemplate nodeTemplate = new NodeTemplate(); - nodeTemplate.setType("nodeTypeRef"); - List directives = new ArrayList<>(); - directives.add(Directive.SELECTABLE.getDisplayName()); - directives.add(Directive.SUBSTITUTABLE.getDisplayName()); - nodeTemplate.setDirectives(directives); - Map nodeTemplateProperties = new HashMap<>(); - nodeTemplateProperties.put("prop1", "abcd"); - nodeTemplateProperties.put("prop2", "{ get_input: my_mysql_rootpw }"); - nodeTemplate.setProperties(nodeTemplateProperties); - Map nodeTemplateAtts = new HashMap<>(); - nodeTemplateAtts.put("att1", "att1Val"); - nodeTemplateAtts.put("att2", "{ get_input: my_mysql_rootpw }"); - nodeTemplate.setAttributes(nodeTemplateAtts); - - - RequirementAssignment reqAssignment1 = new RequirementAssignment(); - reqAssignment1.setNode("nodeA"); - reqAssignment1.setCapability("capA"); - reqAssignment1.setRelationship("relationB"); - Object[] reqAssOccurrences = new Object[2]; - reqAssOccurrences[0] = 1; - reqAssOccurrences[1] = 2; - reqAssignment1.setOccurrences(reqAssOccurrences); - NodeFilter reqNodeFilter = new NodeFilter(); - List propConstrain1 = new ArrayList<>(); - Constraint propConst1 = new Constraint(); - propConst1.setGreater_or_equal(9); - propConstrain1.add(propConst1); - List propConstrain2 = new ArrayList<>(); - Constraint propConst2 = new Constraint(); - propConst2.setMin_length(1); - propConstrain2.add(propConst2); - Constraint propConst3 = new Constraint(); - propConst3.setMax_length(2); - propConstrain2.add(propConst3); - Map> nodeFilterProp = new HashMap<>(); - nodeFilterProp.put("propName1", propConstrain1); - nodeFilterProp.put("propName2", propConstrain2); - reqNodeFilter.setProperties(nodeFilterProp); - reqAssignment1.setNode_filter(reqNodeFilter); - - RequirementAssignment reqAssignment2 = new RequirementAssignment(); - reqAssignment2.setNode("nodeA"); - reqAssignment2.setCapability("capA"); - reqAssignment2.setRelationship("relationB"); - Map nodeTemplateRequirement1 = new HashMap<>(); - Map nodeTemplateRequirement2 = new HashMap<>(); - nodeTemplateRequirement1.put("req1", reqAssignment1); - nodeTemplateRequirement2.put("req2", reqAssignment2); - nodeTemplate.setRequirements(new ArrayList<>()); - nodeTemplate.getRequirements().add(nodeTemplateRequirement1); - nodeTemplate.getRequirements().add(nodeTemplateRequirement2); - - Map nodeTemplateCapability = new HashMap<>(); - CapabilityAssignment capAss = new CapabilityAssignment(); - Map capProps = new HashMap<>(); - capProps.put("num_cpus", "{ get_input: cpus }"); - capAss.setProperties(capProps); - Map capAtts = new HashMap<>(); - capAtts.put("num_cpus", "66"); - capAss.setAttributes(capAtts); - nodeTemplateCapability.put("cap1", capAss); - nodeTemplate.setCapabilities(nodeTemplateCapability); - - NodeFilter nodeTemplateNodeFilter = new NodeFilter(); - Map> ntProp = new HashMap<>(); - Constraint c1 = new Constraint(); - c1.setEqual("1 MB"); - List consList = new ArrayList<>(); - consList.add(c1); - ntProp.put("test1", consList); - nodeTemplateNodeFilter.setProperties(ntProp); - nodeTemplate.setNode_filter(nodeTemplateNodeFilter); - nodeTemplates.put("firatNodeTemplate", nodeTemplate); - topologyTemplate.setNode_templates(nodeTemplates); - - SubstitutionMapping subMap = new SubstitutionMapping(); - subMap.setNode_type("myNodeType.node"); - Map> mapCapabilities = new HashMap<>(); - List NodeCap = new ArrayList<>(); - NodeCap.add("database"); - NodeCap.add("database_endpoint"); - mapCapabilities.put("database_endpoint", NodeCap); - subMap.setCapabilities(mapCapabilities); - topologyTemplate.setSubstitution_mappings(subMap); - serviceTemplate.setTopology_template(topologyTemplate); - - String yaml = new YamlUtil().objectToYaml(serviceTemplate); - ServiceTemplate serviceTemplateFromYaml = - new YamlUtil().yamlToObject(yaml, ServiceTemplate.class); - Assert.assertNotNull(serviceTemplateFromYaml); - } + private YamlUtil yamlUtil = new YamlUtil(); + private static final String INTERFACE_ID = "inter_1"; + private static final String NODE_TEMPLATE_ID = "firstNodeTemplate"; + private static final String NODE_TYPE_ID = "compute_node_type"; + private static final String BASE_DIR = "/mock/model"; + private static final String ST = "/serviceTemplate.yaml"; + private static final String ST_WITH_INTERFACE = "/serviceTemplateWithInterface.yaml"; + private static final String ST_WITH_OPERATIONS = "/serviceTemplateWithInterfaceAndOperation.yaml"; + private static final String ST_WITH_INTERFACE_DEF = "/serviceTemplateWithNodeTemplateInterface.yaml"; + private static final String ST_WITH_NODE_INTERFACE_DEF = "/serviceTemplateWithNodeTypeInterface.yaml"; + private static final String INTERFACE_TYPE_VALUE = "tosca.interfaces.node.lifecycle.Standard"; + private static final String OPERATION_START = "start"; + private static final String OPERATION_DESC = "start operation"; + private static final String IMPLEMENTATION_NAME = "startWorkFlow.json"; + private static final String PRIMARY_IMPL = "myImpl.yaml"; + private static final String DEPENDENCY_NAME = "script1.sh"; + private static final String STRING_TYPE = "string"; + + @Test + public void testServiceTemplateJavaToYaml() { + ServiceTemplate serviceTemplate = new ServiceTemplate(); + Map metadata = new HashMap<>(); + metadata.put("Template_author", "OPENECOMP"); + metadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "Test"); + metadata.put("Template_version", "1.0.0"); + serviceTemplate.setTosca_definitions_version("tosca_simple_yaml_1_0_0"); + serviceTemplate.setDescription("testing desc tosca service template"); + + Import fileImport1 = new Import(); + fileImport1.setFile("path1/path2/file1.yaml"); + Import fileImport2 = new Import(); + fileImport2.setFile("path1/path2/file2.yaml"); + List> imports = new ArrayList<>(); + Map importsMap = new HashMap<>(); + importsMap.put("myfile1", fileImport1); + imports.add(importsMap); + importsMap = new HashMap<>(); + importsMap.put("myfile2", fileImport2); + imports.add(importsMap); + serviceTemplate.setImports(imports); + + OperationDefinition operationDefinition = new OperationDefinition(); + operationDefinition.setDescription("test operation"); + InterfaceType interfaceType = new InterfaceType(); + interfaceType.setDerived_from("derived_from"); + interfaceType.setDescription("desc"); + interfaceType.addOperation("test", operationDefinition); + serviceTemplate.addInterfaceType("test_interface", interfaceType); + + ArtifactType artifact = new ArtifactType(); + artifact.setMime_type("application/java-archive"); + ArrayList ext = new ArrayList<>(); + ext.add("yaml"); + ext.add("xml"); + artifact.setFile_ext(ext); + Map artifactTypes = new HashMap<>(); + artifactTypes.put("one_artifact", artifact); + serviceTemplate.setArtifact_types(artifactTypes); + + NodeType nodeType = new NodeType(); + nodeType.setDerived_from("tosca.nodes.Root"); + nodeType.setVersion("1.0.0"); + nodeType.setDescription("tosca compute test"); + + PropertyDefinition propDef1 = new PropertyDefinition(); + propDef1.setType("integer"); + propDef1.setDescription("Number of CPUs requested for a software node instance"); + propDef1.setRequired(true); + propDef1.set_default(1); + + Constraint graterOrEqual = new Constraint(); + graterOrEqual.setGreater_or_equal((float) 5.0); + Constraint constraintEqual = new Constraint(); + constraintEqual.setEqual(5); + Constraint greater_than = new Constraint(); + greater_than.setGreater_than(6.02); + Constraint inRange = new Constraint(); + inRange.setIn_range(new Object[2]); + inRange.getIn_range()[0] = 0; + inRange.getIn_range()[1] = ToscaConstants.UNBOUNDED; + + List constraints = new ArrayList<>(); + constraints.add(graterOrEqual); + constraints.add(constraintEqual); + constraints.add(greater_than); + constraints.add(inRange); + propDef1.setConstraints(constraints); + + Map properties = new HashMap<>(); + properties.put("cpu_num", propDef1); + nodeType.setProperties(properties); + + Map attributesDef = new HashMap<>(); + AttributeDefinition attDef = new AttributeDefinition(); + attDef.setType(PropertyType.STRING.getDisplayName()); + attDef.set_default("hi"); + attributesDef.put("attDef1", attDef); + nodeType.setAttributes(attributesDef); + + Map reqsDef = new HashMap<>(); + RequirementDefinition reqDef = new RequirementDefinition(); + reqDef.setCapability("tosca.cap1"); + reqDef.getOccurrences()[0] = 5; + reqsDef.put("re1", reqDef); + List> reqList = new ArrayList<>(); + reqList.add(reqsDef); + nodeType.setRequirements(reqList); + + + Map capsDef = new HashMap<>(); + CapabilityDefinition capdef = new CapabilityDefinition(); + capdef.setType("tosca.cap"); + List vvSource = new ArrayList<>(); + vvSource.add("node1"); + vvSource.add("node2"); + capdef.setValid_source_types(vvSource); + capsDef.put("cap1", capdef); + nodeType.setCapabilities(capsDef); + + Map nodeTypes = new HashMap<>(); + nodeTypes.put("compute_node_type", nodeType); + serviceTemplate.setNode_types(nodeTypes); + + TopologyTemplate topologyTemplate = new TopologyTemplate(); + topologyTemplate.setDescription("topologi template descroption"); + Map inputs = new HashMap<>(); + ParameterDefinition paramDef = new ParameterDefinition(); + paramDef.setType(PropertyType.STRING.getDisplayName()); + paramDef.setDescription("desc"); + paramDef.set_default("my default val"); + paramDef.setRequired(false); + paramDef.setEntry_schema(DataModelUtil.createEntrySchema("tosca.myType", null, null)); + List paramConstraint = new ArrayList<>(); + Constraint paramConst1 = new Constraint(); + paramConst1.setGreater_than(6); + Constraint paramConst2 = new Constraint(); + paramConst2.setGreater_or_equal(9); + paramConstraint.add(paramConst1); + paramConstraint.add(paramConst2); + paramDef.setConstraints(paramConstraint); + inputs.put("inParam1", paramDef); + topologyTemplate.setInputs(inputs); + + Map nodeTemplates = new HashMap<>(); + NodeTemplate nodeTemplate = new NodeTemplate(); + nodeTemplate.setType("nodeTypeRef"); + List directives = new ArrayList<>(); + directives.add(Directive.SELECTABLE.getDisplayName()); + directives.add(Directive.SUBSTITUTABLE.getDisplayName()); + nodeTemplate.setDirectives(directives); + Map nodeTemplateProperties = new HashMap<>(); + nodeTemplateProperties.put("prop1", "abcd"); + nodeTemplateProperties.put("prop2", "{ get_input: my_mysql_rootpw }"); + nodeTemplate.setProperties(nodeTemplateProperties); + Map nodeTemplateAtts = new HashMap<>(); + nodeTemplateAtts.put("att1", "att1Val"); + nodeTemplateAtts.put("att2", "{ get_input: my_mysql_rootpw }"); + nodeTemplate.setAttributes(nodeTemplateAtts); + + + RequirementAssignment reqAssignment1 = new RequirementAssignment(); + reqAssignment1.setNode("nodeA"); + reqAssignment1.setCapability("capA"); + reqAssignment1.setRelationship("relationB"); + Object[] reqAssOccurrences = new Object[2]; + reqAssOccurrences[0] = 1; + reqAssOccurrences[1] = 2; + reqAssignment1.setOccurrences(reqAssOccurrences); + NodeFilter reqNodeFilter = new NodeFilter(); + List propConstraint1 = new ArrayList<>(); + Constraint propConst1 = new Constraint(); + propConst1.setGreater_or_equal(9); + propConstraint1.add(propConst1); + List propConstraint2 = new ArrayList<>(); + Constraint propConst2 = new Constraint(); + propConst2.setMin_length(1); + propConstraint2.add(propConst2); + Constraint propConst3 = new Constraint(); + propConst3.setMax_length(2); + propConstraint2.add(propConst3); + List>> nodeFilterProp = new ArrayList<>(); + Map> propsMap = new HashMap<>(); + propsMap.put("propName1", propConstraint1); + propsMap.put("propName2", propConstraint2); + nodeFilterProp.add(propsMap); + reqNodeFilter.setProperties(nodeFilterProp); + reqAssignment1.setNode_filter(reqNodeFilter); + + RequirementAssignment reqAssignment2 = new RequirementAssignment(); + reqAssignment2.setNode("nodeA"); + reqAssignment2.setCapability("capA"); + reqAssignment2.setRelationship("relationB"); + Map nodeTemplateRequirement1 = new HashMap<>(); + Map nodeTemplateRequirement2 = new HashMap<>(); + nodeTemplateRequirement1.put("req1", reqAssignment1); + nodeTemplateRequirement2.put("req2", reqAssignment2); + nodeTemplate.setRequirements(new ArrayList<>()); + nodeTemplate.getRequirements().add(nodeTemplateRequirement1); + nodeTemplate.getRequirements().add(nodeTemplateRequirement2); + + Map nodeTemplateCapability = new HashMap<>(); + CapabilityAssignment capAss = new CapabilityAssignment(); + Map capProps = new HashMap<>(); + capProps.put("num_cpus", "{ get_input: cpus }"); + capAss.setProperties(capProps); + Map capAtts = new HashMap<>(); + capAtts.put("num_cpus", "66"); + capAss.setAttributes(capAtts); + nodeTemplateCapability.put("cap1", capAss); + nodeTemplate.setCapabilities(nodeTemplateCapability); + + NodeFilter nodeTemplateNodeFilter = new NodeFilter(); + List>> nodeFilterProp2 = new ArrayList<>(); + Map> propsMap2 = new HashMap<>(); + Constraint c1 = new Constraint(); + c1.setEqual("1 MB"); + List consList = new ArrayList<>(); + consList.add(c1); + propsMap2.put("test1", consList); + nodeFilterProp2.add(propsMap2); + nodeTemplateNodeFilter.setProperties(nodeFilterProp2); + nodeTemplate.setNode_filter(nodeTemplateNodeFilter); + nodeTemplates.put("firatNodeTemplate", nodeTemplate); + topologyTemplate.setNode_templates(nodeTemplates); + + SubstitutionMapping subMap = new SubstitutionMapping(); + subMap.setNode_type("myNodeType.node"); + Map> mapCapabilities = new HashMap<>(); + List NodeCap = new ArrayList<>(); + NodeCap.add("database"); + NodeCap.add("database_endpoint"); + mapCapabilities.put("database_endpoint", NodeCap); + subMap.setCapabilities(mapCapabilities); + topologyTemplate.setSubstitution_mappings(subMap); + serviceTemplate.setTopology_template(topologyTemplate); + + String yaml = new YamlUtil().objectToYaml(serviceTemplate); + ServiceTemplate serviceTemplateFromYaml = new YamlUtil().yamlToObject(yaml, ServiceTemplate.class); + Assert.assertNotNull(serviceTemplateFromYaml); + } @Test @@ -312,183 +309,171 @@ public class ToscaModelTest { Assert.assertNotNull(serviceTemplateFromYaml); } - @Test - public void testYamlWithInterfaceToServiceTemplateObj() throws IOException { - ServiceTemplate serviceTemplateWithOperation = - getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); - Assert.assertNotNull(serviceTemplateWithOperation); - - InterfaceType expectedInterfaceType = createInterfaceType(); - - Map interfaceTypes = - DataModelUtil.getInterfaceTypes(serviceTemplateWithOperation); - Assert.assertEquals(1, interfaceTypes.size()); - InterfaceType actualInterfaceType = interfaceTypes.get(INTERFACE_ID); - Assert.assertEquals(expectedInterfaceType, actualInterfaceType); - } + @Test + public void testYamlWithInterfaceToServiceTemplateObj() throws IOException { + ServiceTemplate serviceTemplateWithOperation = getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); + Assert.assertNotNull(serviceTemplateWithOperation); - @Test - public void testAddOperationToInterface() throws IOException { - YamlUtil yamlUtil = new YamlUtil(); - ServiceTemplate serviceTemplateWithInterface = - getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE); - ServiceTemplate serviceTemplateWithOperation = - getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); - - OperationDefinition operationDefinition = createOperationDefinition(); - - DataModelUtil - .addInterfaceOperation(serviceTemplateWithInterface, INTERFACE_ID, OPERATION_START, - operationDefinition); - String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithOperation); - String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterface); - Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate); - } + InterfaceType expectedInterfaceType = createInterfaceType(); - @Test - public void testInterfaceTypeToObjConversion() throws IOException { - ServiceTemplate serviceTemplateWithInterface = - getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE); - ServiceTemplate serviceTemplateWithOperation = - getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); - InterfaceType interfaceType = createInterfaceType(); - - Optional interfaceAsObj = DataModelUtil.convertInterfaceTypeToObj(interfaceType); - Assert.assertTrue(interfaceAsObj.isPresent()); - - Map interfaceTypes = new HashMap<>(); - interfaceTypes.put(INTERFACE_ID, interfaceAsObj.get()); - serviceTemplateWithInterface.setInterface_types(interfaceTypes); - - String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithOperation); - String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterface); - Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate); - } + Map interfaceTypes = DataModelUtil.getInterfaceTypes(serviceTemplateWithOperation); + Assert.assertEquals(1, interfaceTypes.size()); + InterfaceType actualInterfaceType = interfaceTypes.get(INTERFACE_ID); + Assert.assertEquals(expectedInterfaceType, actualInterfaceType); + } - @Test - public void testObjToInterfaceTypeConversion() throws IOException, ReflectiveOperationException { - ServiceTemplate serviceTemplateWithOperation = - getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); - Map interfaceTypes = serviceTemplateWithOperation.getInterface_types(); - Object interfaceObj = interfaceTypes.get(INTERFACE_ID); - Optional actualInterfaceType = - DataModelUtil.convertObjToInterfaceType(INTERFACE_ID, interfaceObj); - - Assert.assertTrue(actualInterfaceType.isPresent()); - InterfaceType expectedInterfaceType = createInterfaceType(); - Assert.assertEquals(expectedInterfaceType, actualInterfaceType.get()); - } + @Test + public void testAddOperationToInterface() throws IOException { + YamlUtil yamlUtil = new YamlUtil(); + ServiceTemplate serviceTemplateWithInterface = getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE); + ServiceTemplate serviceTemplateWithOperation = getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); - @Test - public void testObjToInterfaceDefinitionTypeConversion() - throws IOException, ReflectiveOperationException { - ServiceTemplate serviceTemplateWithInterfaceDef = - getServiceTemplate(BASE_DIR + ST_WITH_NODE_INTERFACE_DEF); - NodeType nodeTypeWithInterface = - DataModelUtil.getNodeType(serviceTemplateWithInterfaceDef, NODE_TYPE_ID); - Map interfaces = nodeTypeWithInterface.getInterfaces(); - Object interfaceObj = interfaces.get(INTERFACE_ID); - - Optional actualInterfaceDefinition = - DataModelUtil.convertObjToInterfaceDefinition(INTERFACE_ID, interfaceObj, - InterfaceDefinitionType.class); - - Assert.assertTrue(actualInterfaceDefinition.isPresent()); - - InterfaceDefinitionType expectedInterfaceDefinitionType = createInterfaceDefinitionType(); - Assert.assertEquals(expectedInterfaceDefinitionType, actualInterfaceDefinition.get()); - } + OperationDefinition operationDefinition = createOperationDefinition(); - @Test - public void testObjToInterfaceDefinitionTemplateConversion() - throws IOException, ReflectiveOperationException { - ServiceTemplate serviceTemplateWithInterfaceDef = - getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE_DEF); - NodeTemplate nodeTemplateWithInterface = - DataModelUtil.getNodeTemplate(serviceTemplateWithInterfaceDef, NODE_TEMPLATE_ID); - Map interfaces = nodeTemplateWithInterface.getInterfaces(); - Object interfaceObj = interfaces.get(INTERFACE_ID); - - Optional actualInterfaceDefinition = - DataModelUtil.convertObjToInterfaceDefinition(INTERFACE_ID, interfaceObj, - InterfaceDefinitionTemplate.class); - - Assert.assertTrue(actualInterfaceDefinition.isPresent()); - InterfaceDefinitionTemplate expectedInterfaceDefinitionTemplate = createInterfaceDefinitionTemplate(); - Assert.assertEquals(expectedInterfaceDefinitionTemplate, actualInterfaceDefinition.get()); - } + DataModelUtil.addInterfaceOperation(serviceTemplateWithInterface, INTERFACE_ID, OPERATION_START, + operationDefinition); + String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithOperation); + String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterface); + Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate); + } - @Test - public void testYamlToServiceTemplateIncludingHeatExtend() throws IOException { - ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - try (InputStream yamlFile = - toscaExtensionYamlUtil.loadYamlFileIs(BASE_DIR + "/serviceTemplateHeatExtend.yaml")) { - ServiceTemplate serviceTemplateFromYaml = - toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); - ParameterDefinitionExt parameterDefinitionExt = - (ParameterDefinitionExt) serviceTemplateFromYaml.getTopology_template().getInputs() - .get("inParam1"); - Assert.assertNotNull(parameterDefinitionExt.getLabel()); - String backToYamlString = toscaExtensionYamlUtil.objectToYaml(serviceTemplateFromYaml); - Assert.assertNotNull(backToYamlString); + @Test + public void testInterfaceTypeToObjConversion() throws IOException { + ServiceTemplate serviceTemplateWithInterface = getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE); + ServiceTemplate serviceTemplateWithOperation = getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); + InterfaceType interfaceType = createInterfaceType(); + + Optional interfaceAsObj = DataModelUtil.convertInterfaceTypeToObj(interfaceType); + Assert.assertTrue(interfaceAsObj.isPresent()); + + Map interfaceTypes = new HashMap<>(); + interfaceTypes.put(INTERFACE_ID, interfaceAsObj.get()); + serviceTemplateWithInterface.setInterface_types(interfaceTypes); + + String expectedServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithOperation); + String actualServiceTemplate = yamlUtil.objectToYaml(serviceTemplateWithInterface); + Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate); } - } - private ServiceTemplate getServiceTemplate(String inputPath) throws IOException { - try (InputStream yamlFile = yamlUtil.loadYamlFileIs(inputPath)) { - return yamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + @Test + public void testObjToInterfaceTypeConversion() throws IOException, ReflectiveOperationException { + ServiceTemplate serviceTemplateWithOperation = getServiceTemplate(BASE_DIR + ST_WITH_OPERATIONS); + Map interfaceTypes = serviceTemplateWithOperation.getInterface_types(); + Object interfaceObj = interfaceTypes.get(INTERFACE_ID); + Optional actualInterfaceType = + DataModelUtil.convertObjToInterfaceType(INTERFACE_ID, interfaceObj); + + Assert.assertTrue(actualInterfaceType.isPresent()); + InterfaceType expectedInterfaceType = createInterfaceType(); + Assert.assertEquals(expectedInterfaceType, actualInterfaceType.get()); } - } - private InterfaceType createInterfaceType() { - OperationDefinition operationDefinition = createOperationDefinition(); - InterfaceType interfaceType = new InterfaceType(); - interfaceType.setDescription("test interface"); - interfaceType.addOperation(OPERATION_START, operationDefinition); - return interfaceType; - } + @Test + public void testObjToInterfaceDefinitionTypeConversion() throws IOException, ReflectiveOperationException { + ServiceTemplate serviceTemplateWithInterfaceDef = getServiceTemplate(BASE_DIR + ST_WITH_NODE_INTERFACE_DEF); + NodeType nodeTypeWithInterface = DataModelUtil.getNodeType(serviceTemplateWithInterfaceDef, NODE_TYPE_ID); + Map interfaces = nodeTypeWithInterface.getInterfaces(); + Object interfaceObj = interfaces.get(INTERFACE_ID); - private OperationDefinition createOperationDefinition() { - OperationDefinition operationDefinition = new OperationDefinition(); - operationDefinition.setDescription(OPERATION_DESC); - return operationDefinition; - } + Optional actualInterfaceDefinition = DataModelUtil + .convertObjToInterfaceDefinition( + INTERFACE_ID, interfaceObj, + InterfaceDefinitionType.class); - private InterfaceDefinitionType createInterfaceDefinitionType() { - OperationDefinitionType operationDefinitionType = createOperationDefinitionType(); - InterfaceDefinitionType interfaceDefinitionType = new InterfaceDefinitionType(); - interfaceDefinitionType.setType(INTERFACE_TYPE_VALUE); - interfaceDefinitionType.addOperation(OPERATION_START, operationDefinitionType); - return interfaceDefinitionType; - } + Assert.assertTrue(actualInterfaceDefinition.isPresent()); - private InterfaceDefinitionTemplate createInterfaceDefinitionTemplate() { - OperationDefinitionTemplate operationDefinitionTemplate = createOperationDefinitionTemplate(); - InterfaceDefinitionTemplate interfaceDefinitionTemplate = new InterfaceDefinitionTemplate(); - interfaceDefinitionTemplate.addOperation(OPERATION_START, operationDefinitionTemplate); - return interfaceDefinitionTemplate; - } + InterfaceDefinitionType expectedInterfaceDefinitionType = createInterfaceDefinitionType(); + Assert.assertEquals(expectedInterfaceDefinitionType, actualInterfaceDefinition.get()); + } - private OperationDefinitionTemplate createOperationDefinitionTemplate() { - OperationDefinitionTemplate operationDefinitionTemplate = new OperationDefinitionTemplate(); - operationDefinitionTemplate.setDescription(OPERATION_DESC); - Implementation implementation = new Implementation(); - implementation.setPrimary(PRIMARY_IMPL); - List dependencies = new ArrayList<>(); - dependencies.add(DEPENDENCY_NAME); - implementation.setDependencies(dependencies); - operationDefinitionTemplate.setImplementation(implementation); - return operationDefinitionTemplate; - } + @Test + public void testObjToInterfaceDefinitionTemplateConversion() throws IOException, ReflectiveOperationException { + ServiceTemplate serviceTemplateWithInterfaceDef = getServiceTemplate(BASE_DIR + ST_WITH_INTERFACE_DEF); + NodeTemplate nodeTemplateWithInterface = + DataModelUtil.getNodeTemplate(serviceTemplateWithInterfaceDef, NODE_TEMPLATE_ID); + Map interfaces = nodeTemplateWithInterface.getInterfaces(); + Object interfaceObj = interfaces.get(INTERFACE_ID); + + Optional actualInterfaceDefinition = DataModelUtil + .convertObjToInterfaceDefinition( + INTERFACE_ID, interfaceObj, + InterfaceDefinitionTemplate.class); + + Assert.assertTrue(actualInterfaceDefinition.isPresent()); + InterfaceDefinitionTemplate expectedInterfaceDefinitionTemplate = createInterfaceDefinitionTemplate(); + Assert.assertEquals(expectedInterfaceDefinitionTemplate, actualInterfaceDefinition.get()); + } - private OperationDefinitionType createOperationDefinitionType() { - OperationDefinitionType operationDefinitionType = new OperationDefinitionType(); - operationDefinitionType.setDescription(OPERATION_DESC); - operationDefinitionType.setImplementation(IMPLEMENTATION_NAME); - PropertyDefinition propertyDefinition = new PropertyDefinition(); - propertyDefinition.setType(STRING_TYPE); - return operationDefinitionType; - } + @Test + public void testYamlToServiceTemplateIncludingHeatExtend() throws IOException { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + try (InputStream yamlFile = toscaExtensionYamlUtil + .loadYamlFileIs(BASE_DIR + "/serviceTemplateHeatExtend.yaml")) { + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + ParameterDefinitionExt parameterDefinitionExt = + (ParameterDefinitionExt) serviceTemplateFromYaml.getTopology_template().getInputs().get("inParam1"); + Assert.assertNotNull(parameterDefinitionExt.getLabel()); + String backToYamlString = toscaExtensionYamlUtil.objectToYaml(serviceTemplateFromYaml); + Assert.assertNotNull(backToYamlString); + } + } + + private ServiceTemplate getServiceTemplate(String inputPath) throws IOException { + try (InputStream yamlFile = yamlUtil.loadYamlFileIs(inputPath)) { + return yamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + } + } + + private InterfaceType createInterfaceType() { + OperationDefinition operationDefinition = createOperationDefinition(); + InterfaceType interfaceType = new InterfaceType(); + interfaceType.setDescription("test interface"); + interfaceType.addOperation(OPERATION_START, operationDefinition); + return interfaceType; + } + + private OperationDefinition createOperationDefinition() { + OperationDefinition operationDefinition = new OperationDefinition(); + operationDefinition.setDescription(OPERATION_DESC); + return operationDefinition; + } + + private InterfaceDefinitionType createInterfaceDefinitionType() { + OperationDefinitionType operationDefinitionType = createOperationDefinitionType(); + InterfaceDefinitionType interfaceDefinitionType = new InterfaceDefinitionType(); + interfaceDefinitionType.setType(INTERFACE_TYPE_VALUE); + interfaceDefinitionType.addOperation(OPERATION_START, operationDefinitionType); + return interfaceDefinitionType; + } + + private InterfaceDefinitionTemplate createInterfaceDefinitionTemplate() { + OperationDefinitionTemplate operationDefinitionTemplate = createOperationDefinitionTemplate(); + InterfaceDefinitionTemplate interfaceDefinitionTemplate = new InterfaceDefinitionTemplate(); + interfaceDefinitionTemplate.addOperation(OPERATION_START, operationDefinitionTemplate); + return interfaceDefinitionTemplate; + } + + private OperationDefinitionTemplate createOperationDefinitionTemplate() { + OperationDefinitionTemplate operationDefinitionTemplate = new OperationDefinitionTemplate(); + operationDefinitionTemplate.setDescription(OPERATION_DESC); + Implementation implementation = new Implementation(); + implementation.setPrimary(PRIMARY_IMPL); + List dependencies = new ArrayList<>(); + dependencies.add(DEPENDENCY_NAME); + implementation.setDependencies(dependencies); + operationDefinitionTemplate.setImplementation(implementation); + return operationDefinitionTemplate; + } + + private OperationDefinitionType createOperationDefinitionType() { + OperationDefinitionType operationDefinitionType = new OperationDefinitionType(); + operationDefinitionType.setDescription(OPERATION_DESC); + operationDefinitionType.setImplementation(IMPLEMENTATION_NAME); + PropertyDefinition propertyDefinition = new PropertyDefinition(); + propertyDefinition.setType(STRING_TYPE); + return operationDefinitionType; + } } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/model/NodeFilterTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/model/NodeFilterTest.java new file mode 100644 index 0000000000..a3fd56f3c6 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/datatypes/model/NodeFilterTest.java @@ -0,0 +1,144 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.tosca.datatypes.model; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.junit.Assert; +import org.junit.Test; +import org.onap.sdc.tosca.datatypes.model.CapabilityFilter; +import org.onap.sdc.tosca.datatypes.model.Constraint; +import org.onap.sdc.tosca.datatypes.model.NodeFilter; +import org.onap.sdc.tosca.services.YamlUtil; + +/** + * @author KATYR + * @since May 10, 2018 + */ + +public class NodeFilterTest { + + private static final String GET_INPUT = "get_input"; + + @Test + public void nodeFilterToYamlAndBack() { + NodeFilter nodeFilter = new NodeFilter(); + nodeFilter.setProperties(createTestProperties()); + nodeFilter.setCapabilities(createTestCapabilities()); + + String yamlString = new YamlUtil().objectToYaml(nodeFilter); + + NodeFilter nodeFilterFromYaml = + new YamlUtil().yamlToObject(yamlString, NodeFilter.class); + Assert.assertNotNull(nodeFilterFromYaml); + } + + + private List>> createTestProperties() { + List>> propertyFilterDefinitions = new ArrayList<>(); + Map> pfd1 = new HashMap<>(); + List constraints = new ArrayList<>(); + constraints.add(createConstraint("siteName", true)); + constraints.add(createConstraint("siteName1", false)); + pfd1.put("name1", constraints); + + + Map> pfd2 = new HashMap<>(); + List constraints2 = new ArrayList<>(); + constraints2.add(createConstraint("input", true)); + constraints2.add(createConstraint("output", false)); + pfd2.put("name", constraints2); + + + propertyFilterDefinitions.add(pfd1); + propertyFilterDefinitions.add(pfd2); + + return propertyFilterDefinitions; + + } + + private Constraint createConstraint(String constraintParam, boolean createEqualityConstraint) { + Constraint constraint = new Constraint(); + + if (createEqualityConstraint) { + constraint.setEqual(createValue(constraintParam)); + } else { + constraint.setGreater_than(createValue(constraintParam)); + + } + return constraint; + } + + private static Map createValue(String propertyName) { + Map getPropertyMap = new HashMap<>(); + getPropertyMap.put(GET_INPUT, propertyName); + + return getPropertyMap; + } + + + + private List> createTestCapabilities() { + List> capabilities = new ArrayList<>(); + Map outerMap1 = new HashMap<>(); + Map> innerMap1 = new HashMap<>(); + + CapabilityFilter filterProperties1 = new CapabilityFilter(); + innerMap1.put("queue_model", createConstraints(3)); + List>> innerList1 = new ArrayList<>(); + innerList1.add(innerMap1); + filterProperties1.setProperties(innerList1); + outerMap1.put("diffserv1", filterProperties1); + + Map outerMap2 = new HashMap<>(); + CapabilityFilter filterProperties2 = new CapabilityFilter(); + Map> innerMap2 = new HashMap<>(); + Map> innerMap3 = new HashMap<>(); + innerMap2.put("queue_model", createConstraints(2)); + innerMap3.put("queue_model2", createConstraints(2)); + List>> innerList2 = new ArrayList<>(); + innerList2.add(innerMap2); + innerList2.add(innerMap3); + filterProperties2.setProperties(innerList2); + outerMap2.put("diffserv2", filterProperties2); + + capabilities.add(outerMap1); + capabilities.add(outerMap2); + + + return capabilities; + } + + private List createConstraints(int length) { + List constraints = new ArrayList<>(); + for (int i = 0; i < length; i++) { + if (i / 2 == 0) { + constraints.add(createConstraint("siteName" + UUID.randomUUID().getMostSignificantBits(), true)); + } else { + constraints.add(createConstraint("siteName" + UUID.randomUUID().getLeastSignificantBits(), false)); + } + + } + + return constraints; + + } + +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplate.yaml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplate.yaml index a2bc15ea19..c93061996b 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplate.yaml +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplate.yaml @@ -85,10 +85,10 @@ topology_template: node: nodeA relationship: relationB node_filter: - properties: - propName1: + properties: + - propName1: - greater_or_equal: 9 - propName2: + - propName2: - min_length: 1 - max_length: 2 occurrences: diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateHeatExtend.yaml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateHeatExtend.yaml index ac349313c2..d027a4aa5a 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateHeatExtend.yaml +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateHeatExtend.yaml @@ -91,11 +91,11 @@ topology_template: relationship: relationB node_filter: properties: - propName1: - - greater_or_equal: 9 - propName2: - - min_length: 1 - - max_length: 2 + - propName1: + - greater_or_equal: 9 + - propName2: + - min_length: 1 + - max_length: 2 occurrences: - 1 - 2 @@ -109,10 +109,10 @@ topology_template: num_cpus: '{ get_input: cpus }' attributes: num_cpus: '66' - node_filter: - properties: - test1: - - equal: 1 MB + node_filter: + properties: + -test1: + - equal: 1 MB substitution_mappings: node_type: myNodeType.node capabilities: -- cgit 1.2.3-korg