ce60c3c3df25457b8c0240d82a99dd9c977bb740
[aai/gizmo.git] / src / main / java / org / openecomp / crud / service / CrudGraphDataService.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.openecomp.crud.service;
25
26 import com.att.ecomp.event.api.EventPublisher;
27
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;
40
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Properties;
44
45 public class CrudGraphDataService {
46
47   private GraphDao dao;
48
49   public CrudGraphDataService(EventPublisher champEventPublisher) throws CrudException {
50
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"));
62
63     if (CrudProperties.get("crud.graph.name") != null) {
64       champProperties.put(ChampDao.CONFIG_GRAPH_NAME, CrudProperties.get("crud.graph.name"));
65     }
66
67     if (champEventPublisher != null) {
68       champProperties.put(ChampDao.CONFIG_EVENT_STREAM_PUBLISHER, champEventPublisher);
69     }
70
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)));
74     }
75
76     ChampDao champDao = new ChampDao(champProperties);
77
78     this.dao = champDao;
79
80     //load the schemas
81     OxmModelLoader.loadModels();
82     RelationshipSchemaLoader.loadModels();
83   }
84
85
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);
90   }
91
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),
96             version);
97   }
98
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);
102   }
103
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);
109   }
110
111   public String getEdge(String version, String id, String type) throws CrudException {
112     RelationshipSchemaValidator.validateType(version, type);
113     Edge edge = dao.getEdge(id, type);
114
115     return CrudResponseBuilder.buildGetEdgeResponse(RelationshipSchemaValidator
116             .validateOutgoingPayload(version, edge),
117         version);
118   }
119
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);
126   }
127
128
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);
134
135   }
136
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);
143   }
144
145   public String patchVertex(String version, String id, String type, VertexPayload payload)
146       throws CrudException {
147     Vertex existingVertex = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version,
148         type));
149     Vertex vertex = OxmModelValidator.validateIncomingPatchPayload(id, version, type,
150         payload.getProperties(), existingVertex);
151     return updateVertex(version, vertex);
152
153   }
154
155   public String deleteVertex(String version, String id, String type) throws CrudException {
156     type = OxmModelValidator.resolveCollectionType(version, type);
157     dao.deleteVertex(id, type);
158     return "";
159
160   }
161
162   public String deleteEdge(String version, String id, String type) throws CrudException {
163     RelationshipSchemaValidator.validateType(version, type);
164     dao.deleteEdge(id, type);
165     return "";
166
167   }
168
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,
173         version, payload);
174     return updateEdge(version, validatedEdge);
175
176   }
177
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);
182   }
183
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,
188         version, payload);
189     return updateEdge(version, patchedEdge);
190
191   }
192
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);
199   }
200
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,
205         type, filter));
206     return CrudResponseBuilder.buildGetVerticesResponse(items, version);
207   }
208
209 }