3477a748a22efb1532837ce894ecfe12a2bf7502
[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 implements FormatMapper {
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 JsonObject formatObject(Object input) throws AAIFormatVertexException {
62                 Vertex v = (Vertex)input;
63                 JsonObject json = new JsonObject();
64                 json.addProperty("id", v.id().toString());
65                 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
66                 json.addProperty("url", this.urlBuilder.pathed(v));
67                 json.add("properties", this.createPropertiesObject(v));
68                 if (!nodesOnly) {
69                         json.add("related-to", this.createRelationshipObject(v));
70                 }
71                 return json;
72         }
73
74         @Override
75         public int parallelThreshold() {
76                 return 100;
77         }
78         
79         
80         public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
81                 JsonObject json = new JsonObject();
82                 Iterator<VertexProperty<Object>> iter = v.properties();
83
84                 while (iter.hasNext()) {
85                         VertexProperty<Object> prop = iter.next();
86                         if (prop.value() instanceof String) {
87                                 json.addProperty(prop.key(), (String)prop.value());
88                         } else if (prop.value() instanceof Boolean) {
89                                 json.addProperty(prop.key(), (Boolean)prop.value());
90                         } else if (prop.value() instanceof Number) {
91                                 json.addProperty(prop.key(), (Number)prop.value());
92                         } else if (prop.value() instanceof List) {
93                                 Gson gson = new Gson();
94                                 String list = gson.toJson(prop.value());
95
96                                 json.addProperty(prop.key(), list);
97                         } else {
98                                 //throw exception?
99                                 return null;
100                         }
101                 }
102
103                 return json;
104         }
105         
106         protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
107                 JsonArray jarray = new JsonArray();
108                 Iterator<Edge> inIter = v.edges(Direction.IN);
109                 Iterator<Edge> outIter = v.edges(Direction.OUT);
110
111                 while (inIter.hasNext()) {
112                         Edge e = inIter.next();
113                         jarray.add(this.getRelatedObject(e.label(), e.outVertex()));
114                 }
115                 
116                 while (outIter.hasNext()) {
117                         Edge e = outIter.next();
118                         jarray.add(this.getRelatedObject(e.label(), e.inVertex()));
119                 }
120
121                 return jarray;
122         }
123         
124         private JsonObject getRelatedObject(String label, Vertex related) throws AAIFormatVertexException {
125                 JsonObject json = new JsonObject();
126                 json.addProperty("id", related.id().toString());
127                 json.addProperty("relationship-label", label);
128                 json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
129                 json.addProperty("url", this.urlBuilder.pathed(related));
130                 
131                 return json;
132         }
133         
134         public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
135                 
136                 protected final Loader loader;
137                 protected final DBSerializer serializer;
138                 protected final UrlBuilder urlBuilder;
139                 protected boolean includeUrl = false;
140                 protected boolean nodesOnly = false;
141                 protected int depth = 1;
142                 protected boolean modelDriven = false;
143                 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
144                         this.loader = loader;
145                         this.serializer = serializer;
146                         this.urlBuilder = urlBuilder;
147                 }
148                 
149                 protected Loader getLoader() {
150                         return this.loader;
151                 }
152
153                 protected DBSerializer getSerializer() {
154                         return this.serializer;
155                 }
156
157                 protected UrlBuilder getUrlBuilder() {
158                         return this.urlBuilder;
159                 }
160                 
161                 public Builder includeUrl() {
162                         this.includeUrl = true;
163                         return this;
164                 }
165                 
166                 public Builder nodesOnly(Boolean nodesOnly) {
167                         this.nodesOnly = nodesOnly;
168                         return this;
169                 }
170                 public boolean isNodesOnly() {
171                         return this.nodesOnly;
172                 }
173                 
174                 public Builder depth(Integer depth) {
175                         this.depth = depth;
176                         return this;
177                 }
178                 
179                 public int getDepth() {
180                         return this.depth;
181                 }
182
183                 public boolean isIncludeUrl() {
184                         return this.includeUrl;
185                 }
186                 
187                 public Builder modelDriven() {
188                         this.modelDriven = true;
189                         return this;
190                 }
191                 
192                 public boolean getModelDriven() {
193                         return this.modelDriven;
194                 }
195                 public RawFormat build() {
196                         if (modelDriven) {
197                                 return new SimpleFormat(this);
198                         } else {
199                                 return new RawFormat(this);
200                         }
201                 }
202         }
203 }