f38fe0fa3f58024a1ca17395c45b7a349044f620
[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 org.onap.aaiutils.oxm.OxmModelLoader;
27 import org.openecomp.aai.champcore.ChampGraph;
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.schema.OxmModelValidator;
35 import org.openecomp.schema.RelationshipSchemaLoader;
36 import org.openecomp.schema.RelationshipSchemaValidator;
37
38 import java.util.List;
39 import java.util.Map;
40
41 public class CrudGraphDataService {
42
43   private GraphDao dao;
44
45   public CrudGraphDataService(ChampGraph graphImpl) throws CrudException {
46
47         this.dao = new ChampDao(graphImpl);
48
49     //load the schemas
50     try {
51       OxmModelLoader.loadModels();
52     } catch (Exception e) {
53       throw new CrudException(e);
54     }
55     RelationshipSchemaLoader.loadModels();
56   }
57
58
59   public String addVertex(String version, String type, VertexPayload payload) throws CrudException {
60     Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, type,
61         payload.getProperties());
62     return addVertex(version, vertex);
63   }
64
65   private String addVertex(String version, Vertex vertex) throws CrudException {
66     Vertex addedVertex = dao.addVertex(vertex.getType(), vertex.getProperties());
67     return CrudResponseBuilder
68         .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, addedVertex),
69             version);
70   }
71
72   public String addEdge(String version, String type, EdgePayload payload) throws CrudException {
73     Edge edge = RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload);
74     return addEdge(version, edge);
75   }
76
77   private String addEdge(String version, Edge edge) throws CrudException {
78     Edge addedEdge = dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(),
79         edge.getProperties());
80     return CrudResponseBuilder.buildUpsertEdgeResponse(
81         RelationshipSchemaValidator.validateOutgoingPayload(version, addedEdge), version);
82   }
83
84   public String getEdge(String version, String id, String type) throws CrudException {
85     RelationshipSchemaValidator.validateType(version, type);
86     Edge edge = dao.getEdge(id, type);
87
88     return CrudResponseBuilder.buildGetEdgeResponse(RelationshipSchemaValidator
89             .validateOutgoingPayload(version, edge),
90         version);
91   }
92
93   public String getEdges(String version, String type, Map<String, String> filter)
94       throws CrudException {
95     RelationshipSchemaValidator.validateType(version, type);
96     List<Edge> items = dao.getEdges(type, RelationshipSchemaValidator
97         .resolveCollectionfilter(version, type, filter));
98     return CrudResponseBuilder.buildGetEdgesResponse(items, version);
99   }
100
101
102   public String updateVertex(String version, String id, String type, VertexPayload payload)
103       throws CrudException {
104     Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(id, version, type,
105         payload.getProperties());
106     return updateVertex(version, vertex);
107
108   }
109
110   private String updateVertex(String version, Vertex vertex) throws CrudException {
111     Vertex updatedVertex = dao.updateVertex(vertex.getId().get(), vertex.getType(),
112         vertex.getProperties());
113     return CrudResponseBuilder
114         .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version,
115             updatedVertex), version);
116   }
117
118   public String patchVertex(String version, String id, String type, VertexPayload payload)
119       throws CrudException {
120     Vertex existingVertex = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version,
121         type));
122     Vertex vertex = OxmModelValidator.validateIncomingPatchPayload(id, version, type,
123         payload.getProperties(), existingVertex);
124     return updateVertex(version, vertex);
125
126   }
127
128   public String deleteVertex(String version, String id, String type) throws CrudException {
129     type = OxmModelValidator.resolveCollectionType(version, type);
130     dao.deleteVertex(id, type);
131     return "";
132
133   }
134
135   public String deleteEdge(String version, String id, String type) throws CrudException {
136     RelationshipSchemaValidator.validateType(version, type);
137     dao.deleteEdge(id, type);
138     return "";
139
140   }
141
142   public String updateEdge(String version, String id, String type, EdgePayload payload)
143       throws CrudException {
144     Edge edge = dao.getEdge(id, type);
145     Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge,
146         version, payload);
147     return updateEdge(version, validatedEdge);
148
149   }
150
151   private String updateEdge(String version, Edge edge) throws CrudException {
152     Edge updatedEdge = dao.updateEdge(edge);
153     return CrudResponseBuilder.buildUpsertEdgeResponse(
154         RelationshipSchemaValidator.validateOutgoingPayload(version, updatedEdge), version);
155   }
156
157   public String patchEdge(String version, String id, String type, EdgePayload payload)
158       throws CrudException {
159     Edge edge = dao.getEdge(id, type);
160     Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(edge,
161         version, payload);
162     return updateEdge(version, patchedEdge);
163
164   }
165
166   public Vertex getVertex(String id) throws CrudException {
167     return dao.getVertex(id);
168   }
169   
170   public String getVertex(String version, String id, String type) throws CrudException {
171     type = OxmModelValidator.resolveCollectionType(version, type);
172     Vertex vertex = dao.getVertex(id, type);
173     List<Edge> edges = dao.getVertexEdges(id);
174     return CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator
175             .validateOutgoingPayload(version, vertex), edges, version);
176   }
177
178   public String getVertices(String version, String type, Map<String, String> filter)
179       throws CrudException {
180     type = OxmModelValidator.resolveCollectionType(version, type);
181     List<Vertex> items = dao.getVertices(type, OxmModelValidator.resolveCollectionfilter(version,
182         type, filter));
183     return CrudResponseBuilder.buildGetVerticesResponse(items, version);
184   }
185
186 }