aboutsummaryrefslogtreecommitdiffstats
path: root/common-be
diff options
context:
space:
mode:
authorimamSidero <imam.hussain@est.tech>2023-05-18 17:56:32 +0100
committerimamSidero <imam.hussain@est.tech>2023-05-22 19:32:28 +0100
commite3f5545168163fdbf0d83314ee9dd51983e4bcc8 (patch)
tree07e4f458e8a7a8b9ea35af7b16147068d03e9299 /common-be
parent89b4860dbd57fb1a11fc30397a08e316e793572d (diff)
Provide index token to tosca function for nested lists
Index token capability is provided in tosca function for all nested levels of list and custom types Issue-ID: SDC-4505 Signed-off-by: Imam hussain <imam.hussain@est.tech> Change-Id: If21c0078e0d17c44b5a31b00d6fac3e18ff6831d
Diffstat (limited to 'common-be')
-rw-r--r--common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java38
-rw-r--r--common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinition.java27
-rw-r--r--common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java2
3 files changed, 43 insertions, 24 deletions
diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java
index de8e30b897..f77c6f949b 100644
--- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java
+++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java
@@ -116,7 +116,8 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction
toscaGetFunction.setSourceUniqueId(getAsTextOrElseNull(node, "sourceUniqueId"));
toscaGetFunction.setPropertyName(getAsTextOrElseNull(node, "propertyName"));
toscaGetFunction.setPropertyUniqueId(getAsTextOrElseNull(node, "propertyUniqueId"));
- toscaGetFunction.setToscaIndex(getNumberAsTextOrElseNull(node, "toscaIndex"));
+ toscaGetFunction.setToscaIndexList(getNumberAsTextOrElseNull(node, "toscaIndexList", context));
+
final String propertySource = getAsTextOrElseNull(node, "propertySource");
if (StringUtils.isNotEmpty(propertySource)) {
final PropertySource propertySource1 = PropertySource.findType(propertySource).orElseThrow(() ->
@@ -149,20 +150,31 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction
return jsonNode.asText();
}
- private Object getNumberAsTextOrElseNull(final JsonNode node, final String fieldName) {
+ private List<Object> getNumberAsTextOrElseNull(final JsonNode node, final String fieldName, final DeserializationContext context) throws JsonMappingException{
+ List<Object> toscaIndexList = new ArrayList<Object>();
final JsonNode jsonNode = node.get(fieldName);
- if (jsonNode == null) {
- return null;
- }
- if (jsonNode.asText().equalsIgnoreCase("INDEX")) {
- return jsonNode.asText();
- }
- try {
- Integer.parseInt(jsonNode.asText());
- } catch(Exception e) {
- return null;
+ if (jsonNode != null) {
+ if (!jsonNode.isArray()) {
+ throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for toscaIndexList attribute");
+ }
+ for (int index=0;index<jsonNode.size();index++) {
+ String textValue = jsonNode.get(index).asText();
+ if (index%2 == 0) {
+ if (textValue.equalsIgnoreCase("INDEX")) {
+ toscaIndexList.add(textValue);
+ } else {
+ try {
+ toscaIndexList.add(Integer.parseInt(textValue));
+ } catch(Exception e) {
+ throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting a valid value for toscaIndex attribute");
+ }
+ }
+ } else {
+ toscaIndexList.add(textValue);
+ }
+ }
}
- return Integer.parseInt(jsonNode.asText());
+ return toscaIndexList;
}
private ToscaConcatFunction deserializeConcatFunction(final JsonNode concatFunctionJsonNode,
diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinition.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinition.java
index 9396188fcc..5bd9fe33db 100644
--- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinition.java
+++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinition.java
@@ -44,7 +44,7 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct
private String sourceName;
private ToscaGetFunctionType functionType;
private List<String> propertyPathFromSource = new ArrayList<>();
- private Object toscaIndex;
+ private List<Object> toscaIndexList;
public ToscaGetFunctionDataDefinition() {
//necessary for JSON conversions
@@ -89,10 +89,13 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct
);
}
if (propertySource == PropertySource.SELF) {
- if (toscaIndex != null) {
- Object toscaIndexValue = StringUtils.isNumeric(toscaIndex.toString()) ? Integer.parseInt(toscaIndex.toString()) : toscaIndex;
+ if (toscaIndexList.size() > 0) {
+ List<Object> parsedIndexList = new ArrayList<Object>();
+ toscaIndexList.forEach((obj) -> {
+ parsedIndexList.add(StringUtils.isNumeric(obj.toString()) ? Integer.parseInt(obj.toString()) : obj);
+ });
return Map.of(functionType.getFunctionName(),
- Stream.concat(Stream.of(PropertySource.SELF.getName()), Stream.concat(propertyPathFromSource.stream(),Stream.of(toscaIndexValue))).collect(Collectors.toList())
+ Stream.concat(Stream.of(PropertySource.SELF.getName()), Stream.concat(propertyPathFromSource.stream(),parsedIndexList.stream())).collect(Collectors.toList())
);
}
return Map.of(functionType.getFunctionName(),
@@ -105,10 +108,13 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct
String.format("sourceName is required in order to generate the %s from INSTANCE value", functionType.getFunctionName())
);
}
- if (toscaIndex != null) {
- Object toscaIndexValue = StringUtils.isNumeric(toscaIndex.toString()) ? Integer.parseInt(toscaIndex.toString()) : toscaIndex;
+ if (toscaIndexList.size() > 0) {
+ List<Object> parsedIndexList = new ArrayList<Object>();
+ toscaIndexList.forEach((obj) -> {
+ parsedIndexList.add(StringUtils.isNumeric(obj.toString()) ? Integer.parseInt(obj.toString()) : obj);
+ });
return Map.of(functionType.getFunctionName(),
- Stream.concat(Stream.of(sourceName), Stream.concat(propertyPathFromSource.stream(),Stream.of(toscaIndexValue))).collect(Collectors.toList())
+ Stream.concat(Stream.of(sourceName), Stream.concat(propertyPathFromSource.stream(),parsedIndexList.stream())).collect(Collectors.toList())
);
}
return Map.of(functionType.getFunctionName(),
@@ -123,10 +129,9 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct
List<Object> propertySourceCopy = new ArrayList<Object>(this.propertyPathFromSource);
List<Object> propertySourceOneCopy = new ArrayList<>();
propertySourceOneCopy.add(this.propertyPathFromSource.get(0));
- if (toscaIndex != null) {
- Object toscaIndexValue = StringUtils.isNumeric(toscaIndex.toString()) ? Integer.parseInt(toscaIndex.toString()) : toscaIndex;
- propertySourceCopy.add(toscaIndexValue);
- propertySourceOneCopy.add(toscaIndexValue);
+ if (toscaIndexList.size() > 0) {
+ propertySourceCopy.addAll(toscaIndexList);
+ propertySourceOneCopy.addAll(toscaIndexList);
}
if (this.propertyPathFromSource.size() == 1) {
return Map.of(this.functionType.getFunctionName(), propertySourceOneCopy);
diff --git a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java
index 581f62a91f..3add149e23 100644
--- a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java
+++ b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java
@@ -28,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.gson.Gson;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -206,6 +207,7 @@ class ToscaGetFunctionDataDefinitionTest {
toscaGetFunction.setPropertySource(propertySource);
toscaGetFunction.setPropertyPathFromSource(propertyPath);
toscaGetFunction.setSourceName(sourceName);
+ toscaGetFunction.setToscaIndexList(new ArrayList<>());
return toscaGetFunction;
}