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;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Optional;
30 import org.apache.tinkerpop.gremlin.process.traversal.Path;
31 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
34 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
36 public abstract class MultiFormatMapper implements FormatMapper {
39 public Optional<JsonObject> formatObject(Object input)
40 throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
41 if (input instanceof Vertex) {
42 return this.getJsonFromVertex((Vertex) input);
43 } else if (input instanceof Tree) {
44 return this.getJsonFomTree((Tree<?>) input);
45 } else if (input instanceof Path) {
46 return this.getJsonFromPath((Path) input);
48 throw new AAIFormatQueryResultFormatNotSupported();
52 protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
54 protected Optional<JsonObject> getJsonFromPath(Path input) throws AAIFormatVertexException {
55 List<Object> path = input.objects();
57 JsonObject jo = new JsonObject();
58 JsonArray ja = new JsonArray();
60 for (Object o : path) {
61 if (o instanceof Vertex) {
62 ja.add(this.getJsonFromVertex((Vertex) o).get());
67 return Optional.of(jo);
70 protected Optional<JsonObject> getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {
73 return Optional.of(new JsonObject());
76 JsonObject t = new JsonObject();
77 JsonArray ja = this.getNodesArray(tree);
82 return Optional.of(t);
85 private JsonArray getNodesArray(Tree<?> tree) throws AAIFormatVertexException {
87 JsonArray nodes = new JsonArray();
88 Iterator<?> it = tree.keySet().iterator();
90 while (it.hasNext()) {
92 JsonObject me = new JsonObject();
93 if (o instanceof Vertex) {
94 me = this.getJsonFromVertex((Vertex) o).get();
96 JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));
106 public int parallelThreshold() {