2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.service;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
31 import javax.ws.rs.core.HttpHeaders;
32 import javax.ws.rs.core.Response.Status;
34 import org.onap.crud.dao.GraphDao;
35 import org.onap.crud.entity.Edge;
36 import org.onap.crud.entity.Vertex;
37 import org.onap.crud.exception.CrudException;
38 import org.onap.crud.parser.CrudResponseBuilder;
39 import org.onap.crud.util.CrudServiceUtil;
40 import org.onap.schema.OxmModelValidator;
41 import org.onap.schema.RelationshipSchemaValidator;
43 import com.google.gson.JsonElement;
45 public abstract class AbstractGraphDataService {
46 protected GraphDao dao;
48 public AbstractGraphDataService(GraphDao dao) throws CrudException {
51 CrudServiceUtil.loadModels();
54 public String getEdge(String version, String id, String type) throws CrudException {
55 RelationshipSchemaValidator.validateType(version, type);
56 Edge edge = dao.getEdge(id, type);
58 return CrudResponseBuilder.buildGetEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, edge), version);
61 public String getEdges(String version, String type, Map<String, String> filter) throws CrudException {
62 RelationshipSchemaValidator.validateType(version, type);
63 List<Edge> items = dao.getEdges(type, RelationshipSchemaValidator.resolveCollectionfilter(version, type, filter));
64 return CrudResponseBuilder.buildGetEdgesResponse(items, version);
67 public String getVertex(String version, String id, String type) throws CrudException {
68 type = OxmModelValidator.resolveCollectionType(version, type);
69 Vertex vertex = dao.getVertex(id, type);
70 List<Edge> edges = dao.getVertexEdges(id);
71 return CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator.validateOutgoingPayload(version, vertex), edges,
75 public String getVertices(String version, String type, Map<String, String> filter) throws CrudException {
76 type = OxmModelValidator.resolveCollectionType(version, type);
77 List<Vertex> items = dao.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter));
78 return CrudResponseBuilder.buildGetVerticesResponse(items, version);
81 public String addBulk(String version, BulkPayload payload, HttpHeaders headers) throws CrudException {
82 HashMap<String, Vertex> vertices = new HashMap<String, Vertex>();
83 HashMap<String, Edge> edges = new HashMap<String, Edge>();
85 String txId = dao.openTransaction();
88 // Step 1. Handle edge deletes (must happen before vertex deletes)
89 for (JsonElement v : payload.getRelationships()) {
90 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
91 v.getAsJsonObject().entrySet());
93 if (entries.size() != 2) {
94 throw new CrudException("", Status.BAD_REQUEST);
96 Map.Entry<String, JsonElement> opr = entries.get(0);
97 Map.Entry<String, JsonElement> item = entries.get(1);
98 EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
100 if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
101 RelationshipSchemaValidator.validateType(version, edgePayload.getType());
102 deleteBulkEdge(edgePayload.getId(), version, edgePayload.getType(), txId);
106 // Step 2: Handle vertex deletes
107 for (JsonElement v : payload.getObjects()) {
108 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
109 v.getAsJsonObject().entrySet());
111 if (entries.size() != 2) {
112 throw new CrudException("", Status.BAD_REQUEST);
115 Map.Entry<String, JsonElement> opr = entries.get(0);
116 Map.Entry<String, JsonElement> item = entries.get(1);
117 VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
119 if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
120 String type = OxmModelValidator.resolveCollectionType(version, vertexPayload.getType());
121 deleteBulkVertex(vertexPayload.getId(), version, type, txId);
125 // Step 3: Handle vertex add/modify (must happen before edge adds)
126 for (JsonElement v : payload.getObjects()) {
127 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
128 v.getAsJsonObject().entrySet());
130 if (entries.size() != 2) {
131 throw new CrudException("", Status.BAD_REQUEST);
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());
138 if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
139 vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
141 Vertex validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(),
142 vertexPayload.getProperties());
143 Vertex persistedVertex = addBulkVertex(validatedVertex, version, txId);
144 Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
145 vertices.put(item.getKey(), outgoingVertex);
149 else if (opr.getValue().getAsString().equalsIgnoreCase("modify")) {
150 vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
152 Vertex validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version,
153 vertexPayload.getType(), vertexPayload.getProperties());
154 Vertex persistedVertex = updateBulkVertex(validatedVertex, vertexPayload.getId(), version, txId);
155 Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
156 vertices.put(item.getKey(), outgoingVertex);
160 else if (opr.getValue().getAsString().equalsIgnoreCase("patch")) {
161 if ( (vertexPayload.getId() == null) || (vertexPayload.getType() == null) ) {
162 throw new CrudException("id and type must be specified for patch request", Status.BAD_REQUEST);
165 vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(),
168 Vertex existingVertex = dao.getVertex(vertexPayload.getId(), OxmModelValidator.resolveCollectionType(version, vertexPayload.getType()));
169 Vertex validatedVertex = OxmModelValidator.validateIncomingPatchPayload(vertexPayload.getId(),
170 version, vertexPayload.getType(), vertexPayload.getProperties(), existingVertex);
171 Vertex persistedVertex = updateBulkVertex(validatedVertex, vertexPayload.getId(), version, txId);
172 Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
173 vertices.put(item.getKey(), outgoingVertex);
177 // Step 4: Handle edge add/modify
178 for (JsonElement v : payload.getRelationships()) {
179 List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
180 v.getAsJsonObject().entrySet());
182 if (entries.size() != 2) {
183 throw new CrudException("", Status.BAD_REQUEST);
185 Map.Entry<String, JsonElement> opr = entries.get(0);
186 Map.Entry<String, JsonElement> item = entries.get(1);
187 EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
190 if (opr.getValue().getAsString().equalsIgnoreCase("add")
191 || opr.getValue().getAsString().equalsIgnoreCase("modify")
192 || opr.getValue().getAsString().equalsIgnoreCase("patch")) {
195 if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
196 // Fix the source/destination
197 if (edgePayload.getSource().startsWith("$")) {
198 Vertex source = vertices.get(edgePayload.getSource().substring(1));
199 if (source == null) {
200 throw new CrudException("Not able to find vertex: " + edgePayload.getSource().substring(1),
201 Status.INTERNAL_SERVER_ERROR);
204 .setSource("services/inventory/" + version + "/" + source.getType() + "/" + source.getId().get());
206 if (edgePayload.getTarget().startsWith("$")) {
207 Vertex target = vertices.get(edgePayload.getTarget().substring(1));
208 if (target == null) {
209 throw new CrudException("Not able to find vertex: " + edgePayload.getTarget().substring(1),
210 Status.INTERNAL_SERVER_ERROR);
213 .setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get());
215 validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(),
217 persistedEdge = addBulkEdge(validatedEdge, version, txId);
218 } else if (opr.getValue().getAsString().equalsIgnoreCase("modify")) {
219 Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
220 validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload);
221 persistedEdge = updateBulkEdge(validatedEdge, version, txId);
223 if ( (edgePayload.getId() == null) || (edgePayload.getType() == null) ) {
224 throw new CrudException("id and type must be specified for patch request", Status.BAD_REQUEST);
226 Edge existingEdge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
227 Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(existingEdge, version, edgePayload);
228 persistedEdge = updateBulkEdge(patchedEdge, version, txId);
232 Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge);
233 edges.put(item.getKey(), outgoingEdge);
237 // commit transaction
238 dao.commitTransaction(txId);
239 } catch (CrudException ex) {
240 dao.rollbackTransaction(txId);
242 } catch (Exception ex) {
243 dao.rollbackTransaction(txId);
246 if (dao.transactionExists(txId)) {
247 dao.rollbackTransaction(txId);
251 return CrudResponseBuilder.buildUpsertBulkResponse(vertices, edges, version, payload);
255 public abstract String addVertex(String version, String type, VertexPayload payload) throws CrudException;
256 public abstract String updateVertex(String version, String id, String type, VertexPayload payload) throws CrudException;
257 public abstract String patchVertex(String version, String id, String type, VertexPayload payload) throws CrudException;
258 public abstract String deleteVertex(String version, String id, String type) throws CrudException;
259 public abstract String addEdge(String version, String type, EdgePayload payload) throws CrudException;
260 public abstract String deleteEdge(String version, String id, String type) throws CrudException;
261 public abstract String updateEdge(String version, String id, String type, EdgePayload payload) throws CrudException;
262 public abstract String patchEdge(String version, String id, String type, EdgePayload payload) throws CrudException;
264 protected abstract Vertex addBulkVertex(Vertex vertex, String version, String dbTransId) throws CrudException;
265 protected abstract Vertex updateBulkVertex(Vertex vertex, String id, String version, String dbTransId) throws CrudException;
266 protected abstract void deleteBulkVertex(String id, String version, String type, String dbTransId) throws CrudException;
268 protected abstract Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException;
269 protected abstract Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException;
270 protected abstract void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException;