Merge "Added @Override annotation above signature"
[aai/champ.git] / src / main / java / org / openecomp / aai / champ / graph / impl / AbstractValidatingChampGraph.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
21  */
22 package org.openecomp.aai.champ.graph.impl;
23
24 import java.util.Map;
25 import java.util.Optional;
26
27 import org.openecomp.aai.champ.ChampGraph;
28 import org.openecomp.aai.champ.event.AbstractLoggingChampGraph;
29 import org.openecomp.aai.champ.event.ChampEvent;
30 import org.openecomp.aai.champ.event.ChampEvent.ChampOperation;
31 import org.openecomp.aai.champ.exceptions.ChampMarshallingException;
32 import org.openecomp.aai.champ.exceptions.ChampObjectNotExistsException;
33 import org.openecomp.aai.champ.exceptions.ChampRelationshipNotExistsException;
34 import org.openecomp.aai.champ.exceptions.ChampSchemaViolationException;
35 import org.openecomp.aai.champ.exceptions.ChampUnmarshallingException;
36 import org.openecomp.aai.champ.graph.impl.TitanChampGraphImpl.Builder;
37 import org.openecomp.aai.champ.model.ChampObject;
38 import org.openecomp.aai.champ.model.ChampObjectConstraint;
39 import org.openecomp.aai.champ.model.ChampPartition;
40 import org.openecomp.aai.champ.model.ChampRelationship;
41 import org.openecomp.aai.champ.model.ChampRelationshipConstraint;
42 import org.openecomp.aai.champ.model.ChampSchema;
43 import org.openecomp.aai.champ.schema.ChampSchemaEnforcer;
44
45 public abstract class AbstractValidatingChampGraph extends AbstractLoggingChampGraph {
46
47         private ChampSchema schema = ChampSchema.emptySchema();
48
49         protected abstract ChampSchemaEnforcer getSchemaEnforcer();
50         protected abstract boolean isShutdown();
51
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;
57
58         protected AbstractValidatingChampGraph(Map<String, Object> properties) {
59           super(properties);
60         }
61         
62         public ChampObject executeStoreObject(ChampObject object)
63                         throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException {
64                 if (isShutdown()) throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
65
66                 validate(object);
67
68                 return doStoreObject(object);
69         }
70         
71         public ChampObject executeReplaceObject(ChampObject object)
72                         throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException {
73                 if (isShutdown()) throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
74
75                 validate(object);
76
77                 return doReplaceObject(object);
78         }
79
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()");
83
84                 validate(relationship);
85
86                 return doStoreRelationship(relationship);
87         }
88         
89         public ChampRelationship executeReplaceRelationship(ChampRelationship relationship)
90                         throws ChampUnmarshallingException, ChampMarshallingException, ChampSchemaViolationException, ChampRelationshipNotExistsException {     
91                 if (isShutdown()) throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
92
93                 validate(relationship);
94
95                 return doReplaceRelationship(relationship);
96         }
97
98         public ChampPartition executeStorePartition(ChampPartition partition) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException {
99                 if (isShutdown()) throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
100
101                 validate(partition);
102
103                 return doStorePartition(partition);
104         }
105
106         protected void validate(ChampObject object) throws ChampSchemaViolationException {
107                 final Optional<ChampObjectConstraint> objectConstraint = retrieveSchema().getObjectConstraint(object.getType());
108
109                 if (objectConstraint.isPresent()) getSchemaEnforcer().validate(object, objectConstraint.get());
110         }
111
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());
117
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());
121         }
122
123         protected void validate(ChampPartition partition) throws ChampSchemaViolationException {
124                 for (ChampObject object : partition.getChampObjects()) {
125                         validate(object);
126                 }
127
128                 for (ChampRelationship relationship : partition.getChampRelationships()) {
129                         validate(relationship);
130                 }
131         }
132
133         @Override
134         public void storeSchema(ChampSchema schema) throws ChampSchemaViolationException {
135                 if (isShutdown()) throw new IllegalStateException("Cannot call storeSchema() after shutdown has been initiated");
136
137                 this.schema = schema;
138         }
139
140         @Override
141         public ChampSchema retrieveSchema() {
142                 if (isShutdown()) throw new IllegalStateException("Cannot call retrieveSchema() after shutdown has been initiated");
143
144                 return schema;
145         }
146
147         @Override
148         public void updateSchema(ChampObjectConstraint objectConstraint) throws ChampSchemaViolationException {
149                 if (isShutdown()) throw new IllegalStateException("Cannot call updateSchema() after shutdown has been initiated");
150
151                 final ChampSchema currentSchema = retrieveSchema();
152                 final ChampSchema updatedSchema = new ChampSchema.Builder(currentSchema)
153                                                                                                 .constraint(objectConstraint)
154                                                                                                 .build();
155                 
156                 storeSchema(updatedSchema);
157         }
158
159         @Override
160         public void updateSchema(ChampRelationshipConstraint relationshipConstraint) throws ChampSchemaViolationException {
161                 if (isShutdown()) throw new IllegalStateException("Cannot call updateSchema() after shutdown has been initiated");
162
163                 final ChampSchema currentSchema = retrieveSchema();
164                 final ChampSchema updatedSchema = new ChampSchema.Builder(currentSchema)
165                                                                                                 .constraint(relationshipConstraint)
166                                                                                                 .build();
167                 
168                 storeSchema(updatedSchema);
169         }
170
171         @Override
172         public void deleteSchema() {
173                 if (isShutdown()) throw new IllegalStateException("Cannot call deleteSchema() after shutdown has been initiated");
174                 this.schema = ChampSchema.emptySchema();
175         }
176 }