944c14c4e8ef10d1b9a23b5c8f612f9d599d350b
[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 package org.onap.aai.serialization.queryformats;
21
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;
29
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Optional;
33
34 public abstract class MultiFormatMapper implements FormatMapper {
35
36         @Override
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);
44                 } else {
45                         throw new AAIFormatQueryResultFormatNotSupported();
46                 }
47         }
48
49         protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
50
51         protected Optional<JsonObject> getJsonFromPath(Path input) throws AAIFormatVertexException {
52                 List<Object> path = input.objects();
53
54                 JsonObject jo = new JsonObject();
55                 JsonArray ja = new JsonArray();
56                 
57                 for (Object o : path) {
58                         if (o instanceof Vertex) {
59                                 ja.add(this.getJsonFromVertex((Vertex)o).get());
60                         }
61                 }
62                 
63                 jo.add("path", ja);
64                 return Optional.of(jo);
65         }
66
67         protected Optional<JsonObject> getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {
68                 
69                 if (tree.isEmpty()) {
70                         return Optional.of(new JsonObject());
71                 }
72                 
73                 JsonObject t = new JsonObject();
74                 JsonArray ja = this.getNodesArray(tree);
75                 if (ja.size() > 0) {
76                         t.add("nodes", ja);
77                 }
78                 
79                 return Optional.of(t);
80         }
81         
82         private JsonArray getNodesArray (Tree<?> tree) throws AAIFormatVertexException {
83                 
84                 JsonArray nodes = new JsonArray();
85                 Iterator<?> it = tree.keySet().iterator();
86
87                 while (it.hasNext()) {
88                         Object o = it.next();
89                         JsonObject me = new JsonObject();
90                         if (o instanceof Vertex) {
91                                 me = this.getJsonFromVertex((Vertex) o).get();
92                         }
93                         JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));
94                         if (ja.size() > 0) {
95                                 me.add("nodes", ja);
96                         }
97                         nodes.add(me);
98                 }
99                 return nodes;
100         }
101         
102         
103         @Override
104         public int parallelThreshold() {
105                 return 100;
106         }
107
108 }