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