Apply multiplicity Rule upon Edge creation
[aai/gizmo.git] / src / main / java / org / onap / crud / parser / CrudResponseBuilder.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.crud.parser;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import org.onap.crud.entity.Edge;
28 import org.onap.crud.entity.Vertex;
29 import org.onap.crud.exception.CrudException;
30 import org.onap.schema.EdgeRulesLoader;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33 import com.google.gson.JsonArray;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonObject;
36
37 public class CrudResponseBuilder {
38
39   private static final Gson gson = new GsonBuilder().create();
40
41   public static final String SOURCE = "source";
42   public static final String TARGET = "target";
43   public static final String URL_BASE = "services/inventory/";
44
45   public static String buildUpsertBulkResponse(HashMap<String, Vertex> vertices, HashMap<String, Edge> edges,
46       String version, BulkPayload incomingPayload) throws CrudException {
47
48     for (JsonElement e : incomingPayload.getObjects()) {
49       List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
50           e.getAsJsonObject().entrySet());
51
52       Map.Entry<String, JsonElement> item = entries.get(1);
53
54       Vertex responseVertex = vertices.get(item.getKey());
55       if (responseVertex != null) {
56         JsonObject v = gson.fromJson(buildUpsertVertexResponse(responseVertex, version), JsonObject.class);
57         item.setValue(v);
58       } else {
59         item.setValue(gson.fromJson("{}", JsonObject.class));
60       }
61
62     }
63     for (JsonElement e : incomingPayload.getRelationships()) {
64       List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
65           e.getAsJsonObject().entrySet());
66
67       Map.Entry<String, JsonElement> item = entries.get(1);
68
69       Edge responseEdge = edges.get(item.getKey());
70       if (responseEdge != null) {
71         JsonObject v = gson.fromJson(buildUpsertEdgeResponse(responseEdge, version), JsonObject.class);
72         item.setValue(v);
73       } else {
74         item.setValue(gson.fromJson("{}", JsonObject.class));
75       }
76
77     }
78     return incomingPayload.toJson();
79   }
80
81   public static String buildUpsertVertexResponse(Vertex vertex, String version) throws CrudException {
82     VertexPayload payload = new VertexPayload();
83     payload.setId(vertex.getId().get());
84     payload.setType(vertex.getType());
85     payload.setUrl(URL_BASE + version + "/" + vertex.getType() + "/" + vertex.getId().get());
86     JsonObject props = new JsonObject();
87     for (String key : vertex.getProperties().keySet()) {
88       addJsonProperperty(props, key, vertex.getProperties().get(key));
89     }
90     payload.setProperties(props);
91     return payload.toJson();
92   }
93
94   public static String buildUpsertEdgeResponse(Edge edge, String version) throws CrudException {
95     return buildGetEdgeResponse(edge, version);
96   }
97
98   public static String buildGetVertexResponse(Vertex vertex, List<Edge> edges, String version) throws CrudException {
99     VertexPayload vertexPayload = new VertexPayload();
100     vertexPayload.setId(vertex.getId().get());
101     vertexPayload.setType(vertex.getType());
102     vertexPayload.setUrl(URL_BASE + version + "/" + vertex.getType() + "/" + vertex.getId().get());
103     JsonObject props = new JsonObject();
104     for (String key : vertex.getProperties().keySet()) {
105       addJsonProperperty(props, key, vertex.getProperties().get(key));
106     }
107     vertexPayload.setProperties(props);
108     List<EdgePayload> inEdges = new ArrayList<EdgePayload>();
109     List<EdgePayload> outEdges = new ArrayList<EdgePayload>();
110     for (Edge e : edges) {
111       if (e.getTarget().getId().get().equals(vertex.getId().get())) {
112         EdgePayload inEdge = new EdgePayload();
113         inEdge.setId(e.getId().get());
114         inEdge.setType(e.getType());
115         inEdge.setUrl(URL_BASE + "relationships/" + EdgeRulesLoader.getLatestSchemaVersion() + "/"
116             + e.getType() + "/" + e.getId().get());
117         inEdge.setSource(URL_BASE + version + "/" + e.getSource().getType() + "/" + e.getSource().getId().get());
118
119         inEdges.add(inEdge);
120       } else if (e.getSource().getId().get().equals(vertex.getId().get())) {
121         EdgePayload outEdge = new EdgePayload();
122         outEdge.setId(e.getId().get());
123         outEdge.setType(e.getType());
124         outEdge.setUrl(URL_BASE + "relationships/" + EdgeRulesLoader.getLatestSchemaVersion() + "/"
125             + e.getType() + "/" + e.getId().get());
126         outEdge.setTarget(URL_BASE + version + "/" + e.getTarget().getType() + "/" + e.getTarget().getId().get());
127         outEdges.add(outEdge);
128       }
129     }
130
131     vertexPayload.setIn(inEdges);
132     vertexPayload.setOut(outEdges);
133
134     return vertexPayload.toJson();
135   }
136
137   public static String buildGetVerticesResponse(List<Vertex> items, String version) throws CrudException {
138
139     JsonArray arry = new JsonArray();
140     for (Vertex v : items) {
141       JsonObject item = new JsonObject();
142       item.addProperty("id", v.getId().get());
143       item.addProperty("type", v.getType());
144       item.addProperty("url", "services/inventory/" + version + "/" + v.getType() + "/" + v.getId().get());
145       if (!v.getProperties().isEmpty()) {
146         JsonObject propertiesObject = new JsonObject();
147         for (String key : v.getProperties().keySet()) {
148           propertiesObject.addProperty(key, v.getProperties().get(key).toString());
149         }
150         item.add("properties", propertiesObject);
151       }
152
153       arry.add(item);
154     }
155
156     return gson.toJson(arry);
157   }
158
159   public static String buildGetEdgeResponse(Edge edge, String version) throws CrudException {
160
161     EdgePayload payload = new EdgePayload();
162     payload.setId(edge.getId().get());
163     payload.setType(edge.getType());
164     payload.setUrl(URL_BASE + "relationships/" + version + "/" + edge.getType() + "/" + edge.getId().get());
165     payload.setSource(URL_BASE + version + "/" + edge.getSource().getType() + "/" + edge.getSource().getId().get());
166     payload.setTarget(URL_BASE + version + "/" + edge.getTarget().getType() + "/" + edge.getTarget().getId().get());
167
168     JsonObject props = new JsonObject();
169     for (String key : edge.getProperties().keySet()) {
170       addJsonProperperty(props, key, edge.getProperties().get(key));
171     }
172     payload.setProperties(props);
173     return payload.toJson();
174   }
175
176   public static String buildGetEdgesResponse(List<Edge> items, String version) throws CrudException {
177
178     JsonArray arry = new JsonArray();
179     for (Edge e : items) {
180       JsonObject item = new JsonObject();
181       item.addProperty("id", e.getId().get());
182       item.addProperty("type", e.getType());
183       item.addProperty("url", URL_BASE + "relationships/" + version + "/" + e.getType() + "/" + e.getId().get());
184       item.addProperty(SOURCE,
185           "services/inventory/" + version + "/" + e.getSource().getType() + "/" + e.getSource().getId().get());
186       item.addProperty(TARGET,
187           "services/inventory/" + version + "/" + e.getTarget().getType() + "/" + e.getTarget().getId().get());
188       arry.add(item);
189     }
190
191     return gson.toJson(arry);
192   }
193
194   private static void addJsonProperperty(JsonObject jsonObj, String key, Object value) {
195     if (value instanceof Integer) {
196       jsonObj.addProperty(key, (Integer) value);
197     } else if (value instanceof Boolean) {
198       jsonObj.addProperty(key, (Boolean) value);
199     } else if (value instanceof Double) {
200       jsonObj.addProperty(key, (Double) value);
201     } else if (value instanceof String) {
202       jsonObj.addProperty(key, (String) value);
203     } else {
204       jsonObj.addProperty(key, value.toString());
205     }
206   }
207
208 }