070e22f4849fc02a1bac1ff9d2c8f37948c03a44
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / serialization / queryformats / RawFormat.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.aai.serialization.queryformats;
22
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.apache.tinkerpop.gremlin.structure.Direction;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
29 import org.openecomp.aai.db.props.AAIProperties;
30 import org.openecomp.aai.introspection.Loader;
31 import org.openecomp.aai.serialization.db.DBSerializer;
32 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
33 import org.openecomp.aai.serialization.queryformats.params.Depth;
34 import org.openecomp.aai.serialization.queryformats.params.NodesOnly;
35 import org.openecomp.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 implements FormatMapper {
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 JsonObject formatObject(Object input) throws AAIFormatVertexException {
60                 Vertex v = (Vertex)input;
61                 JsonObject json = new JsonObject();
62                 json.addProperty("id", v.id().toString());
63                 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
64                 json.addProperty("url", this.urlBuilder.pathed(v));
65                 json.add("properties", this.createPropertiesObject(v));
66                 if (!nodesOnly) {
67                         json.add("related-to", this.createRelationshipObject(v));
68                 }
69                 return json;
70         }
71
72         @Override
73         public int parallelThreshold() {
74                 return 100;
75         }
76         
77         
78         public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException {
79                 JsonObject json = new JsonObject();
80                 Iterator<VertexProperty<Object>> iter = v.properties();
81
82                 while (iter.hasNext()) {
83                         VertexProperty<Object> prop = iter.next();
84                         if (prop.value() instanceof String) {
85                                 json.addProperty(prop.key(), (String)prop.value());
86                         } else if (prop.value() instanceof Boolean) {
87                                 json.addProperty(prop.key(), (Boolean)prop.value());
88                         } else if (prop.value() instanceof Number) {
89                                 json.addProperty(prop.key(), (Number)prop.value());
90                         } else if (prop.value() instanceof List) {
91                                 Gson gson = new Gson();
92                                 String list = gson.toJson(prop.value());
93
94                                 json.addProperty(prop.key(), list);
95                         } else {
96                                 //throw exception?
97                                 return null;
98                         }
99                 }
100
101                 return json;
102         }
103         protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
104                 JsonArray jarray = new JsonArray();
105                 Iterator<Vertex> iter = v.vertices(Direction.BOTH);
106
107                 while (iter.hasNext()) {
108                         Vertex related = iter.next();
109
110                         JsonObject json = new JsonObject();
111                         json.addProperty("id", related.id().toString());
112                         json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
113                         json.addProperty("url", this.urlBuilder.pathed(related));
114                         jarray.add(json);
115                 }
116
117                 return jarray;
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 }