Merge "[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 com.google.gson.JsonArray;
24 import com.google.gson.JsonObject;
25
26 import java.util.*;
27
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;
35
36 public class StateFormat extends HistoryFormat {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(StateFormat.class);
39
40     protected StateFormat(HistoryFormat.Builder builder) {
41         super(builder);
42     }
43
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())) {
50                 continue;
51             }
52
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);
59                 jsonList.add(json);
60             }
61         }
62
63         JsonArray jsonArray = new JsonArray();
64         jsonList.stream().sorted(Comparator.comparingLong(o -> o.get(TIMESTAMP).getAsLong())).forEach(jsonArray::add);
65         return jsonArray;
66     }
67
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();
73         }
74
75         return startTs >= sTs && eTs > startTs;
76     }
77
78     @Override
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) {
83                 return false;
84             }
85         }
86         if (e.property(AAIProperties.START_TS).isPresent()) {
87             long edgeStartTs = e.value(AAIProperties.START_TS);
88             return startTs >= edgeStartTs;
89         }
90         return true;
91     }
92
93     @Override
94     protected JsonObject getRelatedObject(Edge e, Vertex related) throws AAIFormatVertexException {
95
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));
102         } else {
103             LOGGER.warn("Vertex {} is missing aai-uri", related.id());
104             json.addProperty("uri", "NA");
105         }
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");
112
113         return json;
114     }
115
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()
120                         : 0);
121         json.addProperty(SOT,
122                 metaProperties.get(AAIProperties.SOURCE_OF_TRUTH) != null
123                         ? metaProperties.get(AAIProperties.SOURCE_OF_TRUTH).getAsString()
124                         : "");
125         json.addProperty(TX_ID,
126                 metaProperties.get(AAIProperties.START_TX_ID) != null
127                         ? metaProperties.get(AAIProperties.START_TX_ID).getAsString()
128                         : "N/A");
129     }
130
131     @Override
132     protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
133
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);
139
140         if (properties.size() > 0) {
141             json.add(PROPERTIES, properties);
142         } else {
143             return Optional.empty();
144         }
145         if (!nodesOnly) {
146             json.add(RELATED_TO, this.createRelationshipObject(v));
147         }
148         return Optional.of(json);
149     }
150
151     @Override
152     protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties)
153             throws AAIFormatVertexException {
154         return Optional.empty();
155     }
156
157 }