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=========================================================
20 package org.onap.aai.serialization.queryformats;
22 import com.google.gson.JsonArray;
23 import com.google.gson.JsonObject;
24 import org.apache.tinkerpop.gremlin.process.traversal.Path;
25 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
28 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Optional;
34 public abstract class MultiFormatMapper implements FormatMapper {
37 public Optional<JsonObject> formatObject(Object input) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
38 if (input instanceof Vertex) {
39 return this.getJsonFromVertex((Vertex) input);
40 } else if (input instanceof Tree) {
41 return this.getJsonFomTree((Tree<?>) input);
42 } else if (input instanceof Path) {
43 return this.getJsonFromPath((Path) input);
45 throw new AAIFormatQueryResultFormatNotSupported();
49 protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
51 protected Optional<JsonObject> getJsonFromPath(Path input) throws AAIFormatVertexException {
52 List<Object> path = input.objects();
54 JsonObject jo = new JsonObject();
55 JsonArray ja = new JsonArray();
57 for (Object o : path) {
58 if (o instanceof Vertex) {
59 ja.add(this.getJsonFromVertex((Vertex)o).get());
64 return Optional.of(jo);
67 protected Optional<JsonObject> getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {
70 return Optional.of(new JsonObject());
73 JsonObject t = new JsonObject();
74 JsonArray ja = this.getNodesArray(tree);
79 return Optional.of(t);
82 private JsonArray getNodesArray (Tree<?> tree) throws AAIFormatVertexException {
84 JsonArray nodes = new JsonArray();
85 Iterator<?> it = tree.keySet().iterator();
87 while (it.hasNext()) {
89 JsonObject me = new JsonObject();
90 if (o instanceof Vertex) {
91 me = this.getJsonFromVertex((Vertex) o).get();
93 JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));
104 public int parallelThreshold() {