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.openecomp.crud.service;
26 import org.onap.aai.event.api.EventPublisher;
28 import org.openecomp.crud.dao.GraphDao;
29 import org.openecomp.crud.dao.champ.ChampDao;
30 import org.openecomp.crud.entity.Edge;
31 import org.openecomp.crud.entity.Vertex;
32 import org.openecomp.crud.exception.CrudException;
33 import org.openecomp.crud.parser.CrudResponseBuilder;
34 import org.openecomp.crud.util.CrudProperties;
35 import org.openecomp.crud.util.CrudServiceConstants;
36 import org.openecomp.schema.OxmModelLoader;
37 import org.openecomp.schema.OxmModelValidator;
38 import org.openecomp.schema.RelationshipSchemaLoader;
39 import org.openecomp.schema.RelationshipSchemaValidator;
41 import java.util.List;
43 import java.util.Properties;
45 public class CrudGraphDataService {
49 public CrudGraphDataService(EventPublisher champEventPublisher) throws CrudException {
51 // Configure the GraphDao and wire it
52 Properties champProperties = new Properties();
53 champProperties.put(ChampDao.CONFIG_STORAGE_BACKEND, "titan");
54 champProperties.put(ChampDao.CONFIG_STORAGE_BACKEND_DB,
55 CrudProperties.get(CrudServiceConstants.CRD_STORAGE_BACKEND_DB, "hbase"));
56 champProperties.put(ChampDao.CONFIG_STORAGE_HOSTNAMES,
57 CrudProperties.get(CrudServiceConstants.CRD_GRAPH_HOST));
58 champProperties.put(ChampDao.CONFIG_STORAGE_PORT,
59 CrudProperties.get(CrudServiceConstants.CRD_GRAPH_PORT, "2181"));
60 champProperties.put(ChampDao.CONFIG_HBASE_ZNODE_PARENT,
61 CrudProperties.get(CrudServiceConstants.CRD_HBASE_ZNODE_PARENT, "/hbase-unsecure"));
63 if (CrudProperties.get("crud.graph.name") != null) {
64 champProperties.put(ChampDao.CONFIG_GRAPH_NAME, CrudProperties.get("crud.graph.name"));
67 if (champEventPublisher != null) {
68 champProperties.put(ChampDao.CONFIG_EVENT_STREAM_PUBLISHER, champEventPublisher);
71 if (CrudProperties.get(ChampDao.CONFIG_EVENT_STREAM_NUM_PUBLISHERS) != null) {
72 champProperties.put(ChampDao.CONFIG_EVENT_STREAM_NUM_PUBLISHERS,
73 Integer.parseInt(CrudProperties.get(ChampDao.CONFIG_EVENT_STREAM_NUM_PUBLISHERS)));
76 ChampDao champDao = new ChampDao(champProperties);
81 OxmModelLoader.loadModels();
82 RelationshipSchemaLoader.loadModels();
86 public String addVertex(String version, String type, VertexPayload payload) throws CrudException {
87 Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, type,
88 payload.getProperties());
89 return addVertex(version, vertex);
92 private String addVertex(String version, Vertex vertex) throws CrudException {
93 Vertex addedVertex = dao.addVertex(vertex.getType(), vertex.getProperties());
94 return CrudResponseBuilder
95 .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, addedVertex),
99 public String addEdge(String version, String type, EdgePayload payload) throws CrudException {
100 Edge edge = RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload);
101 return addEdge(version, edge);
104 private String addEdge(String version, Edge edge) throws CrudException {
105 Edge addedEdge = dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(),
106 edge.getProperties());
107 return CrudResponseBuilder.buildUpsertEdgeResponse(
108 RelationshipSchemaValidator.validateOutgoingPayload(version, addedEdge), version);
111 public String getEdge(String version, String id, String type) throws CrudException {
112 RelationshipSchemaValidator.validateType(version, type);
113 Edge edge = dao.getEdge(id, type);
115 return CrudResponseBuilder.buildGetEdgeResponse(RelationshipSchemaValidator
116 .validateOutgoingPayload(version, edge),
120 public String getEdges(String version, String type, Map<String, String> filter)
121 throws CrudException {
122 RelationshipSchemaValidator.validateType(version, type);
123 List<Edge> items = dao.getEdges(type, RelationshipSchemaValidator
124 .resolveCollectionfilter(version, type, filter));
125 return CrudResponseBuilder.buildGetEdgesResponse(items, version);
129 public String updateVertex(String version, String id, String type, VertexPayload payload)
130 throws CrudException {
131 Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(id, version, type,
132 payload.getProperties());
133 return updateVertex(version, vertex);
137 private String updateVertex(String version, Vertex vertex) throws CrudException {
138 Vertex updatedVertex = dao.updateVertex(vertex.getId().get(), vertex.getType(),
139 vertex.getProperties());
140 return CrudResponseBuilder
141 .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version,
142 updatedVertex), version);
145 public String patchVertex(String version, String id, String type, VertexPayload payload)
146 throws CrudException {
147 Vertex existingVertex = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version,
149 Vertex vertex = OxmModelValidator.validateIncomingPatchPayload(id, version, type,
150 payload.getProperties(), existingVertex);
151 return updateVertex(version, vertex);
155 public String deleteVertex(String version, String id, String type) throws CrudException {
156 type = OxmModelValidator.resolveCollectionType(version, type);
157 dao.deleteVertex(id, type);
162 public String deleteEdge(String version, String id, String type) throws CrudException {
163 RelationshipSchemaValidator.validateType(version, type);
164 dao.deleteEdge(id, type);
169 public String updateEdge(String version, String id, String type, EdgePayload payload)
170 throws CrudException {
171 Edge edge = dao.getEdge(id, type);
172 Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge,
174 return updateEdge(version, validatedEdge);
178 private String updateEdge(String version, Edge edge) throws CrudException {
179 Edge updatedEdge = dao.updateEdge(edge);
180 return CrudResponseBuilder.buildUpsertEdgeResponse(
181 RelationshipSchemaValidator.validateOutgoingPayload(version, updatedEdge), version);
184 public String patchEdge(String version, String id, String type, EdgePayload payload)
185 throws CrudException {
186 Edge edge = dao.getEdge(id, type);
187 Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(edge,
189 return updateEdge(version, patchedEdge);
193 public String getVertex(String version, String id, String type) throws CrudException {
194 type = OxmModelValidator.resolveCollectionType(version, type);
195 Vertex vertex = dao.getVertex(id, type);
196 List<Edge> edges = dao.getVertexEdges(id);
197 return CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator
198 .validateOutgoingPayload(version, vertex), edges, version);
201 public String getVertices(String version, String type, Map<String, String> filter)
202 throws CrudException {
203 type = OxmModelValidator.resolveCollectionType(version, type);
204 List<Vertex> items = dao.getVertices(type, OxmModelValidator.resolveCollectionfilter(version,
206 return CrudResponseBuilder.buildGetVerticesResponse(items, version);