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.JsonArray;
24 import com.google.gson.JsonObject;
25 import org.apache.tinkerpop.gremlin.process.traversal.Path;
26 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
29 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
31 import java.util.List;
33 import java.util.Optional;
35 public abstract class MultiFormatMapper implements FormatMapper {
37 protected boolean isTree = false;
40 public Optional<JsonObject> formatObject(Object input)
41 throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
42 if (input instanceof Vertex) {
43 return this.getJsonFromVertex((Vertex) input);
44 } else if (input instanceof Tree) {
46 return this.getRelatedNodesFromTree((Tree<?>) input);
48 return this.getJsonFomTree((Tree<?>) input);
50 } else if (input instanceof Path) {
51 return this.getJsonFromPath((Path) input);
53 throw new AAIFormatQueryResultFormatNotSupported();
58 public Optional<JsonObject> formatObject(Object input, Map<String, List<String>> properties)
59 throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
60 if (input instanceof Vertex) {
61 return this.getJsonFromVertex((Vertex) input, properties);
62 } else if (input instanceof Tree) {
64 return this.getRelatedNodesFromTree((Tree<?>) input);
66 return this.getJsonFomTree((Tree<?>) input);
68 } else if (input instanceof Path) {
69 return this.getJsonFromPath((Path) input);
71 throw new AAIFormatQueryResultFormatNotSupported();
75 protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
76 protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException;
78 protected Optional<JsonObject> getJsonFromPath(Path input) throws AAIFormatVertexException {
79 List<Object> path = input.objects();
81 JsonObject jo = new JsonObject();
82 JsonArray ja = new JsonArray();
84 for (Object o : path) {
85 if (o instanceof Vertex) {
86 Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) o);
87 obj.ifPresent(ja::add);
92 return Optional.of(jo);
95 protected Optional<JsonObject> getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {
98 return Optional.of(new JsonObject());
101 JsonObject t = new JsonObject();
102 JsonArray ja = this.getNodesArray(tree, "nodes");
107 return Optional.of(t);
110 protected Optional<JsonObject> getRelatedNodesFromTree(Tree<?> tree) throws AAIFormatVertexException {
111 if (tree.isEmpty()) {
112 return Optional.of(new JsonObject());
115 JsonObject t = new JsonObject();
116 JsonArray ja = this.getNodesArray(tree, "related-nodes");
118 t.add("results", ja);
119 return Optional.of(t);
122 return Optional.empty();
125 protected JsonArray getNodesArray(Tree<?> tree, String nodeIdentifier) throws AAIFormatVertexException {
127 JsonArray nodes = new JsonArray();
128 for (Map.Entry<?, ? extends Tree<?>> entry : tree.entrySet()) {
129 JsonObject me = new JsonObject();
130 if (entry.getKey() instanceof Vertex) {
131 Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) entry.getKey());
132 if (obj.isPresent()) {
138 JsonArray ja = this.getNodesArray(entry.getValue(), nodeIdentifier);
140 me.add(nodeIdentifier, ja);
148 public int parallelThreshold() {