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