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