2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.serialization.queryformats;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonObject;
28 import org.apache.tinkerpop.gremlin.structure.Edge;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
31 import org.onap.aai.db.props.AAIProperties;
32 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class StateFormat extends HistoryFormat {
38 private static final Logger LOGGER = LoggerFactory.getLogger(StateFormat.class);
40 protected StateFormat(HistoryFormat.Builder builder) {
44 protected JsonArray createPropertiesObject(Vertex v) {
45 Iterator<VertexProperty<Object>> iter = v.properties();
46 List<JsonObject> jsonList = new ArrayList<>();
47 while (iter.hasNext()) {
48 VertexProperty<Object> prop = iter.next();
49 if (prop.key() != null && ignoredKeys.contains(prop.key())) {
53 JsonObject metaProperties = createMetaPropertiesObject(prop);
54 if (isTsInRange(metaProperties)) {
55 JsonObject json = new JsonObject();
56 json.addProperty(KEY, prop.key());
57 json = mapPropertyValues(json, VALUE, prop.value());
58 addMetaProperties(json, metaProperties);
63 JsonArray jsonArray = new JsonArray();
64 jsonList.stream().sorted(Comparator.comparingLong(o -> o.get(TIMESTAMP).getAsLong())).forEach(jsonArray::add);
68 private boolean isTsInRange(JsonObject metaProperties) {
69 long sTs = metaProperties.get(AAIProperties.START_TS).getAsLong();
70 long eTs = Long.MAX_VALUE;
71 if (metaProperties.has(AAIProperties.END_TS)) {
72 eTs = metaProperties.get(AAIProperties.END_TS).getAsLong();
75 return startTs >= sTs && eTs > startTs;
79 protected boolean isValidEdge(Edge e) {
80 if (e.property(AAIProperties.END_TS).isPresent()) {
81 long edgeEndTs = e.value(AAIProperties.END_TS);
82 if (startTs >= edgeEndTs) {
86 if (e.property(AAIProperties.START_TS).isPresent()) {
87 long edgeStartTs = e.value(AAIProperties.START_TS);
88 return startTs >= edgeStartTs;
94 protected JsonObject getRelatedObject(Edge e, Vertex related) throws AAIFormatVertexException {
96 JsonObject json = new JsonObject();
97 json.addProperty("relationship-label", e.label());
98 json.addProperty(NODE_TYPE, related.<String>value(AAIProperties.NODE_TYPE));
99 json.addProperty("url", this.urlBuilder.pathed(related));
100 if (related.property(AAIProperties.AAI_URI).isPresent()) {
101 json.addProperty("uri", related.<String>value(AAIProperties.AAI_URI));
103 LOGGER.warn("Vertex {} is missing aai-uri", related.id());
104 json.addProperty("uri", "NA");
106 json.addProperty(TIMESTAMP,
107 e.property(AAIProperties.START_TS).isPresent() ? e.value(AAIProperties.START_TS) : 0);
108 json.addProperty(SOT,
109 e.property(AAIProperties.SOURCE_OF_TRUTH).isPresent() ? e.value(AAIProperties.SOURCE_OF_TRUTH) : "");
110 json.addProperty(TX_ID,
111 e.property(AAIProperties.START_TX_ID).isPresent() ? e.value(AAIProperties.START_TX_ID) : "N/A");
116 protected void addMetaProperties(JsonObject json, JsonObject metaProperties) {
117 json.addProperty(TIMESTAMP,
118 metaProperties.get(AAIProperties.START_TS) != null
119 ? metaProperties.get(AAIProperties.START_TS).getAsLong()
121 json.addProperty(SOT,
122 metaProperties.get(AAIProperties.SOURCE_OF_TRUTH) != null
123 ? metaProperties.get(AAIProperties.SOURCE_OF_TRUTH).getAsString()
125 json.addProperty(TX_ID,
126 metaProperties.get(AAIProperties.START_TX_ID) != null
127 ? metaProperties.get(AAIProperties.START_TX_ID).getAsString()
132 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
134 JsonObject json = new JsonObject();
135 json.addProperty(NODE_TYPE, v.<String>value(AAIProperties.NODE_TYPE));
136 json.addProperty("url", this.urlBuilder.pathed(v));
137 json.addProperty("uri", v.property(AAIProperties.AAI_URI).value().toString());
138 JsonArray properties = this.createPropertiesObject(v);
140 if (properties.size() > 0) {
141 json.add(PROPERTIES, properties);
143 return Optional.empty();
146 json.add(RELATED_TO, this.createRelationshipObject(v));
148 return Optional.of(json);
152 protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties)
153 throws AAIFormatVertexException {
154 return Optional.empty();