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;
27 import java.util.HashMap;
29 import org.onap.crud.dao.GraphDao;
30 import org.onap.crud.entity.Edge;
32 import org.onap.crud.entity.Vertex;
33 import org.onap.crud.exception.CrudException;
34 import org.onap.crud.parser.CrudResponseBuilder;
35 import org.onap.schema.OxmModelValidator;
36 import org.onap.schema.RelationshipSchemaValidator;
39 public class CrudGraphDataService extends AbstractGraphDataService {
42 public CrudGraphDataService(GraphDao dao) throws CrudException {
48 public CrudGraphDataService(GraphDao dao, GraphDao daoForGet) throws CrudException {
51 this.daoForGet = daoForGet;
54 public String addVertex(String version, String type, VertexPayload payload) throws CrudException {
55 Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, type, payload.getProperties());
56 return addVertex(version, vertex);
59 private String addVertex(String version, Vertex vertex) throws CrudException {
60 Vertex addedVertex = dao.addVertex(vertex.getType(), vertex.getProperties(), version);
61 return CrudResponseBuilder
62 .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, addedVertex), version);
65 public String addEdge(String version, String type, EdgePayload payload) throws CrudException {
66 Edge edge = RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload);
67 return addEdge(version, edge);
70 private String addEdge(String version, Edge edge) throws CrudException {
71 Edge addedEdge = dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(), edge.getProperties(), version);
72 return CrudResponseBuilder
73 .buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, addedEdge), version);
76 public String updateVertex(String version, String id, String type, VertexPayload payload) throws CrudException {
77 Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(id, version, type, payload.getProperties());
78 return updateVertex(version, vertex);
82 private String updateVertex(String version, Vertex vertex) throws CrudException {
83 Vertex updatedVertex = dao.updateVertex(vertex.getId().get(), vertex.getType(), vertex.getProperties(), version);
84 return CrudResponseBuilder
85 .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, updatedVertex), version);
88 public String patchVertex(String version, String id, String type, VertexPayload payload) throws CrudException {
89 Vertex existingVertex = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version, type), version, new HashMap<String, String>());
90 Vertex vertex = OxmModelValidator.validateIncomingPatchPayload(id, version, type, payload.getProperties(),
92 return updateVertex(version, vertex);
95 public String deleteVertex(String version, String id, String type) throws CrudException {
96 type = OxmModelValidator.resolveCollectionType(version, type);
97 dao.deleteVertex(id, type);
101 public String deleteEdge(String version, String id, String type) throws CrudException {
102 RelationshipSchemaValidator.validateType(version, type);
103 dao.deleteEdge(id, type);
107 public String updateEdge(String version, String id, String type, EdgePayload payload) throws CrudException {
108 Edge edge = dao.getEdge(id, type, new HashMap<String, String>());
109 Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
110 return updateEdge(version, validatedEdge);
113 private String updateEdge(String version, Edge edge) throws CrudException {
114 Edge updatedEdge = dao.updateEdge(edge);
115 return CrudResponseBuilder
116 .buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, updatedEdge), version);
119 public String patchEdge(String version, String id, String type, EdgePayload payload) throws CrudException {
120 Edge edge = dao.getEdge(id, type, new HashMap<String, String>());
121 Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
122 return updateEdge(version, patchedEdge);
127 protected Vertex addBulkVertex(Vertex vertex, String version, String dbTransId) throws CrudException {
128 return dao.addVertex(vertex.getType(), vertex.getProperties(), version, dbTransId);
132 protected Vertex updateBulkVertex(Vertex vertex, String id, String version, String dbTransId) throws CrudException {
133 return dao.updateVertex(id, vertex.getType(), vertex.getProperties(), version, dbTransId);
137 protected void deleteBulkVertex(String id, String version, String type, String dbTransId) throws CrudException {
138 dao.deleteVertex(id, type, dbTransId);
142 protected Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException {
143 return dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(), edge.getProperties(), version, dbTransId);
147 protected Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException {
148 return dao.updateEdge(edge, dbTransId);
152 protected void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException {
153 dao.deleteEdge(id, type, dbTransId);