Add support for service substitution filter

Change-Id: Id320380b83f072e0b8f5a81831111cbe96957d78
Issue-ID: SDC-1740
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 2c95568..be34394 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
@@ -16,13 +16,9 @@
 
 package org.onap.sdc.tosca.datatypes.model;
 
-import org.apache.commons.collections4.CollectionUtils;
-import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
-
-import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import org.onap.sdc.tosca.services.DataModelNormalizeUtil;
 
 
 public class NodeFilter {
@@ -35,79 +31,16 @@
     }
 
     public void setCapabilities(List<Map<String, CapabilityFilter>> capabilities) {
-        this.capabilities = getNormalizeCapabilities(capabilities);
+        this.capabilities = DataModelNormalizeUtil.getNormalizeCapabilitiesFilter(capabilities);
     }
 
     public void setProperties(List<Map<String, List<Constraint>>> properties) {
-        this.properties = getNormalizeProperties(properties);
+        this.properties = DataModelNormalizeUtil.getNormalizePropertiesFilter(properties);
     }
 
     public List<Map<String, List<Constraint>>> getProperties() {
         return properties;
     }
 
-    private List<Map<String, List<Constraint>>> getNormalizeProperties(List<Map<String, List<Constraint>>> properties) {
-        if (CollectionUtils.isEmpty(properties)) {
-            return properties;
-        }
-        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 normalizeProperties;
 
-    }
-
-    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) {
-            Map<String, CapabilityFilter> normalizeCapabilityEntry = getNormalizeCapabilityEntry(capabilityEntry);
-            normalizeCapabilities.add(normalizeCapabilityEntry);
-        }
-        return normalizeCapabilities;
-    }
-
-    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/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/SubstitutionFilter.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/SubstitutionFilter.java
new file mode 100644
index 0000000..8f3aa20
--- /dev/null
+++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/SubstitutionFilter.java
@@ -0,0 +1,57 @@
+/*
+ * 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.extension;
+
+
+import java.util.List;
+
+import java.util.Map;
+import org.onap.sdc.tosca.datatypes.model.Constraint;
+import org.onap.sdc.tosca.services.DataModelNormalizeUtil;
+
+public class SubstitutionFilter {
+
+    private List<Map<String, List<Constraint>>> properties;
+
+    public List<Map<String, List<Constraint>>> getProperties() {
+        return properties;
+    }
+
+    public void setProperties(List<Map<String, List<Constraint>>> properties) {
+        this.properties = DataModelNormalizeUtil.getNormalizePropertiesFilter(properties);
+    }
+
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof SubstitutionFilter)) {
+            return false;
+        }
+
+        SubstitutionFilter that = (SubstitutionFilter) o;
+
+        return getProperties() != null ? getProperties().equals(that.getProperties()) : that.getProperties() == null;
+    }
+
+    @Override
+    public int hashCode() {
+        return getProperties() != null ? getProperties().hashCode() : 0;
+    }
+}
diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/SubstitutionMappingExt.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/SubstitutionMappingExt.java
new file mode 100644
index 0000000..1fa3719
--- /dev/null
+++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/SubstitutionMappingExt.java
@@ -0,0 +1,52 @@
+/*
+ * 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.extension;
+
+import org.onap.sdc.tosca.datatypes.model.SubstitutionMapping;
+
+public class SubstitutionMappingExt extends SubstitutionMapping {
+
+    private SubstitutionFilter substitution_filter;
+
+    public SubstitutionFilter getSubstitution_filter() {
+        return substitution_filter;
+    }
+
+    public void setSubstitution_filter(SubstitutionFilter substitution_filter) {
+        this.substitution_filter = substitution_filter;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof SubstitutionMappingExt)) {
+            return false;
+        }
+
+        SubstitutionMappingExt that = (SubstitutionMappingExt) o;
+
+        return getSubstitution_filter() != null ? getSubstitution_filter().equals(that.getSubstitution_filter()) :
+                       that.getSubstitution_filter() == null;
+    }
+
+    @Override
+    public int hashCode() {
+        return getSubstitution_filter() != null ? getSubstitution_filter().hashCode() : 0;
+    }
+}
diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/DataModelNormalizeUtil.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/DataModelNormalizeUtil.java
new file mode 100644
index 0000000..b8a950c
--- /dev/null
+++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/DataModelNormalizeUtil.java
@@ -0,0 +1,101 @@
+/*
+ * 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.services;
+
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.commons.collections4.CollectionUtils;
+import org.onap.sdc.tosca.datatypes.model.CapabilityFilter;
+import org.onap.sdc.tosca.datatypes.model.Constraint;
+
+public class DataModelNormalizeUtil {
+
+    private DataModelNormalizeUtil() {
+        throw new IllegalStateException("Utility class");
+    }
+
+    public static List<Map<String, CapabilityFilter>> getNormalizeCapabilitiesFilter(
+            List<Map<String, CapabilityFilter>> capabilitiesFilter) {
+        if (CollectionUtils.isEmpty(capabilitiesFilter)) {
+            return capabilitiesFilter;
+        }
+        List<Map<String, CapabilityFilter>> normalizeCapabilities = new ArrayList<>();
+        for (Map<String, CapabilityFilter> capabilityFilterEntry : capabilitiesFilter) {
+            Map<String, CapabilityFilter> normalizeCapabilityEntry = getNormalizeCapabilityFilterEntry(capabilityFilterEntry);
+            normalizeCapabilities.add(normalizeCapabilityEntry);
+        }
+        return normalizeCapabilities;
+    }
+
+    private static Map<String, CapabilityFilter> getNormalizeCapabilityFilterEntry(
+            Map<String, CapabilityFilter> capabilityFilterEntry) {
+        Map<String, CapabilityFilter> normalizeCapabilityEntry = new HashMap<>();
+        String capabilityKey = capabilityFilterEntry.keySet().iterator().next(); //only one entry exist in the map
+        Object capabilityFilterObj = capabilityFilterEntry.get(capabilityKey);
+        CapabilityFilter normalizeCapabilityFilter = getNormalizeCapabilityFilter(capabilityFilterObj);
+        normalizeCapabilityEntry.put(capabilityKey, normalizeCapabilityFilter);
+        return normalizeCapabilityEntry;
+    }
+
+    private static CapabilityFilter getNormalizeCapabilityFilter(Object capabilityFilterObj) {
+        ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
+        CapabilityFilter capabilityFilter = toscaExtensionYamlUtil.yamlToObject(
+                toscaExtensionYamlUtil.objectToYaml(capabilityFilterObj), CapabilityFilter.class);
+        capabilityFilter.setProperties(getNormalizePropertiesFilter(capabilityFilter.getProperties()));
+
+        return capabilityFilter;
+    }
+
+    public static List<Map<String, List<Constraint>>> getNormalizePropertiesFilter(
+            List<Map<String, List<Constraint>>> properties) {
+        if (CollectionUtils.isEmpty(properties)) {
+            return properties;
+        }
+        List<Map<String, List<Constraint>>> normalizeProperties = new ArrayList<>();
+        for (Map<String, List<Constraint>> propertyFilterEntry : properties) {
+            Map<String, List<Constraint>> normalizePropertyEntry = getNormalizePropertyFilterEntry(propertyFilterEntry);
+            normalizeProperties.add(normalizePropertyEntry);
+        }
+        return normalizeProperties;
+
+    }
+
+    private static Map<String, List<Constraint>> getNormalizePropertyFilterEntry(
+            Map<String, List<Constraint>> propertyFilterEntry) {
+        Map<String, List<Constraint>> normalizePropertyEntry = new HashMap<>();
+        String propertyKey = propertyFilterEntry.keySet().iterator().next();  //only one entry exist in the map
+        List<Constraint> constraints = propertyFilterEntry.get(propertyKey);
+        List<Constraint> normalizeConstraints = getNormalizeConstrains(constraints);
+        normalizePropertyEntry.put(propertyKey, normalizeConstraints);
+        return normalizePropertyEntry;
+    }
+
+    private static 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;
+    }
+}
diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/ToscaExtensionYamlUtil.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/ToscaExtensionYamlUtil.java
index 4b84361..eecbdc6 100644
--- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/ToscaExtensionYamlUtil.java
+++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/ToscaExtensionYamlUtil.java
@@ -16,6 +16,7 @@
 
 package org.onap.sdc.tosca.services;
 
+import java.beans.IntrospectionException;
 import org.onap.sdc.tosca.error.ToscaRuntimeException;
 import org.yaml.snakeyaml.constructor.Constructor;
 import org.yaml.snakeyaml.introspector.Property;
@@ -23,8 +24,6 @@
 import org.yaml.snakeyaml.nodes.MappingNode;
 import org.yaml.snakeyaml.nodes.NodeId;
 
-import java.beans.IntrospectionException;
-
 
 public class ToscaExtensionYamlUtil extends YamlUtil {
 
@@ -36,6 +35,10 @@
             "org.onap.sdc.tosca.datatypes.model.RequirementAssignment";
     public static final String TOSCA_MODEL_EXT_REQUIREMENT_ASSIGNMENT =
             "org.onap.sdc.tosca.datatypes.model.extension.RequirementAssignment";
+    public static final String TOSCA_MODEL_SUBSTITUTION_MAPPING =
+            "org.onap.sdc.tosca.datatypes.model.SubstitutionMapping";
+    public static final String TOSCA_MODEL_EXT_SUBSTITUTION_MAPPING =
+            "org.onap.sdc.tosca.datatypes.model.extension.SubstitutionMappingExt";
 
     @Override
     public <T> Constructor getConstructor(Class<T> typClass) {
@@ -59,6 +62,9 @@
                 if (type.equals(Class.forName(TOSCA_MODEL_REQUIREMENT_ASSIGNMENT))) {
                     classType = Class.forName(TOSCA_MODEL_EXT_REQUIREMENT_ASSIGNMENT);
                 }
+                if (type.equals(Class.forName(TOSCA_MODEL_SUBSTITUTION_MAPPING))) {
+                    classType = Class.forName(TOSCA_MODEL_EXT_SUBSTITUTION_MAPPING);
+                }
             } catch (ClassNotFoundException ex) {
                 throw new ToscaRuntimeException(ex);
             }
@@ -89,6 +95,11 @@
                         Object extendHeatObject = extendHeatClass.newInstance();
                         // create JavaBean
                         return super.constructJavaBean2ndStep(node, extendHeatObject);
+                    } else if (type.equals(Class.forName(TOSCA_MODEL_SUBSTITUTION_MAPPING))) {
+                        Class extendHeatClass = Class.forName(TOSCA_MODEL_EXT_SUBSTITUTION_MAPPING);
+                        Object extendHeatObject = extendHeatClass.newInstance();
+                        // create JavaBean
+                        return super.constructJavaBean2ndStep(node, extendHeatObject);
                     } else {
                         // create JavaBean
                         return super.constructJavaBean2ndStep(node, object);
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 5412b55..2833cc4 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
@@ -62,6 +62,8 @@
     private static final String DEPENDENCY_NAME = "script1.sh";
     private static final String STRING_TYPE = "string";
     private static final String ST_WITH_SERVICE_FILTER = "/serviceTemplateWithServiceFilter.yaml";
+    private static final String SUBSTITUTION_MAPPING = "/serviceTemplateWithSubstitutionMapping.yaml";
+    private static final String SUBSTITUTION_MAPPING_EXT = "/substitutionMappingExt.yaml";
 
     @Test
     public void testServiceTemplateJavaToYaml() {
@@ -448,6 +450,44 @@
 
     }
 
+    @Test
+    public void testSubstitutionMapping() throws IOException {
+        ServiceTemplate serviceTemplate = getServiceTemplateExt(BASE_DIR + SUBSTITUTION_MAPPING);
+
+        SubstitutionMapping substitutionMappings = DataModelUtil.getSubstitutionMappings(serviceTemplate);
+        Assert.assertEquals("myNodeType.node", substitutionMappings.getNode_type());
+        Assert.assertNotNull(substitutionMappings.getCapabilities());
+        Assert.assertEquals(1,substitutionMappings.getCapabilities().size());
+        Assert.assertNotNull(substitutionMappings.getRequirements());
+        Assert.assertEquals(1,substitutionMappings.getRequirements().size());
+        Assert.assertEquals(true, substitutionMappings instanceof SubstitutionMappingExt);
+        Assert.assertNull(((SubstitutionMappingExt)substitutionMappings).getSubstitution_filter());
+
+    }
+
+    @Test
+    public void testSubstitutionMappingExt() throws IOException {
+        ServiceTemplate serviceTemplate = getServiceTemplateExt(BASE_DIR + SUBSTITUTION_MAPPING_EXT);
+
+        SubstitutionMapping substitutionMappings = DataModelUtil.getSubstitutionMappings(serviceTemplate);
+        Assert.assertEquals("myNodeType.node", substitutionMappings.getNode_type());
+        Assert.assertNotNull(substitutionMappings.getCapabilities());
+        Assert.assertEquals(1,substitutionMappings.getCapabilities().size());
+        Assert.assertNotNull(substitutionMappings.getRequirements());
+        Assert.assertEquals(1,substitutionMappings.getRequirements().size());
+        Assert.assertEquals(true, substitutionMappings instanceof SubstitutionMappingExt);
+        SubstitutionFilter substitutionFilter = ((SubstitutionMappingExt) substitutionMappings).getSubstitution_filter();
+        Assert.assertNotNull(substitutionFilter);
+        Assert.assertNotNull(substitutionFilter.getProperties());
+        Assert.assertEquals(2,substitutionFilter.getProperties().size());
+        List<Constraint> vendorFilter = substitutionFilter.getProperties().get(0).get("vendor");
+        Assert.assertNotNull(vendorFilter);
+        Assert.assertNotNull(vendorFilter.get(0).getEqual());
+
+
+
+    }
+
     private ServiceTemplate getServiceTemplate(String inputPath) throws IOException {
         try (InputStream yamlFile = yamlUtil.loadYamlFileIs(inputPath)) {
             return yamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithSubstitutionMapping.yaml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithSubstitutionMapping.yaml
new file mode 100644
index 0000000..b5e5480
--- /dev/null
+++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/serviceTemplateWithSubstitutionMapping.yaml
@@ -0,0 +1,17 @@
+tosca_definitions_version: tosca_simple_yaml_1_0_0
+metadata:
+  template_name: Test
+  template_author: OPENECOMP
+  template_version: 1.0.0
+description: testing desc tosca service template
+
+topology_template:
+  description: topologi template descroption
+  substitution_mappings:
+    node_type: myNodeType.node
+    capabilities:
+      database_endpoint:
+      - database
+      - database_endpoint
+    requirements:
+      database_endpoint: [ app, database ]
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/substitutionMappingExt.yaml b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/substitutionMappingExt.yaml
new file mode 100644
index 0000000..ae6a88e
--- /dev/null
+++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/resources/mock/model/substitutionMappingExt.yaml
@@ -0,0 +1,23 @@
+tosca_definitions_version: tosca_simple_yaml_1_0_0
+metadata:
+  template_name: Test
+  template_author: OPENECOMP
+  template_version: 1.0.0
+description: testing desc tosca service template
+
+topology_template:
+  description: topologi template descroption
+  substitution_mappings:
+    node_type: myNodeType.node
+    capabilities:
+      database_endpoint:
+      - database
+      - database_endpoint
+    requirements:
+      database_endpoint: [ app, database ]
+    substitution_filter:
+      properties:
+        - vendor: [{ equal : Simple }]
+        - bandwidth: [{ less_than: 100 GB }]
+
+