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 org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonObject;
27 import org.apache.tinkerpop.gremlin.structure.Edge;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
30 import org.onap.aai.db.props.AAIProperties;
31 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
35 public class StateFormat extends HistoryFormat {
37 private static final Logger LOGGER = LoggerFactory.getLogger(StateFormat.class);
39 protected StateFormat(HistoryFormat.Builder builder) {
43 protected JsonArray createPropertiesObject(Vertex v) {
44 Iterator<VertexProperty<Object>> iter = v.properties();
45 List<JsonObject> jsonList = new ArrayList<>();
46 while (iter.hasNext()) {
47 VertexProperty<Object> prop = iter.next();
48 if (prop.key() != null && ignoredKeys.contains(prop.key())) {
52 JsonObject metaProperties = createMetaPropertiesObject(prop);
53 if (isTsInRange(metaProperties)) {
54 JsonObject json = new JsonObject();
55 json.addProperty(KEY, prop.key());
56 json = mapPropertyValues(json, VALUE, prop.value());
57 addMetaProperties(json, metaProperties);
62 JsonArray jsonArray = new JsonArray();
63 jsonList.stream().sorted(Comparator.comparingLong(o -> o.get(TIMESTAMP).getAsLong())).forEach(jsonArray::add);
67 private boolean isTsInRange(JsonObject metaProperties) {
68 long sTs = metaProperties.get(AAIProperties.START_TS).getAsLong();
69 long eTs = Long.MAX_VALUE;
70 if (metaProperties.has(AAIProperties.END_TS)) {
71 eTs = metaProperties.get(AAIProperties.END_TS).getAsLong();
74 return startTs >= sTs && eTs > startTs;
78 protected boolean isValidEdge(Edge e) {
79 if (e.property(AAIProperties.END_TS).isPresent()) {
80 long edgeEndTs = e.value(AAIProperties.END_TS);
81 if (startTs >= edgeEndTs) {
85 if (e.property(AAIProperties.START_TS).isPresent()) {
86 long edgeStartTs = e.value(AAIProperties.START_TS);
87 return startTs >= edgeStartTs;
93 protected JsonObject getRelatedObject(Edge e, Vertex related) throws AAIFormatVertexException {
95 JsonObject json = new JsonObject();
96 json.addProperty("relationship-label", e.label());
97 json.addProperty(NODE_TYPE, related.<String>value(AAIProperties.NODE_TYPE));
98 json.addProperty("url", this.urlBuilder.pathed(related));
99 if (related.property(AAIProperties.AAI_URI).isPresent()) {
100 json.addProperty("uri", related.<String>value(AAIProperties.AAI_URI));
102 LOGGER.warn("Vertex {} is missing aai-uri", related.id());
103 json.addProperty("uri", "NA");
105 json.addProperty(TIMESTAMP, e.property(AAIProperties.START_TS).isPresent()? e.value(AAIProperties.START_TS) : 0);
106 json.addProperty(SOT, e.property(AAIProperties.SOURCE_OF_TRUTH).isPresent()? e.value(AAIProperties.SOURCE_OF_TRUTH) : "");
107 json.addProperty(TX_ID, e.property(AAIProperties.START_TX_ID).isPresent()? e.value(AAIProperties.START_TX_ID) : "N/A");
113 protected void addMetaProperties(JsonObject json, JsonObject metaProperties) {
114 json.addProperty(TIMESTAMP, metaProperties.get(AAIProperties.START_TS) != null ? metaProperties.get(AAIProperties.START_TS).getAsLong() : 0);
115 json.addProperty(SOT, metaProperties.get(AAIProperties.SOURCE_OF_TRUTH) != null ? metaProperties.get(AAIProperties.SOURCE_OF_TRUTH).getAsString() : "");
116 json.addProperty(TX_ID, metaProperties.get(AAIProperties.START_TX_ID) != null ? metaProperties.get(AAIProperties.START_TX_ID).getAsString() : "N/A");
120 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
122 JsonObject json = new JsonObject();
123 json.addProperty(NODE_TYPE, v.<String>value(AAIProperties.NODE_TYPE));
124 json.addProperty("url", this.urlBuilder.pathed(v));
125 json.addProperty("uri", v.property(AAIProperties.AAI_URI).value().toString());
126 JsonArray properties = this.createPropertiesObject(v);
128 if (properties.size() > 0) {
129 json.add(PROPERTIES, properties);
131 return Optional.empty();
134 json.add(RELATED_TO, this.createRelationshipObject(v));
136 return Optional.of(json);
140 protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException {
141 return Optional.empty();