diff options
Diffstat (limited to 'catalog-be/src/main/java/org')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/ConstraintConvertor.java | 84 |
1 files changed, 40 insertions, 44 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/ConstraintConvertor.java b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/ConstraintConvertor.java index e05ef92431..ca59a0dda1 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/ConstraintConvertor.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/ConstraintConvertor.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import org.openecomp.sdc.be.model.tosca.ToscaFunctions; import org.openecomp.sdc.be.model.tosca.constraints.ConstraintType; import org.openecomp.sdc.be.ui.model.UIConstraint; import org.slf4j.Logger; @@ -25,31 +26,28 @@ public class ConstraintConvertor { public static final String PROPERTY_CONSTRAINT = "property"; public static final String SERVICE_INPUT_CONSTRAINT = "service_input"; public static final String SELF = "SELF"; - private static final Set<String> SUPPORTED_CONSTRAINT_LIST = + private static Set<String> SUPPORTED_CONSTRAINT_LIST = ImmutableSet.of(EQUAL_OPERATOR, GREATER_THAN_OPERATOR, LESS_THAN_OPERATOR); - private static final String GET_INPUT = "get_input"; - private static final String GET_PROPERTY = "get_property"; - private static final Set<String> SUPPORTED_FUNCTIONS = ImmutableSet.of(GET_INPUT, GET_PROPERTY); + + private static Set<String> SUPPORTED_FUNCTIONS = + ImmutableSet.of(ToscaFunctions.GET_INPUT.getFunctionName(), ToscaFunctions.GET_PROPERTY.getFunctionName()); public UIConstraint convert(String inConstraint) { Yaml yamlSource = new Yaml(); UIConstraint uiConstraint = new UIConstraint(); Object content1 = yamlSource.load(inConstraint); - if (content1 instanceof Map) { - Map map1 = (Map) content1; - Object key = map1.keySet().iterator().next(); - uiConstraint.setServicePropertyName(key.toString()); - Object content2 = map1.get(key); - if (content2 instanceof Map && handleServiceConstraint(uiConstraint, (Map) content2)) { - return uiConstraint; - } + if (!(content1 instanceof Map)) { + return null; } - return null; - } - - private boolean handleServiceConstraint(UIConstraint uiConstraint, Map content2) { - Map map2 = content2; + Map map1 = (Map) content1; + Object key = map1.keySet().iterator().next(); + uiConstraint.setServicePropertyName(key.toString()); + Object content2 = map1.get(key); + if (!(content2 instanceof Map)) { + return null; + } + Map map2 = (Map) content2; Object key2 = map2.keySet().iterator().next(); final String operator = key2.toString(); if (SUPPORTED_CONSTRAINT_LIST.contains(operator)) { @@ -59,40 +57,38 @@ public class ConstraintConvertor { if (content3 instanceof String || content3 instanceof Number || content3 instanceof Boolean) { uiConstraint.setValue(content3); uiConstraint.setSourceType(STATIC_CONSTRAINT); - return true; + return uiConstraint; } else if (content3 instanceof List) { List list1 = (List) content3; uiConstraint.setSourceType(STATIC_CONSTRAINT); uiConstraint.setValue(list1); - return true; + return uiConstraint; } else if (content3 instanceof Map) { - Map map3 = (Map) content3; - Map.Entry entry = (Map.Entry) map3.entrySet().iterator().next(); - final String firstKey = entry.getKey().toString().trim(); - if (handleSupportedFunctions(uiConstraint, entry, firstKey)) { - return true; - } - uiConstraint.setValue(content3); - return true; + return handleMap(uiConstraint, content3); } - return false; + return null; } - private boolean handleSupportedFunctions(UIConstraint uiConstraint, Map.Entry entry, String firstKey) { - if (SUPPORTED_FUNCTIONS.contains(firstKey)) { - if (GET_INPUT.equals(firstKey)) { - uiConstraint.setSourceType(SERVICE_INPUT_CONSTRAINT); - uiConstraint.setValue(entry.getValue()); - return true; - } else if (GET_PROPERTY.equals(firstKey)) { - uiConstraint.setSourceType(PROPERTY_CONSTRAINT); - final List<String> value = (List<String>) entry.getValue(); - uiConstraint.setSourceName(value.get(0)); - uiConstraint.setValue(value.get(1)); - return true; - } + private UIConstraint handleMap(UIConstraint uiConstraint, Object content3) { + Map map3 = (Map) content3; + Map.Entry entry = (Map.Entry) map3.entrySet().iterator().next(); + final String firstKey = entry.getKey().toString().trim(); + if (!SUPPORTED_FUNCTIONS.contains(firstKey)) { + uiConstraint.setValue(content3); + return uiConstraint; + } + if (ToscaFunctions.GET_INPUT.getFunctionName().equals(firstKey)) { + uiConstraint.setSourceType(SERVICE_INPUT_CONSTRAINT); + uiConstraint.setValue(entry.getValue()); + return uiConstraint; + } else if (ToscaFunctions.GET_PROPERTY.getFunctionName().equals(firstKey)) { + uiConstraint.setSourceType(PROPERTY_CONSTRAINT); + final List<String> value = (List<String>) entry.getValue(); + uiConstraint.setSourceName(value.get(0)); + uiConstraint.setValue(value.get(1)); + return uiConstraint; } - return false; + return null; } public List<String> convertToList(List<UIConstraint> uiConstraints) { @@ -120,10 +116,10 @@ public class ConstraintConvertor { map2.put(uiConstraint.getConstraintOperator(), value); } else if (uiConstraint.getSourceType().equals(PROPERTY_CONSTRAINT)) { List list1 = Arrays.asList(uiConstraint.getSourceName(), uiConstraint.getValue()); - Map map3 = ImmutableMap.of(GET_PROPERTY, list1); + Map map3 = ImmutableMap.of(ToscaFunctions.GET_PROPERTY.getFunctionName(), list1); map2.put(uiConstraint.getConstraintOperator(), map3); } else if (uiConstraint.getSourceType().equals(SERVICE_INPUT_CONSTRAINT)) { - Map map3 = ImmutableMap.of(GET_INPUT, uiConstraint.getValue()); + Map map3 = ImmutableMap.of(ToscaFunctions.GET_INPUT.getFunctionName(), uiConstraint.getValue()); map2.put(uiConstraint.getConstraintOperator(), map3); } |