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.aai.champcore.graph.impl;
24 import java.util.Optional;
26 import org.onap.aai.champcore.ChampTransaction;
27 import org.onap.aai.champcore.event.AbstractLoggingChampGraph;
28 import org.onap.aai.champcore.exceptions.ChampMarshallingException;
29 import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
30 import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
31 import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
32 import org.onap.aai.champcore.exceptions.ChampTransactionException;
33 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
34 import org.onap.aai.champcore.model.ChampObject;
35 import org.onap.aai.champcore.model.ChampObjectConstraint;
36 import org.onap.aai.champcore.model.ChampPartition;
37 import org.onap.aai.champcore.model.ChampRelationship;
38 import org.onap.aai.champcore.model.ChampRelationshipConstraint;
39 import org.onap.aai.champcore.model.ChampSchema;
40 import org.onap.aai.champcore.schema.ChampSchemaEnforcer;
42 public abstract class AbstractValidatingChampGraph extends AbstractLoggingChampGraph {
44 private ChampSchema schema = ChampSchema.emptySchema();
46 protected abstract ChampSchemaEnforcer getSchemaEnforcer();
47 protected abstract boolean isShutdown();
50 * Updates an existing vertex in the graph data store.
52 * If a transaction context is not provided, then a transaction will be automatically
53 * created and committed for this operation only, otherwise, the supplied transaction
54 * will be used and it will be up to the caller to commit the transaction at its
58 * @param object - The vertex to be updated in the graph data store.
59 * @param transaction - Optional transaction context to perform the operation in.
61 * @return - The updated vertex, marshaled as a {@link ChampObject}
63 * @throws ChampMarshallingException - If the {@code relationship} is not able to be
64 * marshalled into the backend representation
65 * @throws ChampObjectNotExistsException - If {@link org.onap.aai.champcore.model.ChampObject#getKey}
66 * is not present or object not found in the graph
68 protected abstract ChampObject doReplaceObject(ChampObject object, Optional<ChampTransaction> transaction) throws ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException, ChampTransactionException;
71 * Creates or updates a vertex in the graph data store.
73 * If a transaction context is not provided, then a transaction will be automatically
74 * created and committed for this operation only, otherwise, the supplied transaction
75 * will be used and it will be up to the caller to commit the transaction at its
78 * @param object - The vertex to be stored in the graph data store.
79 * @param transaction - Optional transaction context to perform the operation in.
81 * @return - The vertex which was created, marshaled as a {@link ChampObject}
83 * @throws ChampMarshallingException - If the {@code relationship} is not able to be
84 * marshaled into the back end representation
85 * @throws ChampObjectNotExistsException - If {@link org.onap.aai.champcore.model.ChampObject#getKey}
86 * is not present or object not found in the graph
88 protected abstract ChampObject doStoreObject(ChampObject object, Optional<ChampTransaction> transaction) throws ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException;
91 * Replaces an edge in the graph data store.
93 * If a transaction context is not provided, then a transaction will be automatically
94 * created and committed for this operation only, otherwise, the supplied transaction
95 * will be used and it will be up to the caller to commit the transaction at its
98 * @param relationship - The edge to be replaced in the graph data store.
99 * @param transaction - Optional transaction context to perform the operation in.
101 * @return - The edge as it was replaced, marshaled as a {@link ChampRelationship}
103 * @throws ChampUnmarshallingException - If the edge which was created could not be
104 * unmarshaled into a ChampObject
105 * @throws ChampRelationshipNotExistsException - If {@link org.onap.aai.champcore.model.ChampRelationship#getKey}.isPresent()
106 * but the object cannot be found in the graph
107 * @throws ChampMarshallingException - If the {@code relationship} is not able to be
108 * marshaled into the back end representation
110 protected abstract ChampRelationship doReplaceRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction) throws ChampUnmarshallingException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampTransactionException;
113 * Creates or updates a relationship in the graph data store.
115 * If a transaction context is not provided, then a transaction will be automatically
116 * created and committed for this operation only, otherwise, the supplied transaction
117 * will be used and it will be up to the caller to commit the transaction at its
120 * @param relationship - The relationship to be stored in the graph data store.
121 * @param transaction - Optional transaction context to perform the operation in.
123 * @return - The relationship that was stored.
125 * @throws ChampUnmarshallingException - If the edge which was created could not be
126 * unmarshalled into a ChampObject
127 * @throws ChampObjectNotExistsException - If {@link org.onap.aai.champcore.model.ChampObject#getKey}
128 * is not present or object not found in the graph
129 * @throws ChampRelationshipNotExistsException - If {@link org.onap.aai.champcore.model.ChampRelationship#getKey}.isPresent()
130 * but the object cannot be found in the graph
131 * @throws ChampMarshallingException - If the {@code relationship} is not able to be
132 * marshalled into the backend representation
134 protected abstract ChampRelationship doStoreRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction) throws ChampUnmarshallingException, ChampObjectNotExistsException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampTransactionException;
137 * Creates or updates a partition in the graph data store.
139 * @param partition - The partition to be stored in the graph data store.
140 * @param transaction - Optional transaction context to perform the operation in.
142 * @return - The partition that was stored.
144 * @throws ChampRelationshipNotExistsException - If {@link org.onap.aai.champcore.model.ChampRelationship#getKey}.isPresent()
145 * but the object cannot be found in the graph
146 * @throws ChampMarshallingException - If the {@code relationship} is not able to be
147 * marshalled into the backend representation
148 * @throws ChampObjectNotExistsException - If {@link org.onap.aai.champcore.model.ChampObject#getKey}
149 * is not present or object not found in the graph
151 protected abstract ChampPartition doStorePartition(ChampPartition partition, Optional<ChampTransaction> transaction) throws ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException;
153 protected AbstractValidatingChampGraph(Map<String, Object> properties) {
158 public ChampObject executeStoreObject(ChampObject object, Optional<ChampTransaction> transaction)
159 throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException {
162 throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
167 return doStoreObject(object, transaction);
172 public ChampObject executeReplaceObject(ChampObject object, Optional<ChampTransaction> transaction)
173 throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException {
176 throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
181 return doReplaceObject(object, transaction);
185 public ChampRelationship executeStoreRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction)
186 throws ChampUnmarshallingException, ChampMarshallingException, ChampObjectNotExistsException, ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampTransactionException {
189 throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
192 validate(relationship);
194 return doStoreRelationship(relationship, transaction);
198 public ChampRelationship executeReplaceRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction)
199 throws ChampUnmarshallingException, ChampMarshallingException, ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampTransactionException {
202 throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
205 validate(relationship);
207 return doReplaceRelationship(relationship, transaction);
211 public ChampPartition executeStorePartition(ChampPartition partition, Optional<ChampTransaction> transaction) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException {
214 throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
219 return doStorePartition(partition, transaction);
222 protected void validate(ChampObject object) throws ChampSchemaViolationException {
223 final Optional<ChampObjectConstraint> objectConstraint = retrieveSchema().getObjectConstraint(object.getType());
225 if (objectConstraint.isPresent()) getSchemaEnforcer().validate(object, objectConstraint.get());
228 protected void validate(ChampRelationship relationship) throws ChampSchemaViolationException {
229 final ChampSchema graphSchema = retrieveSchema();
230 final Optional<ChampRelationshipConstraint> relationshipConstraint = graphSchema.getRelationshipConstraint(relationship.getType());
231 final Optional<ChampObjectConstraint> sourceObjConstraint = graphSchema.getObjectConstraint(relationship.getSource().getType());
232 final Optional<ChampObjectConstraint> targetObjConstraint = graphSchema.getObjectConstraint(relationship.getTarget().getType());
234 if (relationshipConstraint.isPresent()) getSchemaEnforcer().validate(relationship, relationshipConstraint.get());
235 if (sourceObjConstraint.isPresent()) getSchemaEnforcer().validate(relationship.getSource(), sourceObjConstraint.get());
236 if (targetObjConstraint.isPresent()) getSchemaEnforcer().validate(relationship.getTarget(), targetObjConstraint.get());
239 protected void validate(ChampPartition partition) throws ChampSchemaViolationException {
240 for (ChampObject object : partition.getChampObjects()) {
244 for (ChampRelationship relationship : partition.getChampRelationships()) {
245 validate(relationship);
250 public void storeSchema(ChampSchema schema) throws ChampSchemaViolationException {
251 if (isShutdown()) throw new IllegalStateException("Cannot call storeSchema() after shutdown has been initiated");
253 this.schema = schema;
257 public ChampSchema retrieveSchema() {
258 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveSchema() after shutdown has been initiated");
264 public void updateSchema(ChampObjectConstraint objectConstraint) throws ChampSchemaViolationException {
265 if (isShutdown()) throw new IllegalStateException("Cannot call updateSchema() after shutdown has been initiated");
267 final ChampSchema currentSchema = retrieveSchema();
268 final ChampSchema updatedSchema = new ChampSchema.Builder(currentSchema)
269 .constraint(objectConstraint)
272 storeSchema(updatedSchema);
276 public void updateSchema(ChampRelationshipConstraint relationshipConstraint) throws ChampSchemaViolationException {
277 if (isShutdown()) throw new IllegalStateException("Cannot call updateSchema() after shutdown has been initiated");
279 final ChampSchema currentSchema = retrieveSchema();
280 final ChampSchema updatedSchema = new ChampSchema.Builder(currentSchema)
281 .constraint(relationshipConstraint)
284 storeSchema(updatedSchema);
288 public void deleteSchema() {
289 if (isShutdown()) throw new IllegalStateException("Cannot call deleteSchema() after shutdown has been initiated");
290 this.schema = ChampSchema.emptySchema();