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