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 com.google.gson.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
28 import java.util.Iterator;
29 import java.util.List;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
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;
51 public abstract class HistoryFormat extends MultiFormatMapper {
53 private static final Logger LOGGER = LoggerFactory.getLogger(HistoryFormat.class);
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";
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;
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());
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();
91 public int parallelThreshold() {
95 protected JsonObject createMetaPropertiesObject(VertexProperty<Object> prop) {
96 JsonObject json = new JsonObject();
97 Iterator<Property<Object>> iter = prop.properties();
99 while (iter.hasNext()) {
100 Property<Object> metaProp = iter.next();
101 mapPropertyValues(json, metaProp.key(), metaProp.value());
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);
115 if (!(propertyValue instanceof List)) {
119 Gson gson = new Gson();
120 String list = gson.toJson(propertyValue);
121 json.addProperty(propertyKey, list);
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);
131 while (inIter.hasNext()) {
132 Edge e = inIter.next();
133 if (isValidEdge(e)) {
134 relatedToList.add(getRelatedObject(e, e.outVertex()));
138 while (outIter.hasNext()) {
139 Edge e = outIter.next();
140 if (isValidEdge(e)) {
141 relatedToList.add(getRelatedObject(e, e.inVertex()));
145 return relatedToList;
149 protected abstract boolean isValidEdge(Edge e);
151 protected abstract JsonObject getRelatedObject(Edge e, Vertex related) throws AAIFormatVertexException;
153 public static class Builder implements NodesOnly<Builder>, Depth<Builder>, StartTs<Builder>, EndTs<Builder> {
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;
165 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
166 this.loader = loader;
167 this.serializer = serializer;
168 this.urlBuilder = urlBuilder;
171 protected Loader getLoader() {
175 protected DBSerializer getSerializer() {
176 return this.serializer;
179 protected UrlBuilder getUrlBuilder() {
180 return this.urlBuilder;
183 public Builder includeUrl() {
184 this.includeUrl = true;
188 public Builder nodesOnly(Boolean nodesOnly) {
189 this.nodesOnly = nodesOnly;
193 public Builder startTs(String startTs) {
194 this.startTs = Long.parseLong(startTs);
198 public Builder endTs(String endTs) {
199 this.endTs = Long.parseLong(endTs);
203 public boolean isNodesOnly() {
204 return this.nodesOnly;
207 public Builder depth(Integer depth) {
212 public int getDepth() {
216 public boolean isIncludeUrl() {
217 return this.includeUrl;
220 public Builder modelDriven() {
221 this.modelDriven = true;
225 public boolean getModelDriven() {
226 return this.modelDriven;
229 public long getStartTs() {
233 public long getEndTs() {
237 public HistoryFormat build(Format format) {
239 if (Format.state.equals(format)) {
240 return new StateFormat(this);
242 return new LifecycleFormat(this);