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