[SDC-39] filter nt props values by string 51/5251/1
authorPavel Aharoni <pa0916@att.com>
Mon, 26 Jun 2017 14:32:09 +0000 (17:32 +0300)
committerPavel Aharoni <pa0916@att.com>
Mon, 26 Jun 2017 14:32:41 +0000 (17:32 +0300)
Change-Id: Ic03853b58e97bfcacf1f82449fb4f498acfc347c
Signed-off-by: Pavel Aharoni <pa0916@att.com>
README.md
pom.xml
src/main/java/org/openecomp/sdc/tosca/parser/api/ISdcCsarHelper.java
src/main/java/org/openecomp/sdc/tosca/parser/impl/FilterType.java [new file with mode: 0644]
src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcCsarHelperImpl.java
src/test/java/org/openecomp/sdc/impl/ToscaParserNodeTemplateTest.java

index f66e262..c49d25c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,5 +1,5 @@
 # OpenECOMP SDC-Tosca
-\r
+
 
 ---
 ---
@@ -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
diff --git a/pom.xml b/pom.xml
index 5074354..4325e1d 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
        <artifactId>sdc-tosca</artifactId>\r
        <name>SDC Tosca Parser</name>\r
        <description>SDC Tosca Parser JAR file for use by consumers</description>\r
-       <version>1.1.31-SNAPSHOT</version>\r
+       <version>1.1.32-SNAPSHOT</version>\r
        <packaging>jar</packaging>\r
 \r
        <properties>\r
@@ -97,7 +97,7 @@
                <dependency>\r
                        <groupId>org.openecomp.sdc.jtosca</groupId>\r
                        <artifactId>jtosca</artifactId>\r
-                       <version>1.1.0-SNAPSHOT</version>\r
+                       <version>1.1.1-SNAPSHOT</version>\r
                </dependency>\r
 \r
 \r
index eeb23b4..83bd29d 100644 (file)
@@ -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 (file)
index 0000000..0f7810f
--- /dev/null
@@ -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);
+
+}
index 1c4b71b..4a3f73e 100644 (file)
@@ -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");
index f9cedf3..4ce3db4 100644 (file)
@@ -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
 }