21b666f22f859d7c86af5cc4ea886bd5aa177204
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / MultiFormatMapper.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.serialization.queryformats;
22
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonObject;
25
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Optional;
29
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;
35
36 public abstract class MultiFormatMapper implements FormatMapper {
37
38     @Override
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);
47         } else {
48             throw new AAIFormatQueryResultFormatNotSupported();
49         }
50     }
51
52     protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
53
54     protected Optional<JsonObject> getJsonFromPath(Path input) throws AAIFormatVertexException {
55         List<Object> path = input.objects();
56
57         JsonObject jo = new JsonObject();
58         JsonArray ja = new JsonArray();
59
60         for (Object o : path) {
61             if (o instanceof Vertex) {
62                 ja.add(this.getJsonFromVertex((Vertex) o).get());
63             }
64         }
65
66         jo.add("path", ja);
67         return Optional.of(jo);
68     }
69
70     protected Optional<JsonObject> getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {
71
72         if (tree.isEmpty()) {
73             return Optional.of(new JsonObject());
74         }
75
76         JsonObject t = new JsonObject();
77         JsonArray ja = this.getNodesArray(tree);
78         if (ja.size() > 0) {
79             t.add("nodes", ja);
80         }
81
82         return Optional.of(t);
83     }
84
85     private JsonArray getNodesArray(Tree<?> tree) throws AAIFormatVertexException {
86
87         JsonArray nodes = new JsonArray();
88         Iterator<?> it = tree.keySet().iterator();
89
90         while (it.hasNext()) {
91             Object o = it.next();
92             JsonObject me = new JsonObject();
93             if (o instanceof Vertex) {
94                 me = this.getJsonFromVertex((Vertex) o).get();
95             }
96             JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));
97             if (ja.size() > 0) {
98                 me.add("nodes", ja);
99             }
100             nodes.add(me);
101         }
102         return nodes;
103     }
104
105     @Override
106     public int parallelThreshold() {
107         return 100;
108     }
109
110 }