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;
29 import java.util.Map.Entry;
30 import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet;
31 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.onap.aai.db.props.AAIProperties;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Introspector;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
38 import org.onap.aai.logging.LogFormatTools;
39 import org.onap.aai.serialization.db.DBSerializer;
40 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
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.NodesOnly;
44 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
46 import java.io.UnsupportedEncodingException;
49 public class TreeFormat extends MultiFormatMapper {
50 private static final EELFLogger TREE_FORMAT_LOGGER = EELFManager.getInstance().getLogger(TreeFormat.class);
51 protected JsonParser parser = new JsonParser();
52 protected final DBSerializer serializer;
53 protected final Loader loader;
54 protected final UrlBuilder urlBuilder;
55 protected final int depth;
56 protected final boolean nodesOnly;
58 protected TreeFormat(Builder builder) {
59 this.urlBuilder = builder.getUrlBuilder();
60 this.loader = builder.getLoader();
61 this.serializer = builder.getSerializer();
62 this.depth = builder.getDepth();
63 this.nodesOnly = builder.isNodesOnly();
67 public int parallelThreshold() {
71 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
73 protected final Loader loader;
74 protected final DBSerializer serializer;
75 protected final UrlBuilder urlBuilder;
76 protected boolean includeUrl = false;
77 protected boolean nodesOnly = false;
78 protected int depth = 1;
79 protected boolean modelDriven = false;
81 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
83 this.serializer = serializer;
84 this.urlBuilder = urlBuilder;
87 protected Loader getLoader() {
91 protected DBSerializer getSerializer() {
92 return this.serializer;
95 protected UrlBuilder getUrlBuilder() {
96 return this.urlBuilder;
99 public Builder includeUrl() {
100 this.includeUrl = true;
104 public Builder nodesOnly(Boolean nodesOnly) {
105 this.nodesOnly = nodesOnly;
109 public boolean isNodesOnly() {
110 return this.nodesOnly;
113 public Builder depth(Integer depth) {
118 public int getDepth() {
122 public boolean isIncludeUrl() {
123 return this.includeUrl;
126 public Builder modelDriven() {
127 this.modelDriven = true;
131 public boolean getModelDriven() {
132 return this.modelDriven;
135 public TreeFormat build() {
136 return new TreeFormat(this);
140 public JsonArray process(List<Object> queryResults, Map<String, List<String>> properties) {
141 JsonArray body = new JsonArray();
142 for (Object o : queryResults) {
144 return this.formatObjectToJsonArray(o, properties).orElseGet( () -> {
145 TREE_FORMAT_LOGGER.warn("Empty Optional returned by 'formatObjectToJsonArray'");
148 } catch (AAIFormatVertexException e) {
149 TREE_FORMAT_LOGGER.warn("Failed to format vertex, returning a partial list " + LogFormatTools.getStackTop(e));
150 } catch (AAIFormatQueryResultFormatNotSupported e) {
151 TREE_FORMAT_LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
157 public Optional<JsonArray> formatObjectToJsonArray(Object input, Map<String, List<String>> properties)
158 throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
159 JsonArray json = new JsonArray();
161 return Optional.of(json);
162 if (input instanceof Tree) {
163 return this.getJsonArrayFromTree((Tree<Object>) input);
165 throw new AAIFormatQueryResultFormatNotSupported();
169 protected Optional<JsonArray> getJsonArrayFromTree(Tree<Object> tree) throws AAIFormatVertexException {
170 if (tree.isEmpty()) {
171 return Optional.of(new JsonArray());
175 JsonArray jsonArray = new JsonArray();
176 JsonObject jsonObject = new JsonObject();
177 for (Map.Entry<Object, Tree<Object>> entry : tree.entrySet()) {
178 Object o = entry.getKey();
181 if (o instanceof BulkSet) {
182 BulkSet bs = (BulkSet) o;
183 for (Object o1 : bs) {
184 Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) o1);
185 if (obj.isPresent()) {
186 jsonObject = obj.get();
187 for (Map.Entry<String, JsonElement> mapEntry : jsonObject.entrySet()) {
188 JsonElement jsonRootElementContents = mapEntry.getValue(); // getting everyObject inside
189 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
190 JsonObject relatedJsonNode = (JsonObject) jsonRootElementContents;
191 addRelatedNodesToJsonObject(
192 jsonRootElementContents.getAsJsonObject(),
193 getRelatedNodes(relatedJsonNode)
197 jsonArray.add(jsonObject);
202 else if (o instanceof Vertex) {
203 Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) o);
204 if (obj.isPresent()) {
205 jsonObject = obj.get();
206 for (Map.Entry<String, JsonElement> mapEntry : jsonObject.entrySet()) {
207 JsonElement jsonRootElementContents = mapEntry.getValue();
208 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
209 addRelatedNodesToJsonObject(
210 jsonRootElementContents.getAsJsonObject(),
211 getRelatedNodes(entry.getValue()));
214 jsonArray.add(jsonObject);
218 return Optional.of(jsonArray);
221 protected Optional<JsonArray> getRelatedNodes(JsonObject jsonObj) throws AAIFormatVertexException {
222 JsonArray relatedNodes = new JsonArray();
223 for (Map.Entry<String, JsonElement> mapEntry : jsonObj.entrySet()) {
224 String s = mapEntry.getKey();
225 JsonElement jsonRootElementContents = jsonObj.get(s);
226 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
227 JsonObject relatedJsonNode = jsonRootElementContents.getAsJsonObject();
228 addRelatedNodesToJsonObject(
230 this.getRelatedNodes(relatedJsonNode)
232 relatedNodes.add(relatedJsonNode);
235 return Optional.of(relatedNodes);
238 protected Optional<JsonArray> getRelatedNodes(Tree<Object> tree) throws AAIFormatVertexException {
239 JsonArray relatedNodes = new JsonArray();
240 for (Map.Entry<Object, Tree<Object>> entry : tree.entrySet()) {
241 Object o = entry.getKey();
243 if (o instanceof Vertex) {
244 processVertex(relatedNodes, entry, (Vertex) o);
247 return Optional.of(relatedNodes);
250 private void processVertex(JsonArray relatedNodes, Entry<Object, Tree<Object>> entry, Vertex o)
251 throws AAIFormatVertexException {
252 Optional<JsonObject> obj = this.getJsonFromVertex(o);
253 if (obj.isPresent()) {
254 JsonObject jsonObj = obj.get();
255 for (Entry<String, JsonElement> mapEntry : jsonObj.entrySet()) {
256 JsonElement jsonRootElementContents = mapEntry.getValue();
257 if (jsonRootElementContents != null && jsonRootElementContents.isJsonObject()) {
258 JsonObject jsonObject = addRelatedNodesToJsonObject(
259 jsonRootElementContents.getAsJsonObject(),
260 getRelatedNodes(entry.getValue()));
261 relatedNodes.add(jsonObject);
268 private static JsonObject addRelatedNodesToJsonObject(JsonObject jsonObject, Optional<JsonArray> relatedNodesOpt) {
269 relatedNodesOpt.ifPresent( relatedNodes -> {
270 if (relatedNodes.size() > 0) {
271 jsonObject.add("related-nodes", relatedNodes);
280 * Returns an Optional<JsonObject> to convert the contents from the given Vertex object into a JsonObject.
281 * The fields returned are to record the time stamp of the creation/modification of the object, the user responsible
283 * the change, and the last http method performed on the object.
287 * @throws AAIFormatVertexException
290 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
292 JsonObject json = new JsonObject();
294 Optional<JsonObject> jsonObject = this.vertexToJsonObject(v);
295 if (jsonObject.isPresent()) {
296 json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jsonObject.get());
298 return Optional.empty();
300 return Optional.of(json);
303 protected Optional<JsonObject> vertexToJsonObject(Vertex v) throws AAIFormatVertexException {
305 final Introspector obj =
306 getLoader().introspectorFromName(v.<String>property(AAIProperties.NODE_TYPE).orElse(null));
308 final List<Vertex> wrapper = new ArrayList<>();
313 getSerializer().dbToObject(wrapper, obj, this.depth, this.nodesOnly, "false");
314 } catch (AAIException | UnsupportedEncodingException e) {
315 throw new AAIFormatVertexException(
316 "Failed to format vertex - error while serializing: " + e.getMessage(), e);
319 final String json = obj.marshal(false);
321 return Optional.of(getParser().parse(json).getAsJsonObject());
322 } catch (AAIUnknownObjectException e) {
323 return Optional.empty();
327 private Loader getLoader() {
331 private DBSerializer getSerializer() {
335 private JsonParser getParser() {
341 protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException {
342 return Optional.empty();