425c6edfb0fe849d93bc57e2bba4c3d9d7a9a39c
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / RawFormat.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.serialization.queryformats;
23
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.tinkerpop.gremlin.structure.Direction;
28 import org.apache.tinkerpop.gremlin.structure.Edge;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
31 import org.onap.aai.db.props.AAIProperties;
32 import org.onap.aai.introspection.Loader;
33 import org.onap.aai.serialization.db.DBSerializer;
34 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
35 import org.onap.aai.serialization.queryformats.params.Depth;
36 import org.onap.aai.serialization.queryformats.params.NodesOnly;
37 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
38
39 import com.google.gson.Gson;
40 import com.google.gson.JsonArray;
41 import com.google.gson.JsonObject;
42 import com.google.gson.JsonParser;
43
44
45 public class RawFormat extends MultiFormatMapper {
46         protected JsonParser parser = new JsonParser();
47         protected final DBSerializer serializer;
48         protected final Loader loader;
49         protected final UrlBuilder urlBuilder;
50         protected final int depth;
51         protected final boolean nodesOnly;
52         protected RawFormat(Builder builder) {
53                 this.urlBuilder = builder.getUrlBuilder();
54                 this.loader = builder.getLoader();
55                 this.serializer = builder.getSerializer();
56                 this.depth = builder.getDepth();
57                 this.nodesOnly = builder.isNodesOnly();
58         }
59         
60         @Override
61         public int parallelThreshold() {
62                 return 100;
63         }
64         
65         
66         public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
67                 JsonObject json = new JsonObject();
68                 Iterator<VertexProperty<Object>> iter = v.properties();
69
70                 while (iter.hasNext()) {
71                         VertexProperty<Object> prop = iter.next();
72                         if (prop.value() instanceof String) {
73                                 json.addProperty(prop.key(), (String)prop.value());
74                         } else if (prop.value() instanceof Boolean) {
75                                 json.addProperty(prop.key(), (Boolean)prop.value());
76                         } else if (prop.value() instanceof Number) {
77                                 json.addProperty(prop.key(), (Number)prop.value());
78                         } else if (prop.value() instanceof List) {
79                                 Gson gson = new Gson();
80                                 String list = gson.toJson(prop.value());
81
82                                 json.addProperty(prop.key(), list);
83                         } else {
84                                 //throw exception?
85                                 return null;
86                         }
87                 }
88
89                 return json;
90         }
91         
92         protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
93                 JsonArray jarray = new JsonArray();
94                 Iterator<Edge> inIter = v.edges(Direction.IN);
95                 Iterator<Edge> outIter = v.edges(Direction.OUT);
96
97                 while (inIter.hasNext()) {
98                         Edge e = inIter.next();
99                         jarray.add(this.getRelatedObject(e.label(), e.outVertex()));
100                 }
101                 
102                 while (outIter.hasNext()) {
103                         Edge e = outIter.next();
104                         jarray.add(this.getRelatedObject(e.label(), e.inVertex()));
105                 }
106
107                 return jarray;
108         }
109         
110         private JsonObject getRelatedObject(String label, Vertex related) throws AAIFormatVertexException {
111                 JsonObject json = new JsonObject();
112                 json.addProperty("id", related.id().toString());
113                 json.addProperty("relationship-label", label);
114                 json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
115                 json.addProperty("url", this.urlBuilder.pathed(related));
116                 
117                 return json;
118         }
119         
120         public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
121                 
122                 protected final Loader loader;
123                 protected final DBSerializer serializer;
124                 protected final UrlBuilder urlBuilder;
125                 protected boolean includeUrl = false;
126                 protected boolean nodesOnly = false;
127                 protected int depth = 1;
128                 protected boolean modelDriven = false;
129                 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
130                         this.loader = loader;
131                         this.serializer = serializer;
132                         this.urlBuilder = urlBuilder;
133                 }
134                 
135                 protected Loader getLoader() {
136                         return this.loader;
137                 }
138
139                 protected DBSerializer getSerializer() {
140                         return this.serializer;
141                 }
142
143                 protected UrlBuilder getUrlBuilder() {
144                         return this.urlBuilder;
145                 }
146                 
147                 public Builder includeUrl() {
148                         this.includeUrl = true;
149                         return this;
150                 }
151                 
152                 public Builder nodesOnly(Boolean nodesOnly) {
153                         this.nodesOnly = nodesOnly;
154                         return this;
155                 }
156                 public boolean isNodesOnly() {
157                         return this.nodesOnly;
158                 }
159                 
160                 public Builder depth(Integer depth) {
161                         this.depth = depth;
162                         return this;
163                 }
164                 
165                 public int getDepth() {
166                         return this.depth;
167                 }
168
169                 public boolean isIncludeUrl() {
170                         return this.includeUrl;
171                 }
172                 
173                 public Builder modelDriven() {
174                         this.modelDriven = true;
175                         return this;
176                 }
177                 
178                 public boolean getModelDriven() {
179                         return this.modelDriven;
180                 }
181                 public RawFormat build() {
182                         if (modelDriven) {
183                                 return new SimpleFormat(this);
184                         } else {
185                                 return new RawFormat(this);
186                         }
187                 }
188         }
189
190         @Override
191         protected JsonObject getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
192
193                 JsonObject json = new JsonObject();
194                 json.addProperty("id", v.id().toString());
195                 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
196                 json.addProperty("url", this.urlBuilder.pathed(v));
197                 json.add("properties", this.createPropertiesObject(v));
198                 if (!nodesOnly) {
199                         json.add("related-to", this.createRelationshipObject(v));
200                 }
201                 return json;
202         }
203 }