X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=aai-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fserialization%2Fqueryformats%2FLifecycleFormat.java;h=74742c9898c6dcac8fe38dcf0aa3080e27e4ff57;hb=fbb02159b84435cf37221ae8ae5e0045167be15a;hp=57bb17c2ca235692ad21f71f7315cbdc51d30089;hpb=58f0fd2f6ef58de52e1f9a73540c1bb895c0d6e6;p=aai%2Faai-common.git diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/LifecycleFormat.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/LifecycleFormat.java index 57bb17c2..74742c98 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/LifecycleFormat.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/LifecycleFormat.java @@ -20,10 +20,13 @@ package org.onap.aai.serialization.queryformats; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.google.gson.JsonArray; import com.google.gson.JsonObject; + +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.structure.VertexProperty; @@ -31,14 +34,11 @@ import org.onap.aai.db.props.AAIProperties; import org.onap.aai.logging.LogFormatTools; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.Stream; - - public class LifecycleFormat extends HistoryFormat { private static final Logger LOGGER = LoggerFactory.getLogger(LifecycleFormat.class); @@ -52,11 +52,11 @@ public class LifecycleFormat extends HistoryFormat { Iterator> iter = v.properties(); List jsonList = new ArrayList<>(); - Map> propStartTimes = new HashMap<>(); //vertex end + Map> propStartTimes = new HashMap<>(); // vertex end while (iter.hasNext()) { JsonObject json = new JsonObject(); VertexProperty prop = iter.next(); - if(prop.key() != null && ignoredKeys.contains(prop.key())){ + if (prop.key() != null && ignoredKeys.contains(prop.key())) { continue; } if (!propStartTimes.containsKey(prop.key())) { @@ -79,10 +79,9 @@ public class LifecycleFormat extends HistoryFormat { jsonList.add(jo); propStartTimes.get(prop.key()).add(metaProperties.get(AAIProperties.START_TS).getAsLong()); } - if (!AAIProperties.RESOURCE_VERSION.equals(prop.key()) - && metaProperties.has(AAIProperties.END_TS) - && isTsInRange(metaProperties.get(AAIProperties.END_TS).getAsLong()) - && !propStartTimes.get(prop.key()).contains(metaProperties.get(AAIProperties.END_TS).getAsLong())) { + if (!AAIProperties.RESOURCE_VERSION.equals(prop.key()) && metaProperties.has(AAIProperties.END_TS) + && isTsInRange(metaProperties.get(AAIProperties.END_TS).getAsLong()) + && !propStartTimes.get(prop.key()).contains(metaProperties.get(AAIProperties.END_TS).getAsLong())) { JsonObject jo = new JsonObject(); jo.add(KEY, json.get(KEY)); jo.add(VALUE, null); @@ -94,16 +93,17 @@ public class LifecycleFormat extends HistoryFormat { } jsonList.stream() - // remove all the null values that is the start time for another value - .filter(jo -> !jo.get(VALUE).isJsonNull() || !propStartTimes.get(jo.get(KEY).getAsString()).contains(jo.get(TIMESTAMP).getAsLong())) - // sort by ts in decreasing order - .sorted((o1, o2) -> { - if (o1.get(TIMESTAMP).getAsLong() == o2.get(TIMESTAMP).getAsLong()) { - return o1.get(KEY).getAsString().compareTo(o2.get(KEY).getAsString()); - } else { - return Long.compare(o2.get(TIMESTAMP).getAsLong(), o1.get(TIMESTAMP).getAsLong()); - } - }).forEach(jsonArray::add); + // remove all the null values that is the start time for another value + .filter(jo -> !jo.get(VALUE).isJsonNull() + || !propStartTimes.get(jo.get(KEY).getAsString()).contains(jo.get(TIMESTAMP).getAsLong())) + // sort by ts in decreasing order + .sorted((o1, o2) -> { + if (o1.get(TIMESTAMP).getAsLong() == o2.get(TIMESTAMP).getAsLong()) { + return o1.get(KEY).getAsString().compareTo(o2.get(KEY).getAsString()); + } else { + return Long.compare(o2.get(TIMESTAMP).getAsLong(), o1.get(TIMESTAMP).getAsLong()); + } + }).forEach(jsonArray::add); return jsonArray; } @@ -112,7 +112,6 @@ public class LifecycleFormat extends HistoryFormat { return ts >= startTs && ts <= endTs; } - @Override protected boolean isValidEdge(Edge e) { if (e.property(AAIProperties.END_TS).isPresent()) { @@ -139,21 +138,27 @@ public class LifecycleFormat extends HistoryFormat { json.addProperty("uri", "NA"); } - if(e.property(AAIProperties.START_TS).isPresent()) { + if (e.property(AAIProperties.START_TS).isPresent()) { long edgeStartTimestamp = e.value(AAIProperties.START_TS); if (isTsInRange(edgeStartTimestamp)) { - json.addProperty(TIMESTAMP, e.property(AAIProperties.START_TS).isPresent()? e.value(AAIProperties.START_TS) : 0); - json.addProperty(SOT, e.property(AAIProperties.SOURCE_OF_TRUTH).isPresent()? e.value(AAIProperties.SOURCE_OF_TRUTH) : ""); - json.addProperty(TX_ID, e.property(AAIProperties.START_TX_ID).isPresent()? e.value(AAIProperties.START_TX_ID) : "N/A"); + json.addProperty(TIMESTAMP, + e.property(AAIProperties.START_TS).isPresent() ? e.value(AAIProperties.START_TS) : 0); + json.addProperty(SOT, + e.property(AAIProperties.SOURCE_OF_TRUTH).isPresent() ? e.value(AAIProperties.SOURCE_OF_TRUTH) + : ""); + json.addProperty(TX_ID, + e.property(AAIProperties.START_TX_ID).isPresent() ? e.value(AAIProperties.START_TX_ID) : "N/A"); } } - if(e.property(AAIProperties.END_TS).isPresent()) { + if (e.property(AAIProperties.END_TS).isPresent()) { long edgeEndTimestamp = e.value(AAIProperties.END_TS); if (isTsInRange(edgeEndTimestamp)) { json.addProperty(END_TIMESTAMP, edgeEndTimestamp); - json.addProperty(END_SOT, e.property(AAIProperties.END_SOT).isPresent() ? e.value(AAIProperties.END_SOT) : ""); - json.addProperty(END_TX_ID, e.property(AAIProperties.END_TX_ID).isPresent() ? e.value(AAIProperties.END_TX_ID) : "N/A"); + json.addProperty(END_SOT, + e.property(AAIProperties.END_SOT).isPresent() ? e.value(AAIProperties.END_SOT) : ""); + json.addProperty(END_TX_ID, + e.property(AAIProperties.END_TX_ID).isPresent() ? e.value(AAIProperties.END_TX_ID) : "N/A"); } } @@ -182,14 +187,14 @@ public class LifecycleFormat extends HistoryFormat { json.add(NODE_ACTIONS, getNodeActions(v, json)); if (json.getAsJsonObject().get(PROPERTIES).getAsJsonArray().size() == 0 - && json.getAsJsonObject().get(RELATED_TO).getAsJsonArray().size() == 0 - && json.getAsJsonObject().get(NODE_ACTIONS).getAsJsonArray().size() == 0) { + && json.getAsJsonObject().get(RELATED_TO).getAsJsonArray().size() == 0 + && json.getAsJsonObject().get(NODE_ACTIONS).getAsJsonArray().size() == 0) { return Optional.empty(); } else if (json.getAsJsonObject().get(PROPERTIES).getAsJsonArray().size() == 1 - && (json.getAsJsonObject().get(RELATED_TO).getAsJsonArray().size() > 0 - || json.getAsJsonObject().get(NODE_ACTIONS).getAsJsonArray().size() > 0)) { - if (json.getAsJsonObject().get(PROPERTIES).getAsJsonArray() - .get(0).getAsJsonObject().get("key").getAsString().equals(AAIProperties.END_TS)) { + && (json.getAsJsonObject().get(RELATED_TO).getAsJsonArray().size() > 0 + || json.getAsJsonObject().get(NODE_ACTIONS).getAsJsonArray().size() > 0)) { + if (json.getAsJsonObject().get(PROPERTIES).getAsJsonArray().get(0).getAsJsonObject().get("key") + .getAsString().equals(AAIProperties.END_TS)) { json.getAsJsonObject().add(PROPERTIES, new JsonArray()); } } @@ -198,7 +203,8 @@ public class LifecycleFormat extends HistoryFormat { } @Override - protected Optional getJsonFromVertex(Vertex input, Map> properties) throws AAIFormatVertexException { + protected Optional getJsonFromVertex(Vertex input, Map> properties) + throws AAIFormatVertexException { return Optional.empty(); } @@ -212,7 +218,8 @@ public class LifecycleFormat extends HistoryFormat { action.addProperty("action", "DELETED"); action.addProperty(TIMESTAMP, deletedTs); if (v.property(AAIProperties.END_TS).property(AAIProperties.SOURCE_OF_TRUTH).isPresent()) { - action.addProperty(SOT, v.property(AAIProperties.END_TS).value(AAIProperties.SOURCE_OF_TRUTH)); + action.addProperty(SOT, + v.property(AAIProperties.END_TS).value(AAIProperties.SOURCE_OF_TRUTH)); } if (v.property(AAIProperties.END_TS).property(AAIProperties.END_TX_ID).isPresent()) { action.addProperty(TX_ID, v.property(AAIProperties.END_TS).value(AAIProperties.END_TX_ID)); @@ -229,7 +236,8 @@ public class LifecycleFormat extends HistoryFormat { action.addProperty(TIMESTAMP, createdTs); action.addProperty(SOT, v.value(AAIProperties.SOURCE_OF_TRUTH)); if (v.property(AAIProperties.SOURCE_OF_TRUTH).property(AAIProperties.START_TX_ID).isPresent()) { - action.addProperty(TX_ID, v.property(AAIProperties.SOURCE_OF_TRUTH).value(AAIProperties.START_TX_ID)); + action.addProperty(TX_ID, + v.property(AAIProperties.SOURCE_OF_TRUTH).value(AAIProperties.START_TX_ID)); } else { action.addProperty(TX_ID, "N/A"); } @@ -259,17 +267,15 @@ public class LifecycleFormat extends HistoryFormat { } return Optional.empty(); - }).filter(Optional::isPresent) - .map(Optional::get) - .forEach(json -> { - if (isParallel) { - synchronized (body) { - body.add(json); - } - } else { + }).filter(Optional::isPresent).map(Optional::get).forEach(json -> { + if (isParallel) { + synchronized (body) { body.add(json); } - }); + } else { + body.add(json); + } + }); JsonArray result = organizeBody(body); result.forEach(jsonElement -> jsonElement.getAsJsonObject().remove(TIMESTAMP)); return result; @@ -282,21 +288,23 @@ public class LifecycleFormat extends HistoryFormat { toBeMerged.add(body.get(i).getAsJsonObject().get("uri").getAsString(), i); } - final List> dupes = toBeMerged.values().stream().filter(l -> l.size() > 1).collect(Collectors.toList()); + final List> dupes = + toBeMerged.values().stream().filter(l -> l.size() > 1).collect(Collectors.toList()); if (dupes.isEmpty()) { return body; } else { Set remove = new HashSet<>(); for (List dupe : dupes) { - dupe.sort((a,b) -> Long.compare(body.get(b).getAsJsonObject().get(TIMESTAMP).getAsLong(), body.get(a).getAsJsonObject().get(TIMESTAMP).getAsLong())); + dupe.sort((a, b) -> Long.compare(body.get(b).getAsJsonObject().get(TIMESTAMP).getAsLong(), + body.get(a).getAsJsonObject().get(TIMESTAMP).getAsLong())); int keep = dupe.remove(0); for (Integer idx : dupe) { body.get(keep).getAsJsonObject().getAsJsonArray(NODE_ACTIONS) - .addAll(body.get(idx).getAsJsonObject().getAsJsonArray(NODE_ACTIONS)); + .addAll(body.get(idx).getAsJsonObject().getAsJsonArray(NODE_ACTIONS)); body.get(keep).getAsJsonObject().getAsJsonArray(PROPERTIES) - .addAll(body.get(idx).getAsJsonObject().getAsJsonArray(PROPERTIES)); + .addAll(body.get(idx).getAsJsonObject().getAsJsonArray(PROPERTIES)); body.get(keep).getAsJsonObject().getAsJsonArray(RELATED_TO) - .addAll(body.get(idx).getAsJsonObject().getAsJsonArray(RELATED_TO)); + .addAll(body.get(idx).getAsJsonObject().getAsJsonArray(RELATED_TO)); remove.add(idx); } }