From f450879a13742c710173e6e2ccad74aa21e342fc Mon Sep 17 00:00:00 2001 From: shiria Date: Thu, 6 Sep 2018 08:33:09 +0300 Subject: Add support for service substitution filter Change-Id: Id320380b83f072e0b8f5a81831111cbe96957d78 Issue-ID: SDC-1740 Signed-off-by: shiria --- .../onap/sdc/tosca/datatypes/model/NodeFilter.java | 73 +-------------- .../model/extension/SubstitutionFilter.java | 57 ++++++++++++ .../model/extension/SubstitutionMappingExt.java | 52 +++++++++++ .../sdc/tosca/services/DataModelNormalizeUtil.java | 101 +++++++++++++++++++++ .../sdc/tosca/services/ToscaExtensionYamlUtil.java | 15 ++- 5 files changed, 226 insertions(+), 72 deletions(-) create mode 100644 common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/SubstitutionFilter.java create mode 100644 common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/datatypes/model/extension/SubstitutionMappingExt.java create mode 100644 common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/DataModelNormalizeUtil.java (limited to 'common/onap-tosca-datatype/src/main') 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 2c95568a66..be34394e34 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 class NodeFilter { } public void setCapabilities(List> capabilities) { - this.capabilities = getNormalizeCapabilities(capabilities); + this.capabilities = DataModelNormalizeUtil.getNormalizeCapabilitiesFilter(capabilities); } public void setProperties(List>> properties) { - this.properties = getNormalizeProperties(properties); + this.properties = DataModelNormalizeUtil.getNormalizePropertiesFilter(properties); } public List>> getProperties() { return properties; } - private List>> getNormalizeProperties(List>> properties) { - if (CollectionUtils.isEmpty(properties)) { - return properties; - } - List>> normalizeProperties = new ArrayList<>(); - for (Map> propertyEntry : properties) { - Map> normalizePropertyEntry = getNormalizePropertyEntry(propertyEntry); - normalizeProperties.add(normalizePropertyEntry); - } - return normalizeProperties; - - } - - private Map> getNormalizePropertyEntry(Map> propertyEntry) { - Map> normalizePropertyEntry = new HashMap<>(); - String propertyKey = propertyEntry.keySet().iterator().next(); //only one entry exist in the map - List constraints = propertyEntry.get(propertyKey); - List normalizeConstraints = getNormalizeConstrains(constraints); - normalizePropertyEntry.put(propertyKey, normalizeConstraints); - return normalizePropertyEntry; - } - - private List getNormalizeConstrains(List constraints) { - ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); - List normalizeConstraints = new ArrayList<>(); - for (Object constraintObj : constraints) { - Constraint normalizeConstraint = toscaExtensionYamlUtil - .yamlToObject(toscaExtensionYamlUtil.objectToYaml(constraintObj), - Constraint.class); - normalizeConstraints.add(normalizeConstraint); - } - return normalizeConstraints; - } - - public List> getNormalizeCapabilities(List> capabilities) { - if (CollectionUtils.isEmpty(capabilities)) { - return capabilities; - } - List> normalizeCapabilities = new ArrayList<>(); - for (Map capabilityEntry : capabilities) { - Map normalizeCapabilityEntry = getNormalizeCapabilityEntry(capabilityEntry); - normalizeCapabilities.add(normalizeCapabilityEntry); - } - return normalizeCapabilities; - } - - private Map getNormalizeCapabilityEntry(Map capabilityEntry) { - Map 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 0000000000..8f3aa20a48 --- /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>> properties; + + public List>> getProperties() { + return properties; + } + + public void setProperties(List>> 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 0000000000..1fa3719571 --- /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 0000000000..b8a950cd85 --- /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> getNormalizeCapabilitiesFilter( + List> capabilitiesFilter) { + if (CollectionUtils.isEmpty(capabilitiesFilter)) { + return capabilitiesFilter; + } + List> normalizeCapabilities = new ArrayList<>(); + for (Map capabilityFilterEntry : capabilitiesFilter) { + Map normalizeCapabilityEntry = getNormalizeCapabilityFilterEntry(capabilityFilterEntry); + normalizeCapabilities.add(normalizeCapabilityEntry); + } + return normalizeCapabilities; + } + + private static Map getNormalizeCapabilityFilterEntry( + Map capabilityFilterEntry) { + Map 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>> getNormalizePropertiesFilter( + List>> properties) { + if (CollectionUtils.isEmpty(properties)) { + return properties; + } + List>> normalizeProperties = new ArrayList<>(); + for (Map> propertyFilterEntry : properties) { + Map> normalizePropertyEntry = getNormalizePropertyFilterEntry(propertyFilterEntry); + normalizeProperties.add(normalizePropertyEntry); + } + return normalizeProperties; + + } + + private static Map> getNormalizePropertyFilterEntry( + Map> propertyFilterEntry) { + Map> normalizePropertyEntry = new HashMap<>(); + String propertyKey = propertyFilterEntry.keySet().iterator().next(); //only one entry exist in the map + List constraints = propertyFilterEntry.get(propertyKey); + List normalizeConstraints = getNormalizeConstrains(constraints); + normalizePropertyEntry.put(propertyKey, normalizeConstraints); + return normalizePropertyEntry; + } + + private static List getNormalizeConstrains(List constraints) { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + List 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 4b8436197d..eecbdc6a7e 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.introspector.PropertyUtils; 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 @@ public class ToscaExtensionYamlUtil extends YamlUtil { "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 Constructor getConstructor(Class typClass) { @@ -59,6 +62,9 @@ public class ToscaExtensionYamlUtil extends YamlUtil { 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 @@ public class ToscaExtensionYamlUtil extends YamlUtil { 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); -- cgit 1.2.3-korg