2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.crud.service;
24 import java.util.HashMap;
26 import org.onap.crud.dao.GraphDao;
27 import org.onap.crud.entity.Edge;
29 import org.onap.crud.entity.Vertex;
30 import org.onap.crud.exception.CrudException;
31 import org.onap.crud.parser.CrudResponseBuilder;
32 import org.onap.schema.OxmModelValidator;
33 import org.onap.schema.RelationshipSchemaValidator;
36 public class CrudGraphDataService extends AbstractGraphDataService {
39 public CrudGraphDataService(GraphDao dao) throws CrudException {
45 public CrudGraphDataService(GraphDao dao, GraphDao daoForGet) throws CrudException {
48 this.daoForGet = daoForGet;
51 public String addVertex(String version, String type, VertexPayload payload) throws CrudException {
52 Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, type, payload.getProperties());
53 return addVertex(version, vertex);
56 private String addVertex(String version, Vertex vertex) throws CrudException {
57 Vertex addedVertex = dao.addVertex(vertex.getType(), vertex.getProperties(), version);
58 return CrudResponseBuilder
59 .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, addedVertex), version);
62 public String addEdge(String version, String type, EdgePayload payload) throws CrudException {
63 Edge edge = RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload);
64 return addEdge(version, edge);
67 private String addEdge(String version, Edge edge) throws CrudException {
68 Edge addedEdge = dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(), edge.getProperties(), version);
69 return CrudResponseBuilder
70 .buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, addedEdge), version);
73 public String updateVertex(String version, String id, String type, VertexPayload payload) throws CrudException {
74 Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(id, version, type, payload.getProperties());
75 return updateVertex(version, vertex);
79 private String updateVertex(String version, Vertex vertex) throws CrudException {
80 Vertex updatedVertex = dao.updateVertex(vertex.getId().get(), vertex.getType(), vertex.getProperties(), version);
81 return CrudResponseBuilder
82 .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, updatedVertex), version);
85 public String patchVertex(String version, String id, String type, VertexPayload payload) throws CrudException {
86 Vertex existingVertex = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version, type), version, new HashMap<String, String>());
87 Vertex vertex = OxmModelValidator.validateIncomingPatchPayload(id, version, type, payload.getProperties(),
89 return updateVertex(version, vertex);
92 public String deleteVertex(String version, String id, String type) throws CrudException {
93 type = OxmModelValidator.resolveCollectionType(version, type);
94 dao.deleteVertex(id, type);
98 public String deleteEdge(String version, String id, String type) throws CrudException {
99 RelationshipSchemaValidator.validateType(version, type);
100 dao.deleteEdge(id, type);
104 public String updateEdge(String version, String id, String type, EdgePayload payload) throws CrudException {
105 Edge edge = dao.getEdge(id, type, new HashMap<String, String>());
106 Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
107 return updateEdge(version, validatedEdge);
110 private String updateEdge(String version, Edge edge) throws CrudException {
111 Edge updatedEdge = dao.updateEdge(edge);
112 return CrudResponseBuilder
113 .buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, updatedEdge), version);
116 public String patchEdge(String version, String id, String type, EdgePayload payload) throws CrudException {
117 Edge edge = dao.getEdge(id, type, new HashMap<String, String>());
118 Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
119 return updateEdge(version, patchedEdge);
124 protected Vertex addBulkVertex(Vertex vertex, String version, String dbTransId) throws CrudException {
125 return dao.addVertex(vertex.getType(), vertex.getProperties(), version, dbTransId);
129 protected Vertex updateBulkVertex(Vertex vertex, String id, String version, String dbTransId) throws CrudException {
130 return dao.updateVertex(id, vertex.getType(), vertex.getProperties(), version, dbTransId);
134 protected void deleteBulkVertex(String id, String version, String type, String dbTransId) throws CrudException {
135 dao.deleteVertex(id, type, dbTransId);
139 protected Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException {
140 return dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(), edge.getProperties(), version, dbTransId);
144 protected Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException {
145 return dao.updateEdge(edge, dbTransId);
149 protected void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException {
150 dao.deleteEdge(id, type, dbTransId);