Initial commit with all the necessary files
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / serialization / queryformats / SimpleFormat.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.Arrays;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.apache.tinkerpop.gremlin.structure.Direction;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
33
34 import org.openecomp.aai.db.props.AAIProperties;
35 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
36 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
37 import com.google.gson.Gson;
38 import com.google.gson.JsonArray;
39 import com.google.gson.JsonObject;
40
41 public class SimpleFormat implements FormatMapper {
42
43         private final UrlBuilder urlBuilder;
44         private final Set<String> blacklist;
45         private final Collection<String> props = Arrays.asList(AAIProperties.AAI_URI, AAIProperties.NODE_TYPE);
46         public SimpleFormat(UrlBuilder urlBuilder) {
47                 this.urlBuilder = urlBuilder;
48                 this.blacklist = new HashSet<>();
49                 blacklist.addAll(props);
50         }
51         
52         @Override
53         public JsonObject formatObject(Object input) throws AAIFormatVertexException {
54                 Vertex v = (Vertex)input;
55                 JsonObject json = new JsonObject();
56                 json.addProperty("id", v.id().toString());
57                 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
58                 json.addProperty("url", this.urlBuilder.pathed(v));
59                 json.add("properties", this.createPropertiesObject(v));
60                 json.add("related-to", this.createRelationshipObject(v));
61                 
62                 return json;
63         }
64
65         @Override
66         public int parallelThreshold() {
67                 return 100;
68         }
69         
70         private JsonObject createPropertiesObject(Vertex v) {
71                 JsonObject json = new JsonObject();
72                 Iterator<VertexProperty<Object>> iter = v.properties();
73                 
74                 while (iter.hasNext()) {
75                         VertexProperty<Object> prop = iter.next();
76                         if (!blacklist.contains(prop.key())) {
77                                 if (prop.value() instanceof String) {
78                                         json.addProperty(prop.key(), (String)prop.value());
79                                 } else if (prop.value() instanceof Boolean) {
80                                         json.addProperty(prop.key(), (Boolean)prop.value());
81                                 } else if (prop.value() instanceof Number) {
82                                         json.addProperty(prop.key(), (Number)prop.value());
83                                 } else if (prop.value() instanceof List) {
84                                         Gson gson = new Gson();
85                                         String list = gson.toJson(prop.value());
86                                         
87                                         json.addProperty(prop.key(), list);
88                                 } else {
89                                         //throw exception?
90                                         return null;
91                                 }
92                         }
93                 }
94                 
95                 return json;
96         }
97         
98         private JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException {
99                 JsonArray jarray = new JsonArray();
100                 Iterator<Vertex> iter = v.vertices(Direction.BOTH);
101                 
102                 while (iter.hasNext()) {
103                         Vertex related = iter.next();
104                         
105                         JsonObject json = new JsonObject();
106                         json.addProperty("id", related.id().toString());
107                         json.addProperty("node-type", related.<String>value(AAIProperties.NODE_TYPE));
108                         json.addProperty("url", this.urlBuilder.pathed(related));
109                         jarray.add(json);
110                 }
111                 
112                 return jarray;
113         }
114 }