Update to use Schema Service
[aai/gizmo.git] / src / main / java / org / onap / crud / service / AbstractGraphDataService.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.service;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonElement;
26 import com.google.gson.reflect.TypeToken;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import javax.ws.rs.core.EntityTag;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.Response.Status;
35 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
36 import org.apache.commons.lang3.tuple.ImmutablePair;
37 import org.onap.aai.restclient.client.OperationResult;
38 import org.onap.crud.dao.GraphDao;
39 import org.onap.crud.dao.champ.ChampEdgeSerializer;
40 import org.onap.crud.dao.champ.ChampVertexSerializer;
41 import org.onap.crud.entity.Edge;
42 import org.onap.crud.entity.Vertex;
43 import org.onap.crud.exception.CrudException;
44 import org.onap.crud.parser.BulkPayload;
45 import org.onap.crud.parser.CrudResponseBuilder;
46 import org.onap.crud.parser.EdgePayload;
47 import org.onap.crud.parser.VertexPayload;
48 import org.onap.crud.parser.util.EdgePayloadUtil;
49 import org.onap.crud.util.CrudServiceUtil;
50 import org.onap.schema.validation.OxmModelValidator;
51 import org.onap.schema.validation.RelationshipSchemaValidator;
52
53 public abstract class AbstractGraphDataService {
54   protected GraphDao daoForGet;
55   protected GraphDao dao;
56
57   public AbstractGraphDataService() throws CrudException {
58   }
59
60   public ImmutablePair<EntityTag, String> getEdge(String version, String id, String type, Map<String, String> queryParams) throws CrudException {
61     RelationshipSchemaValidator.validateType(version, type);
62     OperationResult operationResult = daoForGet.getEdge(id, type, queryParams);
63     EntityTag entityTag = CrudServiceUtil.getETagFromHeader(operationResult.getHeaders());
64     Edge edge = Edge.fromJson(operationResult.getResult());
65     return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, edge), version));
66   }
67
68   public ImmutablePair<EntityTag, String> getEdges(String version, String type, Map<String, String> filter) throws CrudException {
69      Gson champGson = new GsonBuilder()
70               .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
71               .registerTypeAdapter(Vertex.class, new ChampVertexSerializer())
72               .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create();
73     RelationshipSchemaValidator.validateType(version, type);
74     OperationResult operationResult = daoForGet.getEdges(type, RelationshipSchemaValidator.resolveCollectionfilter(version, type, filter));
75     List<Edge> items = champGson.fromJson(operationResult.getResult(), new TypeToken<List<Edge>>() {
76     }.getType());
77     EntityTag entityTag = CrudServiceUtil.getETagFromHeader(operationResult.getHeaders());
78     return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetEdgesResponse(items, version));
79   }
80
81   public ImmutablePair<EntityTag, String> getVertex(String version, String id, String type, Map<String, String> queryParams) throws CrudException {
82     type = OxmModelValidator.resolveCollectionType(version, type);
83     OperationResult vertexOpResult = daoForGet.getVertex(id, type, version, queryParams);
84     Vertex vertex = Vertex.fromJson(vertexOpResult.getResult(), version);
85     List<Edge> edges = daoForGet.getVertexEdges(id, queryParams, null);
86     EntityTag entityTag = CrudServiceUtil.getETagFromHeader(vertexOpResult.getHeaders());
87     return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator.validateOutgoingPayload(version, vertex), edges,
88         version));
89   }
90
91   public ImmutablePair<EntityTag, String> getVertices(String version, String type, Map<String, String> filter, Set<String> properties) throws CrudException {
92     type = OxmModelValidator.resolveCollectionType(version, type);
93     OperationResult operationResult = daoForGet.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter), properties, version);
94     List<Vertex> vertices = Vertex.collectionFromJson(operationResult.getResult(), version);
95     EntityTag entityTag = CrudServiceUtil.getETagFromHeader(operationResult.getHeaders());
96     return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetVerticesResponse(vertices, version));
97   }
98
99   public String addBulk(String version, BulkPayload payload, HttpHeaders headers) throws CrudException {
100     HashMap<String, Vertex> vertices = new HashMap<>();
101     HashMap<String, Edge> edges = new HashMap<>();
102
103     String txId = dao.openTransaction();
104
105     try {
106       // Step 1. Handle edge deletes (must happen before vertex deletes)
107       for (JsonElement v : payload.getRelationships()) {
108         List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
109             v.getAsJsonObject().entrySet());
110
111         if (entries.size() != 2) {
112           throw new CrudException("", Status.BAD_REQUEST);
113         }
114         Map.Entry<String, JsonElement> opr = entries.get(0);
115         Map.Entry<String, JsonElement> item = entries.get(1);
116         EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
117
118         if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
119           deleteBulkEdge(edgePayload.getId(), version, txId);
120         }
121       }
122
123       // Step 2: Handle vertex deletes
124       for (JsonElement v : payload.getObjects()) {
125         List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
126             v.getAsJsonObject().entrySet());
127
128         if (entries.size() != 2) {
129           throw new CrudException("", Status.BAD_REQUEST);
130         }
131
132         Map.Entry<String, JsonElement> opr = entries.get(0);
133         Map.Entry<String, JsonElement> item = entries.get(1);
134         VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
135
136         if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
137           String type = OxmModelValidator.resolveCollectionType(version, vertexPayload.getType());
138           deleteBulkVertex(vertexPayload.getId(), version, type, txId);
139         }
140       }
141
142       // Step 3: Handle vertex add/modify (must happen before edge adds)
143       for (JsonElement v : payload.getObjects()) {
144         List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
145             v.getAsJsonObject().entrySet());
146
147         if (entries.size() != 2) {
148           throw new CrudException("", Status.BAD_REQUEST);
149         }
150         Map.Entry<String, JsonElement> opr = entries.get(0);
151         Map.Entry<String, JsonElement> item = entries.get(1);
152         VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
153
154         // Add vertex
155         if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
156           vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
157               headers, true));
158           Vertex validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(),
159               vertexPayload.getProperties());
160           Vertex persistedVertex = addBulkVertex(validatedVertex, version, txId);
161           Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
162           vertices.put(item.getKey(), outgoingVertex);
163         }
164
165         // Update vertex
166         else if (opr.getValue().getAsString().equalsIgnoreCase("modify")) {
167           vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
168               headers, false));
169           Vertex validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version,
170               vertexPayload.getType(), vertexPayload.getProperties());
171           Vertex persistedVertex = updateBulkVertex(validatedVertex, vertexPayload.getId(), version, txId);
172           Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
173           vertices.put(item.getKey(), outgoingVertex);
174         }
175
176         // Patch vertex
177         else if (opr.getValue().getAsString().equalsIgnoreCase("patch")) {
178           if ( (vertexPayload.getId() == null) || (vertexPayload.getType() == null) ) {
179             throw new CrudException("id and type must be specified for patch request", Status.BAD_REQUEST);
180           }
181
182           vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
183               headers, false));
184
185           OperationResult existingVertexOpResult = dao.getVertex(vertexPayload.getId(), OxmModelValidator.resolveCollectionType(version, vertexPayload.getType()), version, new HashMap<String, String>());
186           Vertex existingVertex = Vertex.fromJson(existingVertexOpResult.getResult(), version);
187           Vertex validatedVertex = OxmModelValidator.validateIncomingPatchPayload(vertexPayload.getId(),
188               version, vertexPayload.getType(), vertexPayload.getProperties(), existingVertex);
189           Vertex persistedVertex = updateBulkVertex(validatedVertex, vertexPayload.getId(), version, txId);
190           Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
191           vertices.put(item.getKey(), outgoingVertex);
192         }
193       }
194
195       // Step 4: Handle edge add/modify
196       for (JsonElement v : payload.getRelationships()) {
197         List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
198             v.getAsJsonObject().entrySet());
199
200         if (entries.size() != 2) {
201           throw new CrudException("", Status.BAD_REQUEST);
202         }
203         Map.Entry<String, JsonElement> opr = entries.get(0);
204         Map.Entry<String, JsonElement> item = entries.get(1);
205         EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
206
207         // Add/Update edge
208         if (opr.getValue().getAsString().equalsIgnoreCase("add")
209             || opr.getValue().getAsString().equalsIgnoreCase("modify")
210             || opr.getValue().getAsString().equalsIgnoreCase("patch")) {
211           Edge validatedEdge;
212           Edge persistedEdge;
213           if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
214             // Fix the source/destination
215             if (edgePayload.getSource().startsWith("$")) {
216               Vertex source = vertices.get(edgePayload.getSource().substring(1));
217               if (source == null) {
218                 throw new CrudException("Not able to find vertex: " + edgePayload.getSource().substring(1),
219                     Status.INTERNAL_SERVER_ERROR);
220               }
221               edgePayload
222                   .setSource("services/inventory/" + version + "/" + source.getType() + "/" + source.getId().get());
223             }
224             if (edgePayload.getTarget().startsWith("$")) {
225               Vertex target = vertices.get(edgePayload.getTarget().substring(1));
226               if (target == null) {
227                 throw new CrudException("Not able to find vertex: " + edgePayload.getTarget().substring(1),
228                     Status.INTERNAL_SERVER_ERROR);
229               }
230               edgePayload
231                   .setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get());
232             }
233
234             // If the type isn't set, resolve it based on on the sourece and target vertex types
235             if (edgePayload.getType() == null || edgePayload.getType().isEmpty()) {
236               edgePayload.setType(CrudServiceUtil.determineEdgeType(edgePayload, version));
237             }
238
239             List<Edge> sourceVertexEdges =
240                     EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getSource()), edgePayload.getType(),
241                                  dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getSource()), null, txId));
242
243             List<Edge> targetVertexEdges =
244                      EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getTarget()), edgePayload.getType(),
245                                  dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getTarget()), null, txId));
246
247             validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(), edgePayload, sourceVertexEdges,
248                     targetVertexEdges);
249             persistedEdge = addBulkEdge(validatedEdge, version, txId);
250           } else if (opr.getValue().getAsString().equalsIgnoreCase("modify")) {
251             Edge edge = dao.getEdge(edgePayload.getId(), txId);
252
253             // If the type isn't set, resolve it based on on the sourece and target vertex types
254             if (edgePayload.getType() == null || edgePayload.getType().isEmpty()) {
255               edgePayload.setType(edge.getType());
256             }
257
258             // load source and target vertex relationships for validation
259             List<Edge> sourceVertexEdges =
260                    EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getSource()), edgePayload.getType(),
261                                 dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getSource()), null, txId));
262
263             List<Edge> targetVertexEdges =
264                     EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getTarget()), edgePayload.getType(),
265                                 dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getTarget()), null, txId));
266
267
268             validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload, edgePayload.getType(), sourceVertexEdges, targetVertexEdges);
269             persistedEdge = updateBulkEdge(validatedEdge, version, txId);
270           } else {
271             if (edgePayload.getId() == null) {
272               throw new CrudException("id must be specified for patch request", Status.BAD_REQUEST);
273             }
274             Edge existingEdge = dao.getEdge(edgePayload.getId(), txId);
275
276             // If the type isn't set, resolve it based on on the sourece and target vertex types
277             if (edgePayload.getType() == null || edgePayload.getType().isEmpty()) {
278               edgePayload.setType(existingEdge.getType());
279             }
280
281             Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(existingEdge, version, edgePayload);
282             persistedEdge = updateBulkEdge(patchedEdge, version, txId);
283           }
284
285
286           Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge);
287           edges.put(item.getKey(), outgoingEdge);
288         }
289       }
290
291       // commit transaction
292       dao.commitTransaction(txId);
293     } catch (CrudException ex) {
294       dao.rollbackTransaction(txId);
295       throw ex;
296     } catch (Exception ex) {
297       dao.rollbackTransaction(txId);
298       throw ex;
299     } finally {
300       if (dao.transactionExists(txId)) {
301         dao.rollbackTransaction(txId);
302       }
303     }
304
305     return CrudResponseBuilder.buildUpsertBulkResponse(vertices, edges, version, payload);
306   }
307
308
309   public abstract ImmutablePair<EntityTag, String> addVertex(String version, String type, VertexPayload payload)
310             throws CrudException;
311   public abstract ImmutablePair<EntityTag, String> updateVertex(String version, String id, String type,
312             VertexPayload payload) throws CrudException;
313   public abstract ImmutablePair<EntityTag, String> patchVertex(String version, String id, String type,
314             VertexPayload payload) throws CrudException;
315   public abstract String deleteVertex(String version, String id, String type) throws CrudException;
316   public abstract ImmutablePair<EntityTag, String> addEdge(String version, String type, EdgePayload payload)
317             throws CrudException;
318   public abstract String deleteEdge(String version, String id, String type) throws CrudException;
319   public abstract ImmutablePair<EntityTag, String> updateEdge(String version, String id, String type,
320             EdgePayload payload) throws CrudException;
321   public abstract ImmutablePair<EntityTag, String> patchEdge(String version, String id, String type,
322             EdgePayload payload) throws CrudException;
323
324   protected abstract Vertex addBulkVertex(Vertex vertex, String version, String dbTransId) throws CrudException;
325   protected abstract Vertex updateBulkVertex(Vertex vertex, String id, String version, String dbTransId) throws CrudException;
326   protected abstract void deleteBulkVertex(String id, String version, String type, String dbTransId) throws CrudException;
327
328   protected abstract Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException;
329   protected abstract Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException;
330   protected abstract void deleteBulkEdge(String id, String version, String dbTransId) throws CrudException;
331
332 }