Fix node filter get methods

Support normative properties and capabilities as regular get methods

Issue-ID: SDC-1455
Change-Id: I1ab709a17a2e03f81a7ae789b8c8fb83a1d03e81
Signed-off-by: shiria <shiri.amichai@amdocs.com>
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 5f3d465..2c95568 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
@@ -19,7 +19,8 @@
 import org.apache.commons.collections4.CollectionUtils;
 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
 
-import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -29,66 +30,84 @@
     private List<Map<String, List<Constraint>>> properties;
     private List<Map<String, CapabilityFilter>> capabilities;
 
-    //can't not be removed, in used in snake yaml
     public List<Map<String, CapabilityFilter>> getCapabilities() {
         return capabilities;
     }
 
     public void setCapabilities(List<Map<String, CapabilityFilter>> capabilities) {
-        this.capabilities = capabilities;
+        this.capabilities = getNormalizeCapabilities(capabilities);
     }
 
-    //can't not be removed, in used in snake yaml
+    public void setProperties(List<Map<String, List<Constraint>>> properties) {
+        this.properties = getNormalizeProperties(properties);
+    }
+
     public List<Map<String, List<Constraint>>> getProperties() {
         return properties;
     }
 
-
-    //use this function in order to get node filter properties instead of getProperties function
-    public List<Map<String, List<Constraint>>> getNormalizeProperties() {
-        return getNormalizeProperties(properties);
-    }
-
     private List<Map<String, List<Constraint>>> getNormalizeProperties(List<Map<String, List<Constraint>>> properties) {
-        ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
         if (CollectionUtils.isEmpty(properties)) {
             return properties;
         }
-        for (Map<String, List<Constraint>> propertyConstraintsEntity : properties) {
-            String propertyKey = propertyConstraintsEntity.keySet().iterator().next();
-            List<Constraint> constraints = propertyConstraintsEntity.get(propertyKey);
-            Iterator<Constraint> iterator = constraints.iterator();
-            while (iterator.hasNext()) {
-                Constraint constraintObj = iterator.next();
-                Constraint constraint = toscaExtensionYamlUtil
-                                                .yamlToObject(toscaExtensionYamlUtil.objectToYaml(constraintObj),
-                                                        Constraint.class);
-                constraints.remove(constraintObj);
-                constraints.add(constraint);
-            }
+        List<Map<String, List<Constraint>>> normalizeProperties = new ArrayList<>();
+        for (Map<String, List<Constraint>> propertyEntry : properties) {
+            Map<String, List<Constraint>> normalizePropertyEntry = getNormalizePropertyEntry(propertyEntry);
+            normalizeProperties.add(normalizePropertyEntry);
         }
-        return properties;
+        return normalizeProperties;
+
     }
 
-    //use this function in order to get node filter capabilities instead of getCapabilities function
-    public List<Map<String, CapabilityFilter>> getNormalizeCapabilities() {
+    private Map<String, List<Constraint>> getNormalizePropertyEntry(Map<String, List<Constraint>> propertyEntry) {
+        Map<String, List<Constraint>> normalizePropertyEntry = new HashMap<>();
+        String propertyKey = propertyEntry.keySet().iterator().next();  //only one entry exist in the map
+        List<Constraint> constraints = propertyEntry.get(propertyKey);
+        List<Constraint> normalizeConstraints = getNormalizeConstrains(constraints);
+        normalizePropertyEntry.put(propertyKey, normalizeConstraints);
+        return normalizePropertyEntry;
+    }
+
+    private List<Constraint> getNormalizeConstrains(List<Constraint> constraints) {
         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
+        List<Constraint> normalizeConstraints = new ArrayList<>();
+        for (Object constraintObj : constraints) {
+            Constraint normalizeConstraint = toscaExtensionYamlUtil
+                                                     .yamlToObject(toscaExtensionYamlUtil.objectToYaml(constraintObj),
+                                                             Constraint.class);
+            normalizeConstraints.add(normalizeConstraint);
+        }
+        return normalizeConstraints;
+    }
+
+    public List<Map<String, CapabilityFilter>> getNormalizeCapabilities(List<Map<String, CapabilityFilter>> capabilities) {
         if (CollectionUtils.isEmpty(capabilities)) {
             return capabilities;
         }
+        List<Map<String, CapabilityFilter>> normalizeCapabilities = new ArrayList<>();
         for (Map<String, CapabilityFilter> capabilityEntry : capabilities) {
-            String capabilityKey = capabilityEntry.keySet().iterator().next();
-            Object capabilityFilterObj = capabilityEntry.get(capabilityKey);
-            CapabilityFilter capabilityFilter = toscaExtensionYamlUtil.yamlToObject(
-                    toscaExtensionYamlUtil.objectToYaml(capabilityFilterObj), CapabilityFilter.class);
-            capabilityFilter.setProperties(getNormalizeProperties(capabilityFilter.getProperties()));
-            capabilityEntry.remove(capabilityKey);
-            capabilityEntry.put(capabilityKey, capabilityFilter);
+            Map<String, CapabilityFilter> normalizeCapabilityEntry = getNormalizeCapabilityEntry(capabilityEntry);
+            normalizeCapabilities.add(normalizeCapabilityEntry);
         }
-        return capabilities;
+        return normalizeCapabilities;
     }
 
-    public void setProperties(List<Map<String, List<Constraint>>> properties) {
-        this.properties = properties;
+    private Map<String, CapabilityFilter> getNormalizeCapabilityEntry(Map<String, CapabilityFilter> capabilityEntry) {
+        Map<String, CapabilityFilter> normalizeCapabilityEntry = new HashMap<>();
+        String capabilityKey = capabilityEntry.keySet().iterator().next(); //only one entry exist in the map
+        Object capabilityFilterObj = capabilityEntry.get(capabilityKey);
+        CapabilityFilter normalizeCapabilityFilter = getNormalizeCapabilityFilter(capabilityFilterObj);
+        normalizeCapabilityEntry.put(capabilityKey, normalizeCapabilityFilter);
+        return normalizeCapabilityEntry;
+    }
+
+
+    private CapabilityFilter getNormalizeCapabilityFilter(Object capabilityFilterObj) {
+        ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
+        CapabilityFilter capabilityFilter = toscaExtensionYamlUtil.yamlToObject(
+                toscaExtensionYamlUtil.objectToYaml(capabilityFilterObj), CapabilityFilter.class);
+        capabilityFilter.setProperties(getNormalizeProperties(capabilityFilter.getProperties()));
+
+        return capabilityFilter;
     }
 }
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 3938d8f..5412b55 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
@@ -25,32 +25,9 @@
 import java.util.Optional;
 import org.junit.Assert;
 import org.junit.Test;
-import org.onap.sdc.tosca.datatypes.model.ArtifactType;
-import org.onap.sdc.tosca.datatypes.model.AttributeDefinition;
-import org.onap.sdc.tosca.datatypes.model.CapabilityAssignment;
-import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition;
-import org.onap.sdc.tosca.datatypes.model.Constraint;
-import org.onap.sdc.tosca.datatypes.model.Directive;
-import org.onap.sdc.tosca.datatypes.model.Implementation;
-import org.onap.sdc.tosca.datatypes.model.Import;
-import org.onap.sdc.tosca.datatypes.model.InterfaceDefinition;
-import org.onap.sdc.tosca.datatypes.model.InterfaceDefinitionTemplate;
-import org.onap.sdc.tosca.datatypes.model.InterfaceDefinitionType;
-import org.onap.sdc.tosca.datatypes.model.InterfaceType;
-import org.onap.sdc.tosca.datatypes.model.NodeFilter;
-import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
-import org.onap.sdc.tosca.datatypes.model.NodeType;
-import org.onap.sdc.tosca.datatypes.model.OperationDefinition;
-import org.onap.sdc.tosca.datatypes.model.OperationDefinitionTemplate;
-import org.onap.sdc.tosca.datatypes.model.OperationDefinitionType;
-import org.onap.sdc.tosca.datatypes.model.ParameterDefinition;
-import org.onap.sdc.tosca.datatypes.model.PropertyDefinition;
-import org.onap.sdc.tosca.datatypes.model.PropertyType;
+import org.onap.sdc.tosca.datatypes.model.*;
 import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
-import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
-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.extension.*;
 import org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
 import org.onap.sdc.tosca.services.YamlUtil;
@@ -63,6 +40,9 @@
     public static final String FIRST_NODE_TEMPLATE = "firstNodeTemplate";
     public static final String REQ1 = "req1";
     public static final String REQ2 = "req2";
+    public static final String SERVICE_FILTER_TOSCA_ID = "{get_input=inParam1}";
+    public static final String VMD_NAME = "vmdName";
+    public static final String DIRECTOR = "director";
     private YamlUtil yamlUtil = new YamlUtil();
     private ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
     private static final String INTERFACE_ID = "inter_1";
@@ -436,10 +416,32 @@
         Object req1 = nodeTemplateRequirements.get(REQ1);
         Assert.assertEquals(true, req1 instanceof org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignment);
         Assert.assertNotNull(((org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignment)req1).getService_filter());
+        List<Map<String, List<Constraint>>> properties =
+                ((org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignment) req1).getService_filter()
+                                                                                           .getProperties();
+        Assert.assertNotNull(properties);
+        List<Constraint> vmdNameConstrain = properties.get(0).get(VMD_NAME);
+        Assert.assertNotNull(vmdNameConstrain);
+        Assert.assertNotNull(vmdNameConstrain.get(0).getEqual());
+
+        List<Map<String, CapabilityFilter>> capabilities =
+                ((org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignment) req1).getService_filter()
+                                                                                           .getCapabilities();
+        Assert.assertNotNull(capabilities);
+        CapabilityFilter capabilityFilter = capabilities.get(0).get(DIRECTOR);
+        Assert.assertNotNull(capabilityFilter);
+        Assert.assertNotNull(capabilityFilter.getProperties());
+
 
         Object req2 = nodeTemplateRequirements.get(REQ2);
         Assert.assertEquals(true, req2 instanceof org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignment);
         Assert.assertNotNull(((org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignment)req2).getService_filter());
+        Object tosca_id =
+                ((org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignment) req2).getService_filter()
+                                                                                           .getTosca_id();
+        Assert.assertNotNull(tosca_id);
+        Assert.assertEquals(SERVICE_FILTER_TOSCA_ID, tosca_id.toString());
+
 
         String serviceTemplateYaml = toscaExtensionYamlUtil.objectToYaml(serviceTemplateWithServiceFilter);
         Assert.assertNotNull(serviceTemplateYaml);
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithServiceFilter.yaml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithServiceFilter.yaml
index e1a49a4..87a8fcd 100644
--- a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithServiceFilter.yaml
+++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithServiceFilter.yaml
@@ -80,13 +80,20 @@
         att2: '{ get_input: my_mysql_rootpw }'
         att1: att1Val
       requirements:
-      - req1:
-          service_filter:
-            properties:
-               - name : [{get_input : inParam1}]
-      - req2:
-          service_filter:
-             tosca_id: {get_input : inParam1}
+        - req1:
+#            capability: director
+            service_filter:
+              properties:
+                - vmdName: [{ equal : myName }]
+                - controllers: [{ equal : { get_input: controllers } }]
+              capabilities:
+                - director:
+                   properties:
+                    - url_path :
+                        - { equal : { get_input: directorUrl } }
+        - req2:
+            service_filter:
+               tosca_id: {get_input : inParam1}
   substitution_mappings:
     node_type: myNodeType.node
     capabilities: