diff options
Diffstat (limited to 'catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca')
16 files changed, 231 insertions, 170 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaPropertyType.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaPropertyType.java index 2bbb84dd20..079d64a60e 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaPropertyType.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaPropertyType.java @@ -58,34 +58,27 @@ public enum ToscaPropertyType { STRING("string", StringValidator.getInstance(), StringConvertor.getInstance(), ToscaStringConvertor.getInstance()), - BOOLEAN("boolean", BooleanValidator.getInstance(), ToscaBooleanConverter.getInstance(), - BooleanConverter.getInstance()), + BOOLEAN("boolean", BooleanValidator.getInstance(), ToscaBooleanConverter.getInstance(), BooleanConverter.getInstance()), FLOAT("float", FloatValidator.getInstance(), ToscaFloatConverter.getInstance(), FloatConverter.getInstance()), INTEGER("integer", IntegerValidator.getInstance(), DefaultConverter.getInstance(), IntegerConverter.getInstance()), - SCALAR_UNIT_SIZE("scalar-unit.size", StringValidator.getInstance(), DefaultConverter.getInstance(), - ToscaValueDefaultConverter.getInstance()), + SCALAR_UNIT_SIZE("scalar-unit.size", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()), - SCALAR_UNIT_TIME("scalar-unit.time", StringValidator.getInstance(), DefaultConverter.getInstance(), - ToscaValueDefaultConverter.getInstance()), + SCALAR_UNIT_TIME("scalar-unit.time", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()), - SCALAR_UNIT_FREQUENCY("scalar-unit.frequency", StringValidator.getInstance(), DefaultConverter.getInstance(), - ToscaValueDefaultConverter.getInstance()), + SCALAR_UNIT_FREQUENCY("scalar-unit.frequency", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()), - RANGE("range", StringValidator.getInstance(), DefaultConverter.getInstance(), - ToscaValueDefaultConverter.getInstance()), + RANGE("range", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()), - TIMESTAMP("timestamp", StringValidator.getInstance(), DefaultConverter.getInstance(), - ToscaValueDefaultConverter.getInstance()), + TIMESTAMP("timestamp", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()), MAP("map", MapValidator.getInstance(), MapConverter.getInstance(), ToscaMapValueConverter.getInstance()), LIST("list", ListValidator.getInstance(), ListConverter.getInstance(), ToscaListValueConverter.getInstance()), - VERSION("version", StringValidator.getInstance(), DefaultConverter.getInstance(), - ToscaValueDefaultConverter.getInstance()), + VERSION("version", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()), KEY("key", KeyValidator.getInstance(), StringConvertor.getInstance(), ToscaValueDefaultConverter.getInstance()), @@ -100,16 +93,14 @@ public enum ToscaPropertyType { private ToscaValueConverter valueConverter; private boolean isAbstract = false; - ToscaPropertyType(String type, PropertyTypeValidator validator, PropertyValueConverter converter, - ToscaValueConverter valueConverter) { + ToscaPropertyType(String type, PropertyTypeValidator validator, PropertyValueConverter converter, ToscaValueConverter valueConverter) { this.type = type; this.validator = validator; this.converter = converter; this.valueConverter = valueConverter; } - ToscaPropertyType(String type, PropertyTypeValidator validator, PropertyValueConverter converter, - ToscaValueConverter valueConverter, boolean isAbstract) { + ToscaPropertyType(String type, PropertyTypeValidator validator, PropertyValueConverter converter, ToscaValueConverter valueConverter, boolean isAbstract) { this(type, validator, converter, valueConverter); this.isAbstract = isAbstract; } @@ -179,6 +170,17 @@ public enum ToscaPropertyType { return isPrimitiveToscaType != null && isPrimitiveToscaType.isAbstract() == false; } + + public static boolean isPrimitiveType(String dataTypeName) { + + if (ToscaPropertyType.MAP.getType().equals(dataTypeName) || ToscaPropertyType.LIST.getType().equals(dataTypeName)){ + return false; + } + if(isScalarType(dataTypeName)){ + return true; + } + return false; + } public static ToscaPropertyType getTypeIfScalar(String dataTypeName) { diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatBooleanConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatBooleanConverter.java index 147b2e9c52..52fa9bfa81 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatBooleanConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatBooleanConverter.java @@ -39,7 +39,7 @@ public class HeatBooleanConverter implements PropertyValueConverter { @Override public String convert(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) { - if (value == null) { + if (value == null || value.isEmpty()) { return null; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatCommaDelimitedListConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatCommaDelimitedListConverter.java index a0834791ff..30c21c8c1c 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatCommaDelimitedListConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatCommaDelimitedListConverter.java @@ -39,11 +39,13 @@ public class HeatCommaDelimitedListConverter implements PropertyValueConverter { @Override public String convert(String original, String innerType, Map<String, DataTypeDefinition> dataTypes) { - String coverted = ValidationUtils.removeNoneUtf8Chars(original); - coverted = ValidationUtils.removeHtmlTagsOnly(coverted); - coverted = ValidationUtils.normaliseWhitespace(coverted); - coverted = ValidationUtils.stripOctets(coverted); - + String coverted = null; + if(original != null){ + coverted = ValidationUtils.removeNoneUtf8Chars(original); + coverted = ValidationUtils.removeHtmlTagsOnly(coverted); + coverted = ValidationUtils.normaliseWhitespace(coverted); + coverted = ValidationUtils.stripOctets(coverted); + } return coverted; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatNumberConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatNumberConverter.java index 90781be367..8798b544bc 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatNumberConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatNumberConverter.java @@ -40,7 +40,7 @@ public class HeatNumberConverter implements PropertyValueConverter { @Override public String convert(String original, String innerType, Map<String, DataTypeDefinition> dataTypes) { - if (original == null) { + if (original == null || original.isEmpty()) { return null; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatStringConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatStringConverter.java index 475db1db7c..c8fac11b3a 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatStringConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatStringConverter.java @@ -39,12 +39,14 @@ public class HeatStringConverter implements PropertyValueConverter { @Override public String convert(String original, String innerType, Map<String, DataTypeDefinition> dataTypes) { - String coverted = ValidationUtils.removeNoneUtf8Chars(original); - coverted = ValidationUtils.normaliseWhitespace(coverted); - coverted = ValidationUtils.stripOctets(coverted); - coverted = ValidationUtils.removeHtmlTagsOnly(coverted); - coverted = coverted.replaceAll("\"", "").replaceAll("\'", ""); - + String coverted = null; + if(original != null){ + coverted = ValidationUtils.removeNoneUtf8Chars(original); + coverted = ValidationUtils.normaliseWhitespace(coverted); + coverted = ValidationUtils.stripOctets(coverted); + coverted = ValidationUtils.removeHtmlTagsOnly(coverted); + coverted = coverted.replaceAll("\"", "").replaceAll("\'", ""); + } return coverted; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/IntegerConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/IntegerConverter.java index 076e5aceef..30fbf69b80 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/IntegerConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/IntegerConverter.java @@ -38,6 +38,9 @@ public class IntegerConverter implements ToscaValueConverter { @Override public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) { + if ( value == null || value.isEmpty() ){ + return null; + } return Integer.parseInt(value); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ListConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ListConverter.java index 8265cc2690..4f6de9b80c 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ListConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ListConverter.java @@ -112,6 +112,8 @@ public class ListConverter implements PropertyValueConverter { ArrayList<String> newList = new ArrayList<String>(); JsonArray jo = (JsonArray) jsonParser.parse(value); + if(ToscaPropertyType.JSON == innerToscaType) + return Either.left(value); int size = jo.size(); for (int i = 0; i < size; i++) { JsonElement currentValue = jo.get(i); @@ -166,7 +168,7 @@ public class ListConverter implements PropertyValueConverter { } } catch (JsonParseException e) { - log.debug("Failed to parse json : {}. {}", value, e); + log.debug("Failed to parse json : {}", value, e); BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter"); return Either.right(false); } @@ -199,7 +201,7 @@ public class ListConverter implements PropertyValueConverter { ImmutablePair<JsonElement, Boolean> validateAndUpdate = dataTypeValidatorConverter .validateAndUpdate(element, dataTypeDefinition, allDataTypes); if (validateAndUpdate.right.booleanValue() == false) { - log.debug("Cannot parse value {} from type {} in list position {}", currentValue, innerType, i); + log.debug("Cannot parse value {} from type {} in list position {}",currentValue,innerType,i); return Either.right(false); } JsonElement newValue = validateAndUpdate.left; diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/MapConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/MapConverter.java index 921c6d0d41..30b895f0d2 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/MapConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/MapConverter.java @@ -181,7 +181,7 @@ public class MapConverter implements PropertyValueConverter { log.debug("inner Tosca Type unknown : {}", innerToscaType); } } catch (JsonParseException e) { - log.debug("Failed to parse json : {}. {}", value, e); + log.debug("Failed to parse json : {}", value, e); BeEcompErrorManager.getInstance().logBeInvalidJsonInput("Map Converter"); return Either.right(false); } @@ -227,7 +227,7 @@ public class MapConverter implements PropertyValueConverter { ImmutablePair<JsonElement, Boolean> validateAndUpdate = dataTypeValidatorConverter .validateAndUpdate(element, dataTypeDefinition, allDataTypes); if (validateAndUpdate.right.booleanValue() == false) { - log.debug("Cannot parse value {} from type {} of key {}", currentValue, innerType, currentKey); + log.debug("Cannot parse value {} from type {} of key {}",currentValue,innerType,currentKey); return Either.right(false); } JsonElement newValue = validateAndUpdate.left; diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java index 043446e783..d8198dac6b 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaListValueConverter.java @@ -109,13 +109,14 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements asJsonArray.forEach(e -> { Object convertedValue = null; if (isScalarF) { - log.debug("try to convert scalar value {}", e.getAsString()); - if (e.getAsString() == null) { + String jsonAsString = e.toString(); + log.debug("try to convert scalar value {}", jsonAsString); + if ( jsonAsString == null) { convertedValue = null; } else { - JsonElement singleElement = jsonParser.parse(e.getAsString()); + JsonElement singleElement = jsonParser.parse(jsonAsString); if (singleElement.isJsonPrimitive()) { - convertedValue = innerConverterFinal.convertToToscaValue(e.getAsString(), innerType, + convertedValue = innerConverterFinal.convertToToscaValue(jsonAsString, innerType, dataTypes); } else { convertedValue = handleComplexJsonValue(singleElement); @@ -128,16 +129,14 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType); Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition); Map<String, Object> toscaObjectPresentation = new HashMap<>(); - // log.debug("try to convert datatype value {}", - // e.getAsString()); - + for (Entry<String, JsonElement> entry : entrySet) { String propName = entry.getKey(); JsonElement elementValue = entry.getValue(); PropertyDefinition propertyDefinition = allProperties.get(propName); if (propertyDefinition == null) { - log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName()); + log.debug("The property {} was not found under data type {}",propName,dataTypeDefinition.getName()); continue; // return null; } @@ -173,7 +172,7 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements } catch ( JsonParseException e) { - log.debug("Failed to parse json : {}. {}", value, e); + log.debug("Failed to parse json : {}", value, e); BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter"); return null; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java index 601d8f0fc8..80b8779e1e 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java @@ -21,7 +21,9 @@ package org.openecomp.sdc.be.model.tosca.converters; import java.io.StringReader; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -33,6 +35,7 @@ import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; @@ -63,6 +66,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType); ToscaValueConverter innerConverter = null; boolean isScalar = true; + List<PropertyDefinition> allPropertiesRecursive = new ArrayList<>(); if (innerToscaType != null) { innerConverter = innerToscaType.getValueConverter(); } else { @@ -74,9 +78,14 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T innerConverter = toscaPropertyType.getValueConverter(); } else { isScalar = false; + allPropertiesRecursive.addAll(dataTypeDefinition.getProperties()); + DataTypeDefinition derivedFrom = dataTypeDefinition.getDerivedFrom(); + while ( !derivedFrom.getName().equals("tosca.datatypes.Root") ){ + allPropertiesRecursive.addAll(derivedFrom.getProperties()); + derivedFrom = derivedFrom.getDerivedFrom(); + } } } else { - // TODO handle getinput log.debug("inner Tosca Type is null"); return value; } @@ -105,21 +114,47 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T final boolean isScalarF = isScalar; final ToscaValueConverter innerConverterFinal = innerConverter; entrySet.forEach(e -> { - log.debug("try convert element {}", e.getValue()); - Object convertedValue = convertDataTypeToToscaMap(innerType, dataTypes, innerConverterFinal, isScalarF, - e.getValue()); - toscaMap.put(e.getKey(), convertedValue); + convertEntry(innerType, dataTypes, allPropertiesRecursive, toscaMap, isScalarF, innerConverterFinal, e); }); return toscaMap; } catch (JsonParseException e) { - log.debug("Failed to parse json : {}. {}", value, e); + log.debug("Failed to parse json : {}", value, e); BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Converter"); return null; } } - public Object convertDataTypeToToscaMap(String innerType, Map<String, DataTypeDefinition> dataTypes, - ToscaValueConverter innerConverter, final boolean isScalarF, JsonElement entryValue) { + private void convertEntry(String innerType, Map<String, DataTypeDefinition> dataTypes, List<PropertyDefinition> allPropertiesRecursive, Map<String, Object> toscaMap, final boolean isScalarF, final ToscaValueConverter innerConverterFinal, + Entry<String, JsonElement> e) { + log.debug("try convert element {}", e.getValue()); + boolean scalar = false; + String propType = null; + ToscaValueConverter innerConverterProp = innerConverterFinal; + if ( isScalarF ){ + scalar = isScalarF; + propType = innerType; + }else{ + for ( PropertyDefinition pd : allPropertiesRecursive ){ + if ( pd.getName().equals(e.getKey()) ){ + propType = pd.getType(); + DataTypeDefinition pdDataType = dataTypes.get(propType); + ToscaPropertyType toscaPropType = isScalarType(pdDataType); + if ( toscaPropType == null ){ + scalar = false; + }else{ + scalar = true; + propType = toscaPropType.getType(); + innerConverterProp = toscaPropType.getValueConverter(); + } + break; + } + } + } + Object convertedValue = convertDataTypeToToscaObject(propType, dataTypes, innerConverterProp, scalar, e.getValue()); + toscaMap.put(e.getKey(), convertedValue); + } + + public Object convertDataTypeToToscaObject(String innerType, Map<String, DataTypeDefinition> dataTypes, ToscaValueConverter innerConverter, final boolean isScalarF, JsonElement entryValue) { Object convertedValue = null; if (isScalarF && entryValue.isJsonPrimitive()) { log.debug("try convert scalar value {}", entryValue.getAsString()); @@ -129,25 +164,52 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T convertedValue = innerConverter.convertToToscaValue(entryValue.getAsString(), innerType, dataTypes); } } else { - JsonObject asJsonObjectIn = entryValue.getAsJsonObject(); - Set<Entry<String, JsonElement>> entrySetIn = asJsonObjectIn.entrySet(); - - DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType); - Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition); - Map<String, Object> toscaObjectPresentation = new HashMap<>(); - - for (Entry<String, JsonElement> entry : entrySetIn) { - String propName = entry.getKey(); - - JsonElement elementValue = entry.getValue(); - Object convValue; - if (isScalarF == false) { - PropertyDefinition propertyDefinition = allProperties.get(propName); - if (propertyDefinition == null && isScalarF) { - log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName()); - continue; - } + if ( entryValue.isJsonPrimitive() ){ + return handleComplexJsonValue(entryValue); + } + + // Tal G ticket 228696523 created / DE272734 / Bug 154492 Fix + if(entryValue instanceof JsonArray) { + ArrayList<Object> toscaObjectPresentationArray = new ArrayList<>(); + JsonArray jsonArray = entryValue.getAsJsonArray(); + + for (JsonElement jsonElement : jsonArray) { + Object convertedDataTypeToToscaMap = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, jsonElement); + toscaObjectPresentationArray.add(convertedDataTypeToToscaMap); + } + convertedValue = toscaObjectPresentationArray; + } else { + convertedValue = convertDataTypeToToscaMap(innerType, dataTypes, isScalarF, entryValue); + } + } + return convertedValue; + } + private Object convertDataTypeToToscaMap(String innerType, Map<String, DataTypeDefinition> dataTypes, + final boolean isScalarF, JsonElement entryValue) { + Object convertedValue; + JsonObject asJsonObjectIn = entryValue.getAsJsonObject(); + Set<Entry<String, JsonElement>> entrySetIn = asJsonObjectIn.entrySet(); + + DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType); + Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition); + Map<String, Object> toscaObjectPresentation = new HashMap<>(); + + for (Entry<String, JsonElement> entry : entrySetIn) { + String propName = entry.getKey(); + + JsonElement elementValue = entry.getValue(); + Object convValue; + if (isScalarF == false) { + PropertyDefinition propertyDefinition = allProperties.get(propName); + if (propertyDefinition == null) { + log.trace("The property {} was not found under data type . Parse as map", propName); + if (elementValue.isJsonPrimitive()) { + convValue = elementValue.getAsString(); + } else { + convValue = handleComplexJsonValue(elementValue); + } + } else { String type = propertyDefinition.getType(); ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type); if (propertyType != null) { @@ -165,19 +227,21 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T } } } else { - convValue = convertToToscaValue(elementValue.getAsString(), type, dataTypes); + convValue = convertToToscaValue(elementValue.toString(), type, dataTypes); } + } + } else { + if (elementValue.isJsonPrimitive()) { + convValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive()); } else { - if (elementValue.isJsonPrimitive()) { - convValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive()); - } else { - convValue = handleComplexJsonValue(elementValue); - } + convValue = handleComplexJsonValue(elementValue); } + } + if(!isEmptyObjectValue(convValue)){ toscaObjectPresentation.put(propName, convValue); } - convertedValue = toscaObjectPresentation; } + convertedValue = toscaObjectPresentation; return convertedValue; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java index e886327481..7e2f8766ba 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java @@ -86,7 +86,7 @@ public class ToscaValueBaseConverter { Object jsonValue = null; Map<String, Object> value = new HashMap<String, Object>(); - if ( elementValue.isJsonObject() ){ + if (elementValue.isJsonObject()) { JsonObject jsonOb = elementValue.getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet = jsonOb.entrySet(); Iterator<Entry<String, JsonElement>> iteratorEntry = entrySet.iterator(); @@ -106,14 +106,18 @@ public class ToscaValueBaseConverter { } } jsonValue = value; - }else{ - if ( elementValue.isJsonArray() ){ + } else { + if (elementValue.isJsonArray()) { jsonValue = handleJsonArray(elementValue); - }else{ - log.debug("not supported json type {} ",elementValue); + } else { + if (elementValue.isJsonPrimitive()) { + jsonValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive()); + } else { + log.debug("not supported json type {} ", elementValue); + } } } - + return jsonValue; } @@ -150,4 +154,24 @@ public class ToscaValueBaseConverter { throw new IllegalStateException(); } } + + /** + * checks is received Object empty or equals null or not It is relevant only + * if received Object is instance of String, Map or List class. + * + * @param convertedValue + * @return + */ + static public boolean isEmptyObjectValue(Object convertedValue) { + if (convertedValue == null) { + return true; + } else if (convertedValue instanceof String && ((String) convertedValue).isEmpty()) { + return true; + } else if (convertedValue instanceof Map && ((Map) convertedValue).isEmpty()) { + return true; + } else if (convertedValue instanceof List && ((List) convertedValue).isEmpty()) { + return true; + } + return false; + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/DataTypeValidatorConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/DataTypeValidatorConverter.java index d376a1ec13..3240ccc679 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/DataTypeValidatorConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/DataTypeValidatorConverter.java @@ -88,8 +88,7 @@ public class DataTypeValidatorConverter { return result; } - private ImmutablePair<JsonElement, Boolean> validateAndUpdate(JsonElement jsonElement, - DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) { + private ImmutablePair<JsonElement, Boolean> validateAndUpdate(JsonElement jsonElement, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) { Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition); @@ -100,14 +99,14 @@ public class DataTypeValidatorConverter { PropertyValueConverter converter = toscaPropertyType.getConverter(); if (jsonElement == null || true == jsonElement.isJsonNull()) { boolean valid = validator.isValid(null, null, allDataTypes); - if (false == valid) { - log.trace("Failed in validation of property {} from type {}", dataTypeDefinition.getName(), dataTypeDefinition.getName()); + if (!valid) { + log.trace("Failed in validation of property {} from type {}", dataTypeDefinition.getName(), dataTypeDefinition.getName()); return falseResult; } return new ImmutablePair<JsonElement, Boolean>(jsonElement, true); } else { - if (true == jsonElement.isJsonPrimitive()) { + if (jsonElement.isJsonPrimitive()) { String value = null; if (jsonElement != null) { if (jsonElement.toString().isEmpty()) { @@ -117,7 +116,7 @@ public class DataTypeValidatorConverter { } } boolean valid = validator.isValid(value, null, null); - if (false == valid) { + if (!valid) { log.trace("Failed in validation of property {} from type {}. Json primitive value is {}", dataTypeDefinition.getName(), dataTypeDefinition.getName(), value); return falseResult; } @@ -127,7 +126,7 @@ public class DataTypeValidatorConverter { try { element = jsonParser.parse(convertedValue); } catch (JsonSyntaxException e) { - log.debug("Failed to parse value {} of property {}. {}", convertedValue, dataTypeDefinition.getName(), e); + log.debug("Failed to parse value {} of property {} {}", convertedValue, dataTypeDefinition.getName(), e); return falseResult; } @@ -164,13 +163,13 @@ public class DataTypeValidatorConverter { PropertyDefinition propertyDefinition = allProperties.get(propName); if (propertyDefinition == null) { - log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName()); + log.debug("The property {} was not found under data type {}" ,propName, dataTypeDefinition.getName()); return falseResult; } String type = propertyDefinition.getType(); boolean isScalarType = ToscaPropertyType.isScalarType(type); - if (true == isScalarType) { + if (isScalarType) { ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type); if (propertyType == null) { log.debug("cannot find the {} under default tosca property types", type); @@ -179,8 +178,7 @@ public class DataTypeValidatorConverter { PropertyTypeValidator validator = propertyType.getValidator(); String innerType = null; if (propertyType == ToscaPropertyType.LIST || propertyType == ToscaPropertyType.MAP) { - if (propertyDefinition.getSchema() != null - && propertyDefinition.getSchema().getProperty() != null) { + if (propertyDefinition.getSchema() != null && propertyDefinition.getSchema().getProperty() != null) { innerType = propertyDefinition.getSchema().getProperty().getType(); if (innerType == null) { log.debug("Property type {} must have inner type in its declaration.", propertyType); @@ -215,7 +213,7 @@ public class DataTypeValidatorConverter { try { element = jsonParser.parse(convertedValue); } catch (JsonSyntaxException e) { - log.debug("Failed to parse value {} of type {}. {}", convertedValue, propertyType, e); + log.debug("Failed to parse value {} of type {}", convertedValue, propertyType, e); return falseResult; } } @@ -226,14 +224,13 @@ public class DataTypeValidatorConverter { DataTypeDefinition typeDefinition = allDataTypes.get(type); if (typeDefinition == null) { - log.debug("The data type {] cannot be found in the given data type list.", type); + log.debug("The data type {} cannot be found in the given data type list.", type); return falseResult; } - ImmutablePair<JsonElement, Boolean> isValid = validateAndUpdate(elementValue, - typeDefinition, allDataTypes); + ImmutablePair<JsonElement, Boolean> isValid = validateAndUpdate(elementValue, typeDefinition, allDataTypes); - if (false == isValid.getRight().booleanValue()) { + if (!isValid.getRight().booleanValue()) { log.debug("Failed in validation of value {} from type {}", (elementValue != null ? elementValue.toString() : null), typeDefinition.getName()); return falseResult; } @@ -254,8 +251,7 @@ public class DataTypeValidatorConverter { } - public ImmutablePair<JsonElement, Boolean> validateAndUpdate(String value, DataTypeDefinition dataTypeDefinition, - Map<String, DataTypeDefinition> allDataTypes) { + public ImmutablePair<JsonElement, Boolean> validateAndUpdate(String value, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) { ImmutablePair<JsonElement, Boolean> result = falseResult; @@ -308,8 +304,7 @@ public class DataTypeValidatorConverter { return value; } - public boolean isValid(String value, DataTypeDefinition dataTypeDefinition, - Map<String, DataTypeDefinition> allDataTypes) { + public boolean isValid(String value, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) { boolean result = false; @@ -321,7 +316,7 @@ public class DataTypeValidatorConverter { try { jsonElement = jsonParser.parse(value); } catch (JsonSyntaxException e) { - log.debug("Failed to parse the value {} from type {}. {}", value, dataTypeDefinition, e); + log.debug("Failed to parse the value {} from type {}", value, dataTypeDefinition, e); return false; } @@ -330,8 +325,7 @@ public class DataTypeValidatorConverter { return result; } - private boolean isValid(JsonElement jsonElement, DataTypeDefinition dataTypeDefinition, - Map<String, DataTypeDefinition> allDataTypes) { + private boolean isValid(JsonElement jsonElement, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) { Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition); @@ -342,8 +336,7 @@ public class DataTypeValidatorConverter { if (jsonElement == null || true == jsonElement.isJsonNull()) { boolean valid = validator.isValid(null, null, allDataTypes); if (false == valid) { - log.trace("Failed in validation of property " + dataTypeDefinition.getName() + " from type " - + dataTypeDefinition.getName()); + log.trace("Failed in validation of property {} from type {}", dataTypeDefinition.getName(), dataTypeDefinition.getName()); return false; } @@ -396,7 +389,7 @@ public class DataTypeValidatorConverter { PropertyDefinition propertyDefinition = allProperties.get(propName); if (propertyDefinition == null) { - log.debug("The property {} was not found under data tpye {}", propName, dataTypeDefinition.getName()); + log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName()); return false; } String type = propertyDefinition.getType(); @@ -411,11 +404,10 @@ public class DataTypeValidatorConverter { PropertyTypeValidator validator = propertyType.getValidator(); String innerType = null; if (propertyType == ToscaPropertyType.LIST || propertyType == ToscaPropertyType.MAP) { - if (propertyDefinition.getSchema() != null - && propertyDefinition.getSchema().getProperty() != null) { + if (propertyDefinition.getSchema() != null && propertyDefinition.getSchema().getProperty() != null) { innerType = propertyDefinition.getSchema().getProperty().getType(); if (innerType == null) { - log.debug("Property type {} must have inner type in its decleration.", propertyType); + log.debug("Property type {} must have inner type in its declaration.", propertyType); return false; } } @@ -440,7 +432,7 @@ public class DataTypeValidatorConverter { DataTypeDefinition typeDefinition = allDataTypes.get(type); if (typeDefinition == null) { - log.debug("The data type {} canot be found in the given data type list.", type); + log.debug("The data type {} cannot be found in the given data type list.", type); return false; } @@ -465,35 +457,4 @@ public class DataTypeValidatorConverter { } } - - // public ImmutablePair<String, Boolean> - // validateAndUpdateAndReturnString(String value, DataTypeDefinition - // dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) { - // - // ImmutablePair<JsonElement, Boolean> result = falseResult; - // - // if (value == null || value.isEmpty()) { - // return trueStringEmptyResult; - // } - // - // JsonElement jsonElement = null; - // try { - // jsonElement = jsonParser.parse(value); - // } catch (JsonSyntaxException e) { - // return falseStringEmptyResult; - // } - // - // result = validateAndUpdate(jsonElement, dataTypeDefinition, - // allDataTypes); - // - // if (result.right.booleanValue() == false) { - // log.debug("The value {} of property from type {} is invalid", value, dataTypeDefinition.getName()); - // return new ImmutablePair<String, Boolean>(value, false); - // } - // - // String valueFromJsonElement = getValueFromJsonElement(result.left); - // - // return new ImmutablePair<String, Boolean>(valueFromJsonElement, true); - // } - } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/ListValidator.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/ListValidator.java index 92834690b8..177dcb60ea 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/ListValidator.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/ListValidator.java @@ -84,7 +84,7 @@ public class ListValidator implements PropertyTypeValidator { innerValidator = ToscaPropertyType.JSON.getValidator(); break; default: - log.debug("inner Tosca Type is unknown: {}", innerToscaType); + log.debug("inner Tosca Type is unknown. {}", innerToscaType); return false; } @@ -92,12 +92,14 @@ public class ListValidator implements PropertyTypeValidator { log.debug("inner Tosca Type is: {}", innerType); boolean isValid = validateComplexInnerType(value, innerType, allDataTypes); - log.debug("Finish to validate value {} of list with inner type {}. result is: {}", value, innerType, isValid); + log.debug("Finish to validate value {} of list with inner type {}. result is {}",value,innerType,isValid); return isValid; } try { JsonArray jo = (JsonArray) jsonParser.parse(value); + if(ToscaPropertyType.JSON == innerToscaType) + return true; int size = jo.size(); for (int i = 0; i < size; i++) { JsonElement currentValue = jo.get(i); @@ -111,7 +113,7 @@ public class ListValidator implements PropertyTypeValidator { return true; } catch (JsonSyntaxException e) { - log.debug("Failed to parse json : {}. {}", value, e); + log.debug("Failed to parse json : {}", value, e); BeEcompErrorManager.getInstance().logBeInvalidJsonInput("List Validator"); } @@ -144,7 +146,7 @@ public class ListValidator implements PropertyTypeValidator { boolean isValid = dataTypeValidatorConverter.isValid(element, innerDataTypeDefinition, allDataTypes); if (isValid == false) { - log.debug("Cannot parse value {} from type {} in list parameter", currentValue, innerType); + log.debug("Cannot parse value {} from type {} in list parameter",currentValue,innerType); return false; } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/MapValidator.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/MapValidator.java index c8ffc3f4b8..7c86b6da1b 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/MapValidator.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/MapValidator.java @@ -101,7 +101,7 @@ public class MapValidator implements PropertyTypeValidator { innerValidator = ToscaPropertyType.JSON.getValidator(); break; default: - log.debug("inner Tosca Type is unknown: {}", innerToscaType); + log.debug("inner Tosca Type is unknown. {}", innerToscaType); return false; } @@ -109,7 +109,7 @@ public class MapValidator implements PropertyTypeValidator { log.debug("inner Tosca Type is: {}", innerType); boolean isValid = validateComplexInnerType(value, innerType, allDataTypes); - log.debug("Finish to validate value {} of map with inner type {}. Result is {}", value, innerType, isValid); + log.debug("Finish to validate value {} of map with inner type {}. result is {}",value,innerType,isValid); return isValid; } @@ -126,14 +126,14 @@ public class MapValidator implements PropertyTypeValidator { if (!innerValidator.isValid(element, null, allDataTypes) || !keyValidator.isValid(entry.getKey(), null, allDataTypes)) { - log.debug("validation of key : {}, element: {} failed", currentKey, entry.getValue()); + log.debug("validation of key : {}, element : {} failed", currentKey, entry.getValue()); return false; } } return true; } catch (JsonSyntaxException e) { - log.debug("Failed to parse json : {}. {}", value, e); + log.debug("Failed to parse json : {}", value, e); BeEcompErrorManager.getInstance().logBeInvalidJsonInput("Map Validator"); } @@ -162,15 +162,15 @@ public class MapValidator implements PropertyTypeValidator { String element = JsonUtils.toString(currentValue); boolean isValid = dataTypeValidatorConverter.isValid(element, innerDataTypeDefinition, allDataTypes); - if (isValid == false) { - log.debug("Cannot parse value {} from type {} of key {}", currentValue, innerType, currentKey); + if (!isValid) { + log.debug("Cannot parse value {} from type {} of key {}",currentValue,innerType,currentKey); return false; } } } } catch (Exception e) { - log.debug("Cannot parse value {} of map from inner type {}. {}", value, innerType, e); + log.debug("Cannot parse value {} of map from inner type {}", value, innerType, e); return false; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/PropertyTypeValidator.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/PropertyTypeValidator.java index 35862148f9..f0cf225c0a 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/PropertyTypeValidator.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/PropertyTypeValidator.java @@ -30,16 +30,16 @@ public interface PropertyTypeValidator { boolean isValid(String value, String innerType); /* - * The value format should be validated according to the “Property Type�? : - * “integer�? - valid tag:yaml.org,2002:int , the number base 8,10,18 should + * The value format should be validated according to the "Property Type" : + * "integer" - valid tag:yaml.org,2002:int , the number base 8,10,18 should * be handled ( hint : to validate by calling parseInt( * s,10)/parseInt(s,16)/parseInt(s,8) or just regexp [-+]?[0-9]+ for Base 10 * , [-+]?0[0-7]+ for Base 8 , [-+]?0x[0-9a-fA-F]+ for Base 16 * - * “float�? - valid tag:yaml.org,2002:float , parseFloat() “boolean�? - valid - * tag:yaml.org,2002:bool : can be only “true�? or “false�? ( upper case + * "float" - valid tag:yaml.org,2002:float , parseFloat() "boolean" - valid + * tag:yaml.org,2002:bool : can be only "true" or "false" ( upper case * characters should be converted to lower case : TRUE ->true, True->true - * “string�? - valid tag:yaml.org,2002:str and limited to 100 chars. + * "string" - valid tag:yaml.org,2002:str and limited to 100 chars. * */ diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/StringValidator.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/StringValidator.java index 06994505a9..750941cc0b 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/StringValidator.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/validators/StringValidator.java @@ -31,23 +31,19 @@ import org.slf4j.LoggerFactory; public class StringValidator implements PropertyTypeValidator { - public static final int DEFAULT_STRING_MAXIMUM_LENGTH = 100; + public static final int DEFAULT_STRING_MAXIMUM_LENGTH = 2500; - public static int STRING_MAXIMUM_LENGTH = DEFAULT_STRING_MAXIMUM_LENGTH; + private static int STRING_MAXIMUM_LENGTH = DEFAULT_STRING_MAXIMUM_LENGTH; private static Logger log = LoggerFactory.getLogger(StringValidator.class.getName()); private static StringValidator stringValidator = new StringValidator(); - public static StringValidator getInstance() { - return stringValidator; - } - private StringValidator() { if (ConfigurationManager.getConfigurationManager() != null) { ToscaValidatorsConfig toscaValidators = ConfigurationManager.getConfigurationManager().getConfiguration() .getToscaValidators(); - log.debug("toscaValidators={}", toscaValidators); + log.debug("toscaValidators= {}", toscaValidators); if (toscaValidators != null) { Integer stringMaxLength = toscaValidators.getStringMaxLength(); if (stringMaxLength != null) { @@ -57,6 +53,10 @@ public class StringValidator implements PropertyTypeValidator { } } + public static StringValidator getInstance() { + return stringValidator; + } + @Override public boolean isValid(String value, String innerType, Map<String, DataTypeDefinition> allDataTypes) { @@ -65,14 +65,14 @@ public class StringValidator implements PropertyTypeValidator { } if (value.length() > STRING_MAXIMUM_LENGTH) { - log.debug("parameter String length {} is higher the configured({})", value.length(), STRING_MAXIMUM_LENGTH); + log.debug("parameter String length {} is higher than configured({})", value.length(), STRING_MAXIMUM_LENGTH); return false; } String coverted = ValidationUtils.removeNoneUtf8Chars(value); boolean isValid = ValidationUtils.validateIsAscii(coverted); - if (false == isValid) { - log.debug("parameter String value {} is not ascii string.", (value != null ? value.substring(0, Math.min(value.length(), 20)) : null)); + if (false == isValid && log.isDebugEnabled()) { + log.debug("parameter String value {} is not an ascii string.", value.substring(0, Math.min(value.length(), 20))); } return isValid; |