[AAI] Fix doc config files
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / StateFormat.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.serialization.queryformats;
22
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;
32
33 import java.util.*;
34
35 public class StateFormat extends HistoryFormat {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(StateFormat.class);
38
39     protected StateFormat(HistoryFormat.Builder builder) {
40         super(builder);
41     }
42
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())) {
49                 continue;
50             }
51
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);
58                 jsonList.add(json);
59             }
60         }
61
62         JsonArray jsonArray = new JsonArray();
63         jsonList.stream().sorted(Comparator.comparingLong(o -> o.get(TIMESTAMP).getAsLong())).forEach(jsonArray::add);
64         return jsonArray;
65     }
66
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();
72         }
73
74         return startTs >= sTs && eTs > startTs;
75     }
76
77     @Override
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) {
82                 return false;
83             }
84         }
85         if (e.property(AAIProperties.START_TS).isPresent()) {
86             long edgeStartTs = e.value(AAIProperties.START_TS);
87             return startTs >= edgeStartTs;
88         }
89         return true;
90     }
91
92     @Override
93     protected JsonObject getRelatedObject(Edge e, Vertex related) throws AAIFormatVertexException {
94
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));
101         } else {
102             LOGGER.warn("Vertex {} is missing aai-uri", related.id());
103             json.addProperty("uri", "NA");
104         }
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");
108
109         return json;
110     }
111
112
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");
117     }
118
119     @Override
120     protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
121
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);
127
128         if (properties.size() > 0) {
129             json.add(PROPERTIES, properties);
130         } else {
131             return Optional.empty();
132         }
133         if (!nodesOnly) {
134             json.add(RELATED_TO, this.createRelationshipObject(v));
135         }
136         return Optional.of(json);
137     }
138
139     @Override
140     protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException {
141         return Optional.empty();
142     }
143
144 }