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;
27 import java.util.Iterator;
28 import java.util.List;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
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;
50 public abstract class HistoryFormat extends MultiFormatMapper {
52 private static final Logger LOGGER = LoggerFactory.getLogger(HistoryFormat.class);
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";
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;
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());
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();
89 public int parallelThreshold() {
93 protected JsonObject createMetaPropertiesObject(VertexProperty<Object> prop) {
94 JsonObject json = new JsonObject();
95 Iterator iter = prop.properties();
97 while (iter.hasNext()) {
98 Property<Object> metaProp = (Property) iter.next();
99 mapPropertyValues(json, metaProp.key(), metaProp.value());
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);
113 if (!(propertyValue instanceof List)) {
117 Gson gson = new Gson();
118 String list = gson.toJson(propertyValue);
119 json.addProperty(propertyKey, list);
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);
129 while (inIter.hasNext()) {
130 Edge e = inIter.next();
131 if (isValidEdge(e)) {
132 relatedToList.add(getRelatedObject(e, e.outVertex()));
136 while (outIter.hasNext()) {
137 Edge e = outIter.next();
138 if (isValidEdge(e)) {
139 relatedToList.add(getRelatedObject(e, e.inVertex()));
143 return relatedToList;
147 protected abstract boolean isValidEdge(Edge e);
149 protected abstract JsonObject getRelatedObject(Edge e, Vertex related) throws AAIFormatVertexException;
151 public static class Builder implements NodesOnly<Builder>, Depth<Builder>, StartTs<Builder>, EndTs<Builder> {
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;
163 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
164 this.loader = loader;
165 this.serializer = serializer;
166 this.urlBuilder = urlBuilder;
169 protected Loader getLoader() {
173 protected DBSerializer getSerializer() {
174 return this.serializer;
177 protected UrlBuilder getUrlBuilder() {
178 return this.urlBuilder;
181 public Builder includeUrl() {
182 this.includeUrl = true;
186 public Builder nodesOnly(Boolean nodesOnly) {
187 this.nodesOnly = nodesOnly;
191 public Builder startTs(String startTs) {
192 this.startTs = Long.parseLong(startTs);
196 public Builder endTs(String endTs) {
197 this.endTs = Long.parseLong(endTs);
201 public boolean isNodesOnly() {
202 return this.nodesOnly;
205 public Builder depth(Integer depth) {
210 public int getDepth() {
214 public boolean isIncludeUrl() {
215 return this.includeUrl;
218 public Builder modelDriven() {
219 this.modelDriven = true;
223 public boolean getModelDriven() {
224 return this.modelDriven;
227 public long getStartTs() {
231 public long getEndTs() {
235 public HistoryFormat build(Format format) {
237 if (Format.state.equals(format)) {
238 return new StateFormat(this);
240 return new LifecycleFormat(this);