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