4443ade8867f44dacede5d61e6bc01e98328fb37
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / HistoryFormat.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  * <p>
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * <p>
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.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonObject;
26
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32
33 import org.apache.tinkerpop.gremlin.structure.Direction;
34 import org.apache.tinkerpop.gremlin.structure.Edge;
35 import org.apache.tinkerpop.gremlin.structure.Property;
36 import org.apache.tinkerpop.gremlin.structure.Vertex;
37 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
38 import org.onap.aai.db.props.AAIProperties;
39 import org.onap.aai.introspection.Loader;
40 import org.onap.aai.serialization.db.DBSerializer;
41 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
42 import org.onap.aai.serialization.queryformats.params.Depth;
43 import org.onap.aai.serialization.queryformats.params.EndTs;
44 import org.onap.aai.serialization.queryformats.params.NodesOnly;
45 import org.onap.aai.serialization.queryformats.params.StartTs;
46 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public abstract class HistoryFormat extends MultiFormatMapper {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(HistoryFormat.class);
53
54     protected static final String KEY = "key";
55     protected static final String VALUE = "value";
56     protected static final String TIMESTAMP = "timestamp";
57     protected static final String NODE_TYPE = "node-type";
58     protected static final String END_TIMESTAMP = "end-timestamp";
59     protected static final String SOT = "sot";
60     protected static final String END_SOT = "end-sot";
61     protected static final String TX_ID = "tx-id";
62     protected static final String END_TX_ID = "end-tx-id";
63     protected static final String PROPERTIES = "properties";
64     protected static final String RELATED_TO = "related-to";
65     protected static final String NODE_ACTIONS = "node-actions";
66
67     protected final DBSerializer serializer;
68     protected final Loader loader;
69     protected final UrlBuilder urlBuilder;
70     protected final int depth;
71     protected final boolean nodesOnly;
72     protected long startTs;
73     protected long endTs;
74     protected static final Set<String> ignoredKeys =
75             Stream.of(AAIProperties.LAST_MOD_TS, AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, AAIProperties.CREATED_TS)
76                     .collect(Collectors.toSet());
77
78     protected HistoryFormat(Builder builder) {
79         this.urlBuilder = builder.getUrlBuilder();
80         this.loader = builder.getLoader();
81         this.serializer = builder.getSerializer();
82         this.depth = builder.getDepth();
83         this.nodesOnly = builder.isNodesOnly();
84         this.startTs = builder.getStartTs();
85         this.endTs = builder.getEndTs();
86     }
87
88     @Override
89     public int parallelThreshold() {
90         return 100;
91     }
92
93     protected JsonObject createMetaPropertiesObject(VertexProperty<Object> prop) {
94         JsonObject json = new JsonObject();
95         Iterator iter = prop.properties();
96
97         while (iter.hasNext()) {
98             Property<Object> metaProp = (Property) iter.next();
99             mapPropertyValues(json, metaProp.key(), metaProp.value());
100         }
101
102         return json;
103     }
104
105     protected JsonObject mapPropertyValues(JsonObject json, String propertyKey, Object propertyValue) {
106         if (propertyValue instanceof String) {
107             json.addProperty(propertyKey, (String) propertyValue);
108         } else if (propertyValue instanceof Boolean) {
109             json.addProperty(propertyKey, (Boolean) propertyValue);
110         } else if (propertyValue instanceof Number) {
111             json.addProperty(propertyKey, (Number) propertyValue);
112         } else {
113             if (!(propertyValue instanceof List)) {
114                 return json;
115             }
116
117             Gson gson = new Gson();
118             String list = gson.toJson(propertyValue);
119             json.addProperty(propertyKey, list);
120         }
121         return json;
122     }
123
124     protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
125         JsonArray relatedToList = new JsonArray();
126         Iterator<Edge> inIter = v.edges(Direction.IN);
127         Iterator<Edge> outIter = v.edges(Direction.OUT);
128
129         while (inIter.hasNext()) {
130             Edge e = inIter.next();
131             if (isValidEdge(e)) {
132                 relatedToList.add(getRelatedObject(e, e.outVertex()));
133             }
134         }
135
136         while (outIter.hasNext()) {
137             Edge e = outIter.next();
138             if (isValidEdge(e)) {
139                 relatedToList.add(getRelatedObject(e, e.inVertex()));
140             }
141         }
142
143         return relatedToList;
144
145     }
146
147     protected abstract boolean isValidEdge(Edge e);
148
149     protected abstract JsonObject getRelatedObject(Edge e, Vertex related) throws AAIFormatVertexException;
150
151     public static class Builder implements NodesOnly<Builder>, Depth<Builder>, StartTs<Builder>, EndTs<Builder> {
152
153         protected final Loader loader;
154         protected final DBSerializer serializer;
155         protected final UrlBuilder urlBuilder;
156         protected boolean includeUrl = false;
157         protected boolean nodesOnly = false;
158         protected int depth = 1;
159         protected boolean modelDriven = false;
160         protected long startTs = -1;
161         protected long endTs = -1;
162
163         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
164             this.loader = loader;
165             this.serializer = serializer;
166             this.urlBuilder = urlBuilder;
167         }
168
169         protected Loader getLoader() {
170             return this.loader;
171         }
172
173         protected DBSerializer getSerializer() {
174             return this.serializer;
175         }
176
177         protected UrlBuilder getUrlBuilder() {
178             return this.urlBuilder;
179         }
180
181         public Builder includeUrl() {
182             this.includeUrl = true;
183             return this;
184         }
185
186         public Builder nodesOnly(Boolean nodesOnly) {
187             this.nodesOnly = nodesOnly;
188             return this;
189         }
190
191         public Builder startTs(String startTs) {
192             this.startTs = Long.parseLong(startTs);
193             return this;
194         }
195
196         public Builder endTs(String endTs) {
197             this.endTs = Long.parseLong(endTs);
198             return this;
199         }
200
201         public boolean isNodesOnly() {
202             return this.nodesOnly;
203         }
204
205         public Builder depth(Integer depth) {
206             this.depth = depth;
207             return this;
208         }
209
210         public int getDepth() {
211             return this.depth;
212         }
213
214         public boolean isIncludeUrl() {
215             return this.includeUrl;
216         }
217
218         public Builder modelDriven() {
219             this.modelDriven = true;
220             return this;
221         }
222
223         public boolean getModelDriven() {
224             return this.modelDriven;
225         }
226
227         public long getStartTs() {
228             return this.startTs;
229         }
230
231         public long getEndTs() {
232             return this.endTs;
233         }
234
235         public HistoryFormat build(Format format) {
236
237             if (Format.state.equals(format)) {
238                 return new StateFormat(this);
239             } else {
240                 return new LifecycleFormat(this);
241             }
242
243         }
244     }
245
246 }