e4b5fc177e3108a78cf62aa12617e164bc62f09f
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / MultiFormatMapper.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *    http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  *\r
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  */\r
22 package org.onap.aai.serialization.queryformats;\r
23 \r
24 import java.util.Iterator;\r
25 import java.util.List;\r
26 \r
27 import org.apache.tinkerpop.gremlin.process.traversal.Path;\r
28 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;\r
29 import org.apache.tinkerpop.gremlin.structure.Vertex;\r
30 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;\r
31 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;\r
32 \r
33 import com.google.gson.JsonArray;\r
34 import com.google.gson.JsonObject;\r
35 \r
36 public abstract class MultiFormatMapper implements FormatMapper {\r
37 \r
38         @Override\r
39         public JsonObject formatObject(Object input) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {\r
40                 if (input instanceof Vertex) {\r
41                         return this.getJsonFromVertex((Vertex) input);\r
42                 } else if (input instanceof Tree) {\r
43                         return this.getJsonFomTree((Tree<?>) input);\r
44                 } else if (input instanceof Path) {\r
45                         return this.getJsonFromPath((Path) input);\r
46                 } else {\r
47                         throw new AAIFormatQueryResultFormatNotSupported();\r
48                 }\r
49         }\r
50 \r
51         protected abstract JsonObject getJsonFromVertex(Vertex input) throws AAIFormatVertexException;\r
52 \r
53         protected JsonObject getJsonFromPath(Path input) throws AAIFormatVertexException {\r
54                 List<Object> path = input.objects();\r
55 \r
56                 JsonObject jo = new JsonObject();\r
57                 JsonArray ja = new JsonArray();\r
58                 \r
59                 for (Object o : path) {\r
60                         if (o instanceof Vertex) {\r
61                                 ja.add(this.getJsonFromVertex((Vertex)o));\r
62                         }\r
63                 }\r
64                 \r
65                 jo.add("path", ja);\r
66                 return jo;\r
67         }\r
68 \r
69         protected JsonObject getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {\r
70                 \r
71                 if (tree.isEmpty()) {\r
72                         return new JsonObject();\r
73                 }\r
74                 \r
75                 JsonObject t = new JsonObject();\r
76                 JsonArray ja = this.getNodesArray(tree);\r
77                 if (ja.size() > 0) {\r
78                         t.add("nodes", ja);\r
79                 }\r
80                 \r
81                 return t;\r
82         }\r
83         \r
84         private JsonArray getNodesArray (Tree<?> tree) throws AAIFormatVertexException {\r
85                 \r
86                 JsonArray nodes = new JsonArray();\r
87                 Iterator<?> it = tree.keySet().iterator();\r
88 \r
89                 while (it.hasNext()) {\r
90                         Object o = it.next();\r
91                         JsonObject me = new JsonObject();\r
92                         if (o instanceof Vertex) {\r
93                                 me = this.getJsonFromVertex((Vertex) o);\r
94                         }\r
95                         JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));\r
96                         if (ja.size() > 0) {\r
97                                 me.add("nodes", ja);\r
98                         }\r
99                         nodes.add(me);\r
100                 }\r
101                 return nodes;\r
102         }\r
103         \r
104         \r
105         @Override\r
106         public int parallelThreshold() {\r
107                 return 100;\r
108         }\r
109 \r
110 }\r