diff options
author | Pavel Aharoni <pa0916@att.com> | 2017-06-26 17:32:09 +0300 |
---|---|---|
committer | Pavel Aharoni <pa0916@att.com> | 2017-06-26 17:32:41 +0300 |
commit | 53b4ab6e88e4e7359534e6ca84e884005c7153b4 (patch) | |
tree | 4236812d294e1ae1a0ca6651f0db54e993be69b2 | |
parent | b29c5c244b23bbf549bc4e7637019e28ed48b0c4 (diff) |
[SDC-39] filter nt props values by string
Change-Id: Ic03853b58e97bfcacf1f82449fb4f498acfc347c
Signed-off-by: Pavel Aharoni <pa0916@att.com>
-rw-r--r-- | README.md | 17 | ||||
-rw-r--r-- | pom.xml | 4 | ||||
-rw-r--r-- | src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java | 14 | ||||
-rw-r--r-- | src/main/java/org/openecomp/sdc/tosca/parser/impl/FilterType.java | 26 | ||||
-rw-r--r-- | src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java | 63 | ||||
-rw-r--r-- | src/test/java/org/openecomp/sdc/impl/ToscaParserNodeTemplateTest.java | 84 |
6 files changed, 203 insertions, 5 deletions
@@ -1,5 +1,5 @@ # OpenECOMP SDC-Tosca -
+ --- --- @@ -26,3 +26,18 @@ SDC Javadoc and Maven site *** to be completed on rrelease *** +# Release notes for versions + +1.1.31-SNAPSHOT + +Initial after separating into separate repo + + + +------------------------------- + +1.1.1-SNAPSHOT + +Added toString of Function (GetInput, etc.) + +Allowed two arguments for GetInput - name of list input and index in list @@ -7,7 +7,7 @@ <artifactId>sdc-tosca</artifactId>
<name>SDC Tosca Parser</name>
<description>SDC Tosca Parser JAR file for use by consumers</description>
- <version>1.1.31-SNAPSHOT</version>
+ <version>1.1.32-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
@@ -97,7 +97,7 @@ <dependency>
<groupId>org.openecomp.sdc.jtosca</groupId>
<artifactId>jtosca</artifactId>
- <version>1.1.0-SNAPSHOT</version>
+ <version>1.1.1-SNAPSHOT</version>
</dependency>
diff --git a/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java b/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java index eeb23b4..83bd29d 100644 --- a/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java +++ b/src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Map; import org.apache.commons.lang3.tuple.Pair; +import org.openecomp.sdc.tosca.parser.impl.FilterType; import org.openecomp.sdc.tosca.parser.impl.SdcTypes; import org.openecomp.sdc.toscaparser.api.Group; import org.openecomp.sdc.toscaparser.api.NodeTemplate; @@ -335,6 +336,15 @@ public interface ISdcCsarHelper { */ public String getNodeTemplateCustomizationUuid(NodeTemplate nt); + /** + * Filter Node Template properties equals/contains specific pattern + * @param nodeTemplate Node Template to filter its properties + * @param filterType filter by equals/contains + * @param pattern value to filter with it + * @return Map <b>full path to a property</b> mapped to <b>property value<b/> filtered by type & pattern + */ + public Map<String, Object> filterNodeTemplatePropertiesByValue(NodeTemplate nodeTemplate, FilterType filterType, String pattern); + /** * Get all node templates by sdcType for parent Node Template. * @@ -342,7 +352,7 @@ public interface ISdcCsarHelper { * @param sdcType - the SDC type of the node. * @return node templates of this SDC type. */ - List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate parentNodeTemplate, SdcTypes sdcType); + public List<NodeTemplate> getNodeTemplateBySdcType(NodeTemplate parentNodeTemplate, SdcTypes sdcType); /** * Get all node templates by sdcType for this CSAR service. @@ -350,5 +360,5 @@ public interface ISdcCsarHelper { * @param sdcType - the SDC type of the node. * @return service node templates of this SDC type. */ - List<NodeTemplate> getServiceNodeTemplateBySdcType(SdcTypes sdcType); + public List<NodeTemplate> getServiceNodeTemplateBySdcType(SdcTypes sdcType); } diff --git a/src/main/java/org/openecomp/sdc/tosca/parser/impl/FilterType.java b/src/main/java/org/openecomp/sdc/tosca/parser/impl/FilterType.java new file mode 100644 index 0000000..0f7810f --- /dev/null +++ b/src/main/java/org/openecomp/sdc/tosca/parser/impl/FilterType.java @@ -0,0 +1,26 @@ +package org.openecomp.sdc.tosca.parser.impl; + +public enum FilterType { + + CONTAINS("contains"){ + @Override + public boolean isMatch(String value, String pattern) { + return value.contains(pattern); + } + }, + EQUALS("equals"){ + @Override + public boolean isMatch(String value, String pattern) { + return value.equals(pattern); + } + }; + + String filterName; + + FilterType(String name) { + this.filterName = name; + } + + public abstract boolean isMatch(String value, String pattern); + +} diff --git a/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java b/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java index 1c4b71b..4a3f73e 100644 --- a/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java +++ b/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java @@ -42,6 +42,7 @@ import org.openecomp.sdc.toscaparser.api.ToscaTemplate; import org.openecomp.sdc.toscaparser.api.elements.Metadata; import org.openecomp.sdc.toscaparser.api.elements.NodeType; import org.openecomp.sdc.toscaparser.api.functions.Function; +import org.openecomp.sdc.toscaparser.api.functions.GetInput; import org.openecomp.sdc.toscaparser.api.parameters.Input; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -576,6 +577,68 @@ public class SdcCsarHelperImpl implements ISdcCsarHelper { return new ArrayList<>(); } + public Map<String, Object> filterNodeTemplatePropertiesByValue(NodeTemplate nodeTemplate, FilterType filterType, String pattern) { + Map<String, Object> filterMap = new HashMap<>(); + + if (nodeTemplate == null) { + log.error("filterNodeTemplatePropertiesByValue nodeTemplate is null"); + return filterMap; + } + + if (filterType == null) { + log.error("filterNodeTemplatePropertiesByValue filterType is null"); + return filterMap; + } + + if (GeneralUtility.isEmptyString(pattern)) { + log.error("filterNodeTemplatePropertiesByValue pattern string is empty"); + return filterMap; + } + + Map<String, Property> ntProperties = nodeTemplate.getProperties(); + + if (ntProperties != null && ntProperties.size() > 0) { + + for (Property current : ntProperties.values()) { + filterProperties(current.getValue(), current.getName(), filterType, pattern, filterMap); + } + } + + log.trace("filterNodeTemplatePropertiesByValue - filterMap value: {}", filterMap); + + return filterMap; + } + + /************************************* helper functions ***********************************/ + private Map<String, Object> filterProperties(Object property, String path, FilterType filterType, String pattern, Map<String, Object> filterMap) { + + if (property instanceof Map) { + for (Map.Entry<String, Object> item: ((Map<String, Object>) property).entrySet()) { + if (item.getKey().equals("get_input")) { + String itemToStr = item.getKey() + ":" + item.getValue().toString(); + filterProperties(itemToStr, path, filterType, pattern, filterMap); + } else { + path += PATH_DELIMITER + item.getKey(); + filterProperties(item.getValue(), path, filterType, pattern, filterMap); + } + } + } else if (property instanceof List) { + for (Object item: (List<Object>)property) { + filterProperties(item, path, filterType, pattern, filterMap); + } + } else if (property instanceof Function) { + if (filterType.isMatch(property.toString(), pattern)) { + filterMap.put(path, property); + } + } else { + if (filterType.isMatch(property.toString(), pattern)) { + filterMap.put(path, property); + } + } + + return filterMap; + } + public List<NodeTemplate> getServiceNodeTemplateBySdcType(SdcTypes sdcType) { if (sdcType == null) { log.error("getServiceNodeTemplateBySdcType - sdcType is null or empty"); diff --git a/src/test/java/org/openecomp/sdc/impl/ToscaParserNodeTemplateTest.java b/src/test/java/org/openecomp/sdc/impl/ToscaParserNodeTemplateTest.java index f9cedf3..4ce3db4 100644 --- a/src/test/java/org/openecomp/sdc/impl/ToscaParserNodeTemplateTest.java +++ b/src/test/java/org/openecomp/sdc/impl/ToscaParserNodeTemplateTest.java @@ -12,6 +12,7 @@ import java.util.Map; import org.apache.commons.lang3.tuple.Pair; import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException; +import org.openecomp.sdc.tosca.parser.impl.FilterType; import org.openecomp.sdc.tosca.parser.impl.SdcTypes; import org.openecomp.sdc.toscaparser.api.Group; import org.openecomp.sdc.toscaparser.api.NodeTemplate; @@ -395,6 +396,56 @@ public class ToscaParserNodeTemplateTest extends SdcToscaParserBasicTest { } //endregion + //region filterNodeTemplatePropertiesByValue + @Test + public void testFilterNodeTemplatePropertiesByContains() { + List<NodeTemplate> vfcs = complexCps.getVfcListByVf("f999e2ca-72c0-42d3-9b11-13f2122fb8ef"); + boolean isChecked = false; + for (NodeTemplate vfc: vfcs) { + if(vfc.getName().equalsIgnoreCase("abstract_ddc")) + { + isChecked = true; + Map<String, Object> filteredInputs = complexCps.filterNodeTemplatePropertiesByValue(vfc, FilterType.CONTAINS, "get_input"); + + assertEquals(16, filteredInputs.size()); + assertEquals("get_input:vnf_id", filteredInputs.get("compute_ddc_metadata#vf_module_id#vnf_id")); + assertEquals("get_input:[ddc_int_imbl_v6_ips, 3]", filteredInputs.get("port_ddc_int_imbl__port_fixed_ips#ip_address")); + + break; + } + + } + assertTrue(isChecked); + } + + @Test + public void testFilterNodeTemplatePropertiesByDummyContains() { + List<NodeTemplate> vfcs = complexCps.getVfcListByVf("f999e2ca-72c0-42d3-9b11-13f2122fb8ef"); + Map<String, Object> filteredInputs = complexCps.filterNodeTemplatePropertiesByValue(vfcs.get(0), FilterType.CONTAINS, "dummy"); + assertNotNull(filteredInputs); + assertEquals(0, filteredInputs.size()); + } + + @Test + public void testFilterNodeTemplatePropertiesByEquals() { + List<NodeTemplate> vfcs = complexCps.getVfcListByVf("f999e2ca-72c0-42d3-9b11-13f2122fb8ef"); + boolean isChecked = false; + for (NodeTemplate vfc: vfcs) { + if(vfc.getName().equalsIgnoreCase("abstract_ddc")) + { + isChecked = true; + Map<String, Object> filteredInputs = complexCps.filterNodeTemplatePropertiesByValue(vfc, FilterType.EQUALS, "ddc"); + + assertEquals(2, filteredInputs.size()); + assertEquals("ddc", filteredInputs.get("vm_type_tag")); + assertEquals("ddc", filteredInputs.get("nfc_naming_code")); + break; + } + + } + assertTrue(isChecked); + } + //region getServiceNodeTemplateBySdcType @Test public void testServiceNodeTemplateBySdcType() { @@ -437,4 +488,37 @@ public class ToscaParserNodeTemplateTest extends SdcToscaParserBasicTest { assertEquals(0, vfcList.size()); } //endregion + + + @Test + public void testFilterNodeTemplatePropertiesByDummyEquals() { + List<NodeTemplate> vfcs = complexCps.getVfcListByVf("f999e2ca-72c0-42d3-9b11-13f2122fb8ef"); + Map<String, Object> filteredInputs = complexCps.filterNodeTemplatePropertiesByValue(vfcs.get(0), FilterType.EQUALS, "dummy"); + assertNotNull(filteredInputs); + assertEquals(0, filteredInputs.size()); + } + + @Test + public void testFilterNodeTemplatePropertiesByNullFilterType() { + List<NodeTemplate> vfcs = complexCps.getVfcListByVf("f999e2ca-72c0-42d3-9b11-13f2122fb8ef"); + Map<String, Object> filteredInputs = complexCps.filterNodeTemplatePropertiesByValue(vfcs.get(11), null, "ddc"); + assertNotNull(filteredInputs); + assertEquals(0, filteredInputs.size()); + } + + @Test + public void testFilterNodeTemplatePropertiesByNullPattern() { + List<NodeTemplate> vfcs = complexCps.getVfcListByVf("f999e2ca-72c0-42d3-9b11-13f2122fb8ef"); + Map<String, Object> filteredInputs = complexCps.filterNodeTemplatePropertiesByValue(vfcs.get(11), FilterType.EQUALS, null); + assertNotNull(filteredInputs); + assertEquals(0, filteredInputs.size()); + } + + @Test + public void testFilterNodeTemplatePropertiesByNullVfc() { + Map<String, Object> filteredInputs = complexCps.filterNodeTemplatePropertiesByValue(null, FilterType.EQUALS, "ddc"); + assertNotNull(filteredInputs); + assertEquals(0, filteredInputs.size()); + } + //endregion } |