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