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.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParser;
30 import java.io.UnsupportedEncodingException;
31 import java.util.ArrayList;
32 import java.util.List;
34 import java.util.Map.Entry;
35 import java.util.Optional;
37 import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet;
38 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
39 import org.apache.tinkerpop.gremlin.structure.Vertex;
40 import org.onap.aai.db.props.AAIProperties;
41 import org.onap.aai.exceptions.AAIException;
42 import org.onap.aai.introspection.Introspector;
43 import org.onap.aai.introspection.Loader;
44 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
45 import org.onap.aai.logging.LogFormatTools;
46 import org.onap.aai.serialization.db.DBSerializer;
47 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
48 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
49 import org.onap.aai.serialization.queryformats.params.Depth;
50 import org.onap.aai.serialization.queryformats.params.NodesOnly;
51 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
53 public class TreeFormat extends MultiFormatMapper {
54 private static final EELFLogger TREE_FORMAT_LOGGER = EELFManager.getInstance().getLogger(TreeFormat.class);
55 protected JsonParser parser = new JsonParser();
56 protected final DBSerializer serializer;
57 protected final Loader loader;
58 protected final UrlBuilder urlBuilder;
59 protected final int depth;
60 protected final boolean nodesOnly;
62 protected TreeFormat(Builder builder) {
63 this.urlBuilder = builder.getUrlBuilder();
64 this.loader = builder.getLoader();
65 this.serializer = builder.getSerializer();
66 this.depth = builder.getDepth();
67 this.nodesOnly = builder.isNodesOnly();
71 public int parallelThreshold() {
75 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
77 protected final Loader loader;
78 protected final DBSerializer serializer;
79 protected final UrlBuilder urlBuilder;
80 protected boolean includeUrl = false;
81 protected boolean nodesOnly = false;
82 protected int depth = 1;
83 protected boolean modelDriven = false;
85 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
87 this.serializer = serializer;
88 this.urlBuilder = urlBuilder;
91 protected Loader getLoader() {
95 protected DBSerializer getSerializer() {
96 return this.serializer;
99 protected UrlBuilder getUrlBuilder() {
100 return this.urlBuilder;
103 public Builder includeUrl() {
104 this.includeUrl = true;
108 public Builder nodesOnly(Boolean nodesOnly) {
109 this.nodesOnly = nodesOnly;
113 public boolean isNodesOnly() {
114 return this.nodesOnly;
117 public Builder depth(Integer depth) {
122 public int getDepth() {
126 public boolean isIncludeUrl() {
127 return this.includeUrl;
130 public Builder modelDriven() {
131 this.modelDriven = true;
135 public boolean getModelDriven() {
136 return this.modelDriven;
139 public TreeFormat build() {
140 return new TreeFormat(this);
144 public JsonArray process(List<Object> queryResults, Map<String, List<String>> properties) {
145 JsonArray body = new JsonArray();
146 for (Object o : queryResults) {
148 return this.formatObjectToJsonArray(o, properties).orElseGet(() -> {
149 TREE_FORMAT_LOGGER.warn("Empty Optional returned by 'formatObjectToJsonArray'");
152 } catch (AAIFormatVertexException e) {
154 .warn("Failed to format vertex, returning a partial list " + LogFormatTools.getStackTop(e));
155 } catch (AAIFormatQueryResultFormatNotSupported e) {
156 TREE_FORMAT_LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
162 public Optional<JsonArray> formatObjectToJsonArray(Object input, Map<String, List<String>> properties)
163 throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
164 JsonArray json = new JsonArray();
166 return Optional.of(json);
167 if (input instanceof Tree) {
168 return this.getJsonArrayFromTree((Tree<Object>) input);
170 throw new AAIFormatQueryResultFormatNotSupported();
174 protected Optional<JsonArray> getJsonArrayFromTree(Tree<Object> tree) throws AAIFormatVertexException {
175 if (tree.isEmpty()) {
176 return Optional.of(new JsonArray());
180 JsonArray jsonArray = new JsonArray();
181 JsonObject jsonObject = new JsonObject();
182 for (Map.Entry<Object, Tree<Object>> entry : tree.entrySet()) {
183 Object o = entry.getKey();
186 if (o instanceof BulkSet) {
187 BulkSet<Vertex> bs = (BulkSet<Vertex>) o;
188 for (Vertex o1 : bs) {
189 Optional<JsonObject> obj = this.getJsonFromVertex(o1);
190 if (obj.isPresent()) {
191 jsonObject = obj.get();
192 for (Map.Entry<String, JsonElement> mapEntry : jsonObject.entrySet()) {
193 JsonElement jsonRootElementContents = mapEntry.getValue(); // getting everyObject inside
194 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
195 JsonObject relatedJsonNode = (JsonObject) jsonRootElementContents;
196 addRelatedNodesToJsonObject(jsonRootElementContents.getAsJsonObject(),
197 getRelatedNodes(relatedJsonNode));
200 jsonArray.add(jsonObject);
205 else if (o instanceof Vertex) {
206 Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) o);
207 if (obj.isPresent()) {
208 jsonObject = obj.get();
209 for (Map.Entry<String, JsonElement> mapEntry : jsonObject.entrySet()) {
210 JsonElement jsonRootElementContents = mapEntry.getValue();
211 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
212 addRelatedNodesToJsonObject(jsonRootElementContents.getAsJsonObject(),
213 getRelatedNodes(entry.getValue()));
216 jsonArray.add(jsonObject);
220 return Optional.of(jsonArray);
223 protected Optional<JsonArray> getRelatedNodes(JsonObject jsonObj) throws AAIFormatVertexException {
224 JsonArray relatedNodes = new JsonArray();
225 for (Map.Entry<String, JsonElement> mapEntry : jsonObj.entrySet()) {
226 String s = mapEntry.getKey();
227 JsonElement jsonRootElementContents = jsonObj.get(s);
228 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
229 JsonObject relatedJsonNode = jsonRootElementContents.getAsJsonObject();
230 addRelatedNodesToJsonObject(relatedJsonNode, this.getRelatedNodes(relatedJsonNode));
231 relatedNodes.add(relatedJsonNode);
234 return Optional.of(relatedNodes);
237 protected Optional<JsonArray> getRelatedNodes(Tree<Object> tree) throws AAIFormatVertexException {
238 JsonArray relatedNodes = new JsonArray();
239 for (Map.Entry<Object, Tree<Object>> entry : tree.entrySet()) {
240 Object o = entry.getKey();
242 if (o instanceof Vertex) {
243 processVertex(relatedNodes, entry, (Vertex) o);
246 return Optional.of(relatedNodes);
249 private void processVertex(JsonArray relatedNodes, Entry<Object, Tree<Object>> entry, Vertex o)
250 throws AAIFormatVertexException {
251 Optional<JsonObject> obj = this.getJsonFromVertex(o);
252 if (obj.isPresent()) {
253 JsonObject jsonObj = obj.get();
254 for (Entry<String, JsonElement> mapEntry : jsonObj.entrySet()) {
255 JsonElement jsonRootElementContents = mapEntry.getValue();
256 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
257 JsonObject jsonObject = addRelatedNodesToJsonObject(jsonRootElementContents.getAsJsonObject(),
258 getRelatedNodes(entry.getValue()));
259 relatedNodes.add(jsonObject);
265 private static JsonObject addRelatedNodesToJsonObject(JsonObject jsonObject, Optional<JsonArray> relatedNodesOpt) {
266 relatedNodesOpt.ifPresent(relatedNodes -> {
267 if (relatedNodes.size() > 0) {
268 jsonObject.add("related-nodes", relatedNodes);
277 * Returns an Optional<JsonObject> to convert the contents from the given Vertex object into a JsonObject.
278 * The fields returned are to record the time stamp of the creation/modification of the object, the user responsible
280 * the change, and the last http method performed on the object.
284 * @throws AAIFormatVertexException
287 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
289 JsonObject json = new JsonObject();
291 Optional<JsonObject> jsonObject = this.vertexToJsonObject(v);
292 if (jsonObject.isPresent()) {
293 json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jsonObject.get());
295 return Optional.empty();
297 return Optional.of(json);
300 protected Optional<JsonObject> vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
302 final Introspector obj =
303 getLoader().introspectorFromName(v.<String>property(AAIProperties.NODE_TYPE).orElse(null));
305 final List<Vertex> wrapper = new ArrayList<>();
310 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false");
311 } catch (AAIException | UnsupportedEncodingException e) {
312 throw new AAIFormatVertexException(
313 "Failed to format vertex - error while serializing: " + e.getMessage(), e);
316 final String json = obj.marshal(false);
318 return Optional.of(getParser().parse(json).getAsJsonObject());
319 } catch (AAIUnknownObjectException e) {
320 return Optional.empty();
324 private Loader getLoader() {
328 private DBSerializer getSerializer() {
332 private JsonParser getParser() {
337 protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties)
338 throws AAIFormatVertexException {
339 return Optional.empty();