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 final DBSerializer serializer;
56 protected final Loader loader;
57 protected final UrlBuilder urlBuilder;
58 protected final int depth;
59 protected final boolean nodesOnly;
61 protected TreeFormat(Builder builder) {
62 this.urlBuilder = builder.getUrlBuilder();
63 this.loader = builder.getLoader();
64 this.serializer = builder.getSerializer();
65 this.depth = builder.getDepth();
66 this.nodesOnly = builder.isNodesOnly();
70 public int parallelThreshold() {
74 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
76 protected final Loader loader;
77 protected final DBSerializer serializer;
78 protected final UrlBuilder urlBuilder;
79 protected boolean includeUrl = false;
80 protected boolean nodesOnly = false;
81 protected int depth = 1;
82 protected boolean modelDriven = false;
84 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
86 this.serializer = serializer;
87 this.urlBuilder = urlBuilder;
90 protected Loader getLoader() {
94 protected DBSerializer getSerializer() {
95 return this.serializer;
98 protected UrlBuilder getUrlBuilder() {
99 return this.urlBuilder;
102 public Builder includeUrl() {
103 this.includeUrl = true;
107 public Builder nodesOnly(Boolean nodesOnly) {
108 this.nodesOnly = nodesOnly;
112 public boolean isNodesOnly() {
113 return this.nodesOnly;
116 public Builder depth(Integer depth) {
121 public int getDepth() {
125 public boolean isIncludeUrl() {
126 return this.includeUrl;
129 public Builder modelDriven() {
130 this.modelDriven = true;
134 public boolean getModelDriven() {
135 return this.modelDriven;
138 public TreeFormat build() {
139 return new TreeFormat(this);
143 public JsonArray process(List<Object> queryResults, Map<String, List<String>> properties) {
144 JsonArray body = new JsonArray();
145 for (Object o : queryResults) {
147 return this.formatObjectToJsonArray(o, properties).orElseGet(() -> {
148 TREE_FORMAT_LOGGER.warn("Empty Optional returned by 'formatObjectToJsonArray'");
151 } catch (AAIFormatVertexException e) {
153 .warn("Failed to format vertex, returning a partial list " + LogFormatTools.getStackTop(e));
154 } catch (AAIFormatQueryResultFormatNotSupported e) {
155 TREE_FORMAT_LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
161 public Optional<JsonArray> formatObjectToJsonArray(Object input, Map<String, List<String>> properties)
162 throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
163 JsonArray json = new JsonArray();
165 return Optional.of(json);
166 if (input instanceof Tree) {
167 return this.getJsonArrayFromTree((Tree<Object>) input);
169 throw new AAIFormatQueryResultFormatNotSupported();
173 protected Optional<JsonArray> getJsonArrayFromTree(Tree<Object> tree) throws AAIFormatVertexException {
174 if (tree.isEmpty()) {
175 return Optional.of(new JsonArray());
179 JsonArray jsonArray = new JsonArray();
180 JsonObject jsonObject = new JsonObject();
181 for (Map.Entry<Object, Tree<Object>> entry : tree.entrySet()) {
182 Object o = entry.getKey();
185 if (o instanceof BulkSet) {
186 BulkSet<Vertex> bs = (BulkSet<Vertex>) o;
187 for (Vertex o1 : bs) {
188 Optional<JsonObject> obj = this.getJsonFromVertex(o1);
189 if (obj.isPresent()) {
190 jsonObject = obj.get();
191 for (Map.Entry<String, JsonElement> mapEntry : jsonObject.entrySet()) {
192 JsonElement jsonRootElementContents = mapEntry.getValue(); // getting everyObject inside
193 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
194 JsonObject relatedJsonNode = (JsonObject) jsonRootElementContents;
195 addRelatedNodesToJsonObject(jsonRootElementContents.getAsJsonObject(),
196 getRelatedNodes(relatedJsonNode));
199 jsonArray.add(jsonObject);
204 else if (o instanceof Vertex) {
205 Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) o);
206 if (obj.isPresent()) {
207 jsonObject = obj.get();
208 for (Map.Entry<String, JsonElement> mapEntry : jsonObject.entrySet()) {
209 JsonElement jsonRootElementContents = mapEntry.getValue();
210 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
211 addRelatedNodesToJsonObject(jsonRootElementContents.getAsJsonObject(),
212 getRelatedNodes(entry.getValue()));
215 jsonArray.add(jsonObject);
219 return Optional.of(jsonArray);
222 protected Optional<JsonArray> getRelatedNodes(JsonObject jsonObj) throws AAIFormatVertexException {
223 JsonArray relatedNodes = new JsonArray();
224 for (Map.Entry<String, JsonElement> mapEntry : jsonObj.entrySet()) {
225 String s = mapEntry.getKey();
226 JsonElement jsonRootElementContents = jsonObj.get(s);
227 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
228 JsonObject relatedJsonNode = jsonRootElementContents.getAsJsonObject();
229 addRelatedNodesToJsonObject(relatedJsonNode, this.getRelatedNodes(relatedJsonNode));
230 relatedNodes.add(relatedJsonNode);
233 return Optional.of(relatedNodes);
236 protected Optional<JsonArray> getRelatedNodes(Tree<Object> tree) throws AAIFormatVertexException {
237 JsonArray relatedNodes = new JsonArray();
238 for (Map.Entry<Object, Tree<Object>> entry : tree.entrySet()) {
239 Object o = entry.getKey();
241 if (o instanceof Vertex) {
242 processVertex(relatedNodes, entry, (Vertex) o);
245 return Optional.of(relatedNodes);
248 private void processVertex(JsonArray relatedNodes, Entry<Object, Tree<Object>> entry, Vertex o)
249 throws AAIFormatVertexException {
250 Optional<JsonObject> obj = this.getJsonFromVertex(o);
251 if (obj.isPresent()) {
252 JsonObject jsonObj = obj.get();
253 for (Entry<String, JsonElement> mapEntry : jsonObj.entrySet()) {
254 JsonElement jsonRootElementContents = mapEntry.getValue();
255 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
256 JsonObject jsonObject = addRelatedNodesToJsonObject(jsonRootElementContents.getAsJsonObject(),
257 getRelatedNodes(entry.getValue()));
258 relatedNodes.add(jsonObject);
264 private static JsonObject addRelatedNodesToJsonObject(JsonObject jsonObject, Optional<JsonArray> relatedNodesOpt) {
265 relatedNodesOpt.ifPresent(relatedNodes -> {
266 if (relatedNodes.size() > 0) {
267 jsonObject.add("related-nodes", relatedNodes);
276 * Returns an Optional<JsonObject> to convert the contents from the given Vertex object into a JsonObject.
277 * The fields returned are to record the time stamp of the creation/modification of the object, the user responsible
279 * the change, and the last http method performed on the object.
283 * @throws AAIFormatVertexException
286 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
288 JsonObject json = new JsonObject();
290 Optional<JsonObject> jsonObject = this.vertexToJsonObject(v);
291 if (jsonObject.isPresent()) {
292 json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jsonObject.get());
294 return Optional.empty();
296 return Optional.of(json);
299 protected Optional<JsonObject> vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
301 final Introspector obj =
302 getLoader().introspectorFromName(v.<String>property(AAIProperties.NODE_TYPE).orElse(null));
304 final List<Vertex> wrapper = new ArrayList<>();
309 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false");
310 } catch (AAIException | UnsupportedEncodingException e) {
311 throw new AAIFormatVertexException(
312 "Failed to format vertex - error while serializing: " + e.getMessage(), e);
315 final String json = obj.marshal(false);
317 return Optional.of(JsonParser.parseString(json).getAsJsonObject());
318 } catch (AAIUnknownObjectException e) {
319 return Optional.empty();
323 private Loader getLoader() {
327 private DBSerializer getSerializer() {
332 protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties)
333 throws AAIFormatVertexException {
334 return Optional.empty();