2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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============================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.champ.graph.impl;
25 import java.util.Optional;
27 import org.onap.aai.champ.ChampGraph;
28 import org.onap.aai.champ.event.AbstractLoggingChampGraph;
29 import org.onap.aai.champ.event.ChampEvent;
30 import org.onap.aai.champ.event.ChampEvent.ChampOperation;
31 import org.onap.aai.champ.exceptions.ChampMarshallingException;
32 import org.onap.aai.champ.exceptions.ChampObjectNotExistsException;
33 import org.onap.aai.champ.exceptions.ChampRelationshipNotExistsException;
34 import org.onap.aai.champ.exceptions.ChampSchemaViolationException;
35 import org.onap.aai.champ.exceptions.ChampUnmarshallingException;
36 import org.onap.aai.champ.graph.impl.TitanChampGraphImpl.Builder;
37 import org.onap.aai.champ.model.ChampObject;
38 import org.onap.aai.champ.model.ChampObjectConstraint;
39 import org.onap.aai.champ.model.ChampPartition;
40 import org.onap.aai.champ.model.ChampRelationship;
41 import org.onap.aai.champ.model.ChampRelationshipConstraint;
42 import org.onap.aai.champ.model.ChampSchema;
43 import org.onap.aai.champ.schema.ChampSchemaEnforcer;
45 public abstract class AbstractValidatingChampGraph extends AbstractLoggingChampGraph {
47 private ChampSchema schema = ChampSchema.emptySchema();
49 protected abstract ChampSchemaEnforcer getSchemaEnforcer();
50 protected abstract boolean isShutdown();
52 protected abstract ChampObject doReplaceObject(ChampObject object) throws ChampMarshallingException, ChampObjectNotExistsException;
53 protected abstract ChampObject doStoreObject(ChampObject object) throws ChampMarshallingException, ChampObjectNotExistsException;
54 protected abstract ChampRelationship doReplaceRelationship(ChampRelationship relationship) throws ChampUnmarshallingException, ChampRelationshipNotExistsException, ChampMarshallingException;
55 protected abstract ChampRelationship doStoreRelationship(ChampRelationship relationship) throws ChampUnmarshallingException, ChampObjectNotExistsException, ChampRelationshipNotExistsException, ChampMarshallingException;
56 protected abstract ChampPartition doStorePartition(ChampPartition partition) throws ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException;
58 protected AbstractValidatingChampGraph(Map<String, Object> properties) {
62 public ChampObject executeStoreObject(ChampObject object)
63 throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException {
64 if (isShutdown()) throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
68 return doStoreObject(object);
71 public ChampObject executeReplaceObject(ChampObject object)
72 throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException {
73 if (isShutdown()) throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
77 return doReplaceObject(object);
80 public ChampRelationship executeStoreRelationship(ChampRelationship relationship)
81 throws ChampUnmarshallingException, ChampMarshallingException, ChampObjectNotExistsException, ChampSchemaViolationException, ChampRelationshipNotExistsException {
82 if (isShutdown()) throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
84 validate(relationship);
86 return doStoreRelationship(relationship);
89 public ChampRelationship executeReplaceRelationship(ChampRelationship relationship)
90 throws ChampUnmarshallingException, ChampMarshallingException, ChampSchemaViolationException, ChampRelationshipNotExistsException {
91 if (isShutdown()) throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
93 validate(relationship);
95 return doReplaceRelationship(relationship);
98 public ChampPartition executeStorePartition(ChampPartition partition) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException {
99 if (isShutdown()) throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
103 return doStorePartition(partition);
106 protected void validate(ChampObject object) throws ChampSchemaViolationException {
107 final Optional<ChampObjectConstraint> objectConstraint = retrieveSchema().getObjectConstraint(object.getType());
109 if (objectConstraint.isPresent()) getSchemaEnforcer().validate(object, objectConstraint.get());
112 protected void validate(ChampRelationship relationship) throws ChampSchemaViolationException {
113 final ChampSchema graphSchema = retrieveSchema();
114 final Optional<ChampRelationshipConstraint> relationshipConstraint = graphSchema.getRelationshipConstraint(relationship.getType());
115 final Optional<ChampObjectConstraint> sourceObjConstraint = graphSchema.getObjectConstraint(relationship.getSource().getType());
116 final Optional<ChampObjectConstraint> targetObjConstraint = graphSchema.getObjectConstraint(relationship.getTarget().getType());
118 if (relationshipConstraint.isPresent()) getSchemaEnforcer().validate(relationship, relationshipConstraint.get());
119 if (sourceObjConstraint.isPresent()) getSchemaEnforcer().validate(relationship.getSource(), sourceObjConstraint.get());
120 if (targetObjConstraint.isPresent()) getSchemaEnforcer().validate(relationship.getTarget(), targetObjConstraint.get());
123 protected void validate(ChampPartition partition) throws ChampSchemaViolationException {
124 for (ChampObject object : partition.getChampObjects()) {
128 for (ChampRelationship relationship : partition.getChampRelationships()) {
129 validate(relationship);
134 public void storeSchema(ChampSchema schema) throws ChampSchemaViolationException {
135 if (isShutdown()) throw new IllegalStateException("Cannot call storeSchema() after shutdown has been initiated");
137 this.schema = schema;
141 public ChampSchema retrieveSchema() {
142 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveSchema() after shutdown has been initiated");
148 public void updateSchema(ChampObjectConstraint objectConstraint) throws ChampSchemaViolationException {
149 if (isShutdown()) throw new IllegalStateException("Cannot call updateSchema() after shutdown has been initiated");
151 final ChampSchema currentSchema = retrieveSchema();
152 final ChampSchema updatedSchema = new ChampSchema.Builder(currentSchema)
153 .constraint(objectConstraint)
156 storeSchema(updatedSchema);
160 public void updateSchema(ChampRelationshipConstraint relationshipConstraint) throws ChampSchemaViolationException {
161 if (isShutdown()) throw new IllegalStateException("Cannot call updateSchema() after shutdown has been initiated");
163 final ChampSchema currentSchema = retrieveSchema();
164 final ChampSchema updatedSchema = new ChampSchema.Builder(currentSchema)
165 .constraint(relationshipConstraint)
168 storeSchema(updatedSchema);
172 public void deleteSchema() {
173 if (isShutdown()) throw new IllegalStateException("Cannot call deleteSchema() after shutdown has been initiated");
174 this.schema = ChampSchema.emptySchema();