Enhancements for the aai-common library
[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 import org.apache.tinkerpop.gremlin.process.traversal.Path;
26 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
29 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
30
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Optional;
34
35 public abstract class MultiFormatMapper implements FormatMapper {
36
37     protected boolean isTree = false;
38
39     @Override
40     public Optional<JsonObject> formatObject(Object input)
41             throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
42         if (input instanceof Vertex) {
43             return this.getJsonFromVertex((Vertex) input);
44         } else if (input instanceof Tree) {
45             if (isTree) {
46                 return this.getRelatedNodesFromTree((Tree<?>) input);
47             } else {
48                 return this.getJsonFomTree((Tree<?>) input);
49             }
50         } else if (input instanceof Path) {
51             return this.getJsonFromPath((Path) input);
52         } else {
53             throw new AAIFormatQueryResultFormatNotSupported();
54         }
55     }
56
57     @Override
58     public Optional<JsonObject> formatObject(Object input, Map<String, List<String>> properties)
59         throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
60         if (input instanceof Vertex) {
61             return this.getJsonFromVertex((Vertex) input, properties);
62         } else if (input instanceof Tree) {
63             if (isTree) {
64                 return this.getRelatedNodesFromTree((Tree<?>) input);
65             } else {
66                 return this.getJsonFomTree((Tree<?>) input);
67             }
68         } else if (input instanceof Path) {
69             return this.getJsonFromPath((Path) input);
70         } else {
71             throw new AAIFormatQueryResultFormatNotSupported();
72         }
73     }
74
75     protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
76     protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException;
77
78     protected Optional<JsonObject> getJsonFromPath(Path input) throws AAIFormatVertexException {
79         List<Object> path = input.objects();
80
81         JsonObject jo = new JsonObject();
82         JsonArray ja = new JsonArray();
83
84         for (Object o : path) {
85             if (o instanceof Vertex) {
86                 Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) o);
87                 obj.ifPresent(ja::add);
88             }
89         }
90
91         jo.add("path", ja);
92         return Optional.of(jo);
93     }
94
95     protected Optional<JsonObject> getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {
96
97         if (tree.isEmpty()) {
98             return Optional.of(new JsonObject());
99         }
100
101         JsonObject t = new JsonObject();
102         JsonArray ja = this.getNodesArray(tree, "nodes");
103         if (ja.size() > 0) {
104             t.add("nodes", ja);
105         }
106
107         return Optional.of(t);
108     }
109
110     protected Optional<JsonObject> getRelatedNodesFromTree(Tree<?> tree) throws AAIFormatVertexException {
111         if (tree.isEmpty()) {
112             return Optional.of(new JsonObject());
113         }
114
115         JsonObject t = new JsonObject();
116         JsonArray ja = this.getNodesArray(tree, "related-nodes");
117         if (ja.size() > 0) {
118             t.add("results", ja);
119             return Optional.of(t);
120         }
121
122         return Optional.empty();
123     }
124
125     protected JsonArray getNodesArray(Tree<?> tree, String nodeIdentifier) throws AAIFormatVertexException {
126
127         JsonArray nodes = new JsonArray();
128         for (Map.Entry<?, ? extends Tree<?>> entry : tree.entrySet()) {
129             JsonObject me = new JsonObject();
130             if (entry.getKey() instanceof Vertex) {
131                 Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) entry.getKey());
132                 if (obj.isPresent()) {
133                     me = obj.get();
134                 } else {
135                     continue;
136                 }
137             }
138             JsonArray ja = this.getNodesArray(entry.getValue(), nodeIdentifier);
139             if (ja.size() > 0) {
140                 me.add(nodeIdentifier, ja);
141             }
142             nodes.add(me);
143         }
144         return nodes;
145     }
146
147     @Override
148     public int parallelThreshold() {
149         return 100;
150     }
151
152 }