From 89d7125d6ae8a3f172ce7d6dc9546443d9477f5b Mon Sep 17 00:00:00 2001 From: JvD_Ericsson Date: Tue, 14 Feb 2023 11:53:07 +0000 Subject: Support for copy/paste tosca functions into operation inputs Issue-ID: SDC-4394 Signed-off-by: JvD_Ericsson Change-Id: I4aedb641e950419677d2509f4944fcf778c2efd8 --- .../tosca/converters/ToscaListValueConverter.java | 23 ++++----------- .../tosca/converters/ToscaMapValueConverter.java | 34 +++++++--------------- .../tosca/converters/ToscaValueBaseConverter.java | 32 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 42 deletions(-) (limited to 'catalog-model/src/main/java') 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 67894bdade..f449f7841b 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 @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.openecomp.sdc.be.model.tosca.converters; import com.google.gson.JsonArray; @@ -32,7 +33,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import org.jetbrains.annotations.Nullable; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; @@ -93,12 +93,11 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements final boolean isScalarF = isScalar; final ToscaValueConverter innerConverterFinal = innerConverter; asJsonArray.forEach(e -> { - Object convertedValue = null; + Object convertedValue; if (isScalarF) { - if (e.isJsonPrimitive()) { - String jsonAsString = e.getAsString(); - log.debug("try to convert scalar value {}", jsonAsString); - convertedValue = innerConverterFinal.convertToToscaValue(jsonAsString, innerType, dataTypes); + if (isJsonElementAJsonPrimitive(e)) { + log.debug("try to convert scalar value {}", e.getAsString()); + convertedValue = innerConverterFinal.convertToToscaValue(e.getAsString(), innerType, dataTypes); } else { convertedValue = handleComplexJsonValue(e); } @@ -154,16 +153,4 @@ public class ToscaListValueConverter extends ToscaValueBaseConverter implements return null; } } - - private JsonElement parseToJson(final String value) { - try { - final StringReader reader = new StringReader(value); - final JsonReader jsonReader = new JsonReader(reader); - jsonReader.setLenient(true); - return JsonParser.parseReader(jsonReader); - } catch (final JsonSyntaxException e) { - log.debug("convertToToscaValue failed to parse json value :", e); - 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 2cc868b17c..76d9d2bf47 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 @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.openecomp.sdc.be.model.tosca.converters; import com.google.gson.JsonArray; @@ -67,7 +68,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T } else { DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType); if (dataTypeDefinition != null) { - ToscaPropertyType toscaPropertyType = null; + ToscaPropertyType toscaPropertyType; if ((toscaPropertyType = isScalarType(dataTypeDefinition)) != null) { innerConverter = toscaPropertyType.getValueConverter(); } else { @@ -85,15 +86,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T } } JsonElement jsonElement; - try { - StringReader reader = new StringReader(value); - JsonReader jsonReader = new JsonReader(reader); - jsonReader.setLenient(true); - jsonElement = JsonParser.parseReader(jsonReader); - } catch (JsonSyntaxException e) { - log.debug("convertToToscaValue failed to parse json value :", e); - return null; - } + jsonElement = parseToJson(value); if (jsonElement == null || jsonElement.isJsonNull()) { log.debug("convertToToscaValue json element is null"); return null; @@ -127,9 +120,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T propType = pd.getType(); final DataTypeDefinition pdDataType = dataTypes.get(propType); final ToscaPropertyType toscaPropType = isScalarType(pdDataType); - if (toscaPropType == null) { - scalar = false; - } else { + if (toscaPropType != null) { scalar = true; propType = toscaPropType.getType(); innerConverterProp = toscaPropType.getValueConverter(); @@ -144,14 +135,9 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T public Object convertDataTypeToToscaObject(String innerType, Map dataTypes, ToscaValueConverter innerConverter, final boolean isScalarF, JsonElement entryValue, boolean preserveEmptyValue) { - Object convertedValue = null; - if (isScalarF && entryValue.isJsonPrimitive()) { - log.debug("try convert scalar value "); - if (entryValue.getAsString() == null) { - convertedValue = null; - } else { - convertedValue = innerConverter.convertToToscaValue(entryValue.getAsString(), innerType, dataTypes); - } + Object convertedValue; + if (isScalarF && isJsonElementAJsonPrimitive(entryValue)) { + return innerConverter.convertToToscaValue(entryValue.getAsString(), innerType, dataTypes); } else { if (entryValue.isJsonPrimitive()) { return handleComplexJsonValue(entryValue); @@ -201,11 +187,11 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T String type = propertyDefinition.getType(); ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type); if (propertyType != null) { - if (elementValue.isJsonPrimitive()) { + if (isJsonElementAJsonPrimitive(elementValue)) { ToscaValueConverter valueConverter = propertyType.getValueConverter(); convValue = valueConverter.convertToToscaValue(elementValue.getAsString(), type, dataTypes); } else { - if (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(propertyType)) { + if (ToscaPropertyType.MAP.equals(propertyType) || ToscaPropertyType.LIST.equals(propertyType)) { ToscaValueConverter valueConverter = propertyType.getValueConverter(); String json = gson.toJson(elementValue); String innerTypeRecursive = propertyDefinition.getSchema().getProperty().getType(); @@ -219,7 +205,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T } } } else { - if (elementValue.isJsonPrimitive()) { + if (isJsonElementAJsonPrimitive(elementValue)) { convValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive()); } else { convValue = handleComplexJsonValue(elementValue); 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 7505d2af88..c9b5db8e87 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 @@ -17,13 +17,18 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.openecomp.sdc.be.model.tosca.converters; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSyntaxException; +import com.google.gson.stream.JsonReader; +import java.io.StringReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -91,6 +96,9 @@ public class ToscaValueBaseConverter { return handleJsonArray(jsonElement); } if (jsonElement.isJsonPrimitive()) { + if (!isJsonElementAJsonPrimitive(jsonElement)) { + return handleComplexJsonValue(parseToJson(jsonElement.getAsString())); + } return json2JavaPrimitive(jsonElement.getAsJsonPrimitive()); } log.debug("JSON type '{}' not supported", jsonElement); @@ -137,4 +145,28 @@ public class ToscaValueBaseConverter { } throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive)); } + + public JsonElement parseToJson(final String value) { + try { + final StringReader reader = new StringReader(value); + final JsonReader jsonReader = new JsonReader(reader); + jsonReader.setLenient(true); + return JsonParser.parseReader(jsonReader); + } catch (final JsonSyntaxException e) { + log.debug("convertToToscaValue failed to parse json value :", e); + return null; + } + } + + public boolean isJsonElementAJsonPrimitive(JsonElement jsonElement) { + if (!jsonElement.isJsonPrimitive()) { + return false; + } + String elementAsString = jsonElement.getAsString(); + JsonElement elementAsJson = parseToJson(elementAsString); + if (elementAsJson.isJsonPrimitive() || elementAsJson.isJsonNull()) { + return true; + } + return false; + } } -- cgit 1.2.3-korg