summaryrefslogtreecommitdiffstats
path: root/champ-lib/champ-core/src/main/java/org/onap/aai/champcore/graph/impl/AbstractValidatingChampGraph.java
blob: 4568bc3c98d35846760910e5ed2057c156ea630e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
 * ============LICENSE_START==========================================
 * org.onap.aai
 * ===================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.onap.aai.champcore.graph.impl;

import java.util.Map;
import java.util.Optional;

import org.onap.aai.champcore.ChampTransaction;
import org.onap.aai.champcore.event.AbstractLoggingChampGraph;
import org.onap.aai.champcore.exceptions.ChampMarshallingException;
import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
import org.onap.aai.champcore.exceptions.ChampTransactionException;
import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
import org.onap.aai.champcore.model.ChampObject;
import org.onap.aai.champcore.model.ChampObjectConstraint;
import org.onap.aai.champcore.model.ChampPartition;
import org.onap.aai.champcore.model.ChampRelationship;
import org.onap.aai.champcore.model.ChampRelationshipConstraint;
import org.onap.aai.champcore.model.ChampSchema;
import org.onap.aai.champcore.schema.ChampSchemaEnforcer;

public abstract class AbstractValidatingChampGraph extends AbstractLoggingChampGraph {

	private ChampSchema schema = ChampSchema.emptySchema();

	protected abstract ChampSchemaEnforcer getSchemaEnforcer();
	protected abstract boolean isShutdown();

	/**
	 * Updates an existing vertex in the graph data store.
	 * <p>
     * If a transaction context is not provided, then a transaction will be automatically 
     * created and committed for this operation only, otherwise, the supplied transaction
     * will be used and it will be up to the caller to commit the transaction at its 
     * discretion.
     * <p>
     * 
	 * @param object      - The vertex to be updated in the graph data store.
	 * @param transaction - Optional transaction context to perform the operation in.
	 * 
	 * @return - The updated vertex, marshaled as a {@link ChampObject}
	 * 
	 * @throws ChampMarshallingException     - If the {@code relationship} is not able to be 
     *                                         marshalled into the backend representation
	 * @throws ChampObjectNotExistsException - If {@link org.onap.aai.champcore.model.ChampObject#getKey} 
     *                                         is not present or object not found in the graph
	 */
	protected abstract ChampObject doReplaceObject(ChampObject object, Optional<ChampTransaction> transaction) throws ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException, ChampTransactionException;
	
    /** 
     * Creates or updates a vertex in the graph data store.
     * <p>
     * If a transaction context is not provided, then a transaction will be automatically 
     * created and committed for this operation only, otherwise, the supplied transaction
     * will be used and it will be up to the caller to commit the transaction at its 
     * discretion.
     * 
	 * @param object      - The vertex to be stored in the graph data store.
	 * @param transaction - Optional transaction context to perform the operation in.
	 * 
	 * @return - The vertex which was created, marshaled as a {@link ChampObject}
	 * 
     * @throws ChampMarshallingException     - If the {@code relationship} is not able to be 
     *                                         marshaled into the back end representation
     * @throws ChampObjectNotExistsException - If {@link org.onap.aai.champcore.model.ChampObject#getKey} 
     *                                         is not present or object not found in the graph
	 */
	protected abstract ChampObject doStoreObject(ChampObject object, Optional<ChampTransaction> transaction) throws ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException;
	
	/**
	 * Replaces an edge in the graph data store.
     * <p>
     * If a transaction context is not provided, then a transaction will be automatically 
     * created and committed for this operation only, otherwise, the supplied transaction
     * will be used and it will be up to the caller to commit the transaction at its 
     * discretion.
     * 
	 * @param relationship - The edge to be replaced in the graph data store.
	 * @param transaction  - Optional transaction context to perform the operation in.
	 * 
	 * @return - The edge as it was replaced, marshaled as a {@link ChampRelationship}
	 * 
	 * @throws ChampUnmarshallingException         - If the edge which was created could not be 
     *                                               unmarshaled into a ChampObject
	 * @throws ChampRelationshipNotExistsException - If {@link org.onap.aai.champcore.model.ChampRelationship#getKey}.isPresent() 
     *                                               but the object cannot be found in the graph
	 * @throws ChampMarshallingException           - If the {@code relationship} is not able to be 
     *                                               marshaled into the back end representation
	 */
	protected abstract ChampRelationship doReplaceRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction) throws ChampUnmarshallingException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampTransactionException;
	
	/**
	 * Creates or updates a relationship in the graph data store.
     * <p>
     * If a transaction context is not provided, then a transaction will be automatically 
     * created and committed for this operation only, otherwise, the supplied transaction
     * will be used and it will be up to the caller to commit the transaction at its 
     * discretion.
     *  
	 * @param relationship - The relationship to be stored in the graph data store.
	 * @param transaction  - Optional transaction context to perform the operation in.
	 * 
	 * @return - The relationship that was stored.
	 * 
	 * @throws ChampUnmarshallingException         - If the edge which was created could not be 
     *                                               unmarshalled into a ChampObject
	 * @throws ChampObjectNotExistsException       - If {@link org.onap.aai.champcore.model.ChampObject#getKey} 
     *                                               is not present or object not found in the graph
	 * @throws ChampRelationshipNotExistsException - If {@link org.onap.aai.champcore.model.ChampRelationship#getKey}.isPresent() 
   *                                                 but the object cannot be found in the graph
	 * @throws ChampMarshallingException           - If the {@code relationship} is not able to be 
     *                                               marshalled into the backend representation
	 */
	protected abstract ChampRelationship doStoreRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction) throws ChampUnmarshallingException, ChampObjectNotExistsException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampTransactionException;
		
	/**
	 * Creates or updates a partition in the graph data store.
	 * 
	 * @param partition   - The partition to be stored in the graph data store.
	 * @param transaction - Optional transaction context to perform the operation in.
	 * 
	 * @return - The partition that was stored.
	 * 
	 * @throws ChampRelationshipNotExistsException - If {@link org.onap.aai.champcore.model.ChampRelationship#getKey}.isPresent() 
   *                                                 but the object cannot be found in the graph
	 * @throws ChampMarshallingException           - If the {@code relationship} is not able to be 
     *                                               marshalled into the backend representation
	 * @throws ChampObjectNotExistsException       - If {@link org.onap.aai.champcore.model.ChampObject#getKey} 
     *                                               is not present or object not found in the graph
	 */
	protected abstract ChampPartition doStorePartition(ChampPartition partition, Optional<ChampTransaction> transaction) throws ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException;

	protected AbstractValidatingChampGraph(Map<String, Object> properties) {
	  super(properties);
	}
	
	@Override
	public ChampObject executeStoreObject(ChampObject object, Optional<ChampTransaction> transaction)
			throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException {
	  
	  if (isShutdown()) {
	    throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
	  }

      validate(object);

      return doStoreObject(object, transaction);
	}
	  

	@Override
	public ChampObject executeReplaceObject(ChampObject object, Optional<ChampTransaction> transaction)
			throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException {
	  
		if (isShutdown()) {
		  throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
		}

		validate(object);

		return doReplaceObject(object, transaction);
	}

	@Override
	public ChampRelationship executeStoreRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction)
			throws ChampUnmarshallingException, ChampMarshallingException, ChampObjectNotExistsException, ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampTransactionException {	
		
	  if (isShutdown()) {
	    throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
	  }

	  validate(relationship);

	  return doStoreRelationship(relationship, transaction);
	}
	
	@Override
	public ChampRelationship executeReplaceRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction)
			throws ChampUnmarshallingException, ChampMarshallingException, ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampTransactionException {	
		
	  if (isShutdown()) {
	    throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
	  }

	  validate(relationship);

	  return doReplaceRelationship(relationship, transaction);
	}

	@Override
	public ChampPartition executeStorePartition(ChampPartition partition, Optional<ChampTransaction> transaction) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException {

	  if (isShutdown()) {
	    throw new IllegalStateException("Cannot use ChampAPI after calling shutdown()");
	  }

	  validate(partition);

	  return doStorePartition(partition, transaction);
	}

	protected void validate(ChampObject object) throws ChampSchemaViolationException {
		final Optional<ChampObjectConstraint> objectConstraint = retrieveSchema().getObjectConstraint(object.getType());

 		if (objectConstraint.isPresent()) getSchemaEnforcer().validate(object, objectConstraint.get());
	}

	protected void validate(ChampRelationship relationship) throws ChampSchemaViolationException {
		final ChampSchema graphSchema = retrieveSchema();
		final Optional<ChampRelationshipConstraint> relationshipConstraint = graphSchema.getRelationshipConstraint(relationship.getType());
		final Optional<ChampObjectConstraint> sourceObjConstraint = graphSchema.getObjectConstraint(relationship.getSource().getType());
 		final Optional<ChampObjectConstraint> targetObjConstraint = graphSchema.getObjectConstraint(relationship.getTarget().getType());

 		if (relationshipConstraint.isPresent()) getSchemaEnforcer().validate(relationship, relationshipConstraint.get());
		if (sourceObjConstraint.isPresent()) getSchemaEnforcer().validate(relationship.getSource(), sourceObjConstraint.get());
		if (targetObjConstraint.isPresent()) getSchemaEnforcer().validate(relationship.getTarget(), targetObjConstraint.get());
	}

	protected void validate(ChampPartition partition) throws ChampSchemaViolationException {
		for (ChampObject object : partition.getChampObjects()) {
			validate(object);
		}

		for (ChampRelationship relationship : partition.getChampRelationships()) {
			validate(relationship);
		}
	}

	@Override
	public void storeSchema(ChampSchema schema) throws ChampSchemaViolationException {
		if (isShutdown()) throw new IllegalStateException("Cannot call storeSchema() after shutdown has been initiated");

		this.schema = schema;
	}

	@Override
	public ChampSchema retrieveSchema() {
		if (isShutdown()) throw new IllegalStateException("Cannot call retrieveSchema() after shutdown has been initiated");

		return schema;
	}

	@Override
	public void updateSchema(ChampObjectConstraint objectConstraint) throws ChampSchemaViolationException {
		if (isShutdown()) throw new IllegalStateException("Cannot call updateSchema() after shutdown has been initiated");

		final ChampSchema currentSchema = retrieveSchema();
		final ChampSchema updatedSchema = new ChampSchema.Builder(currentSchema)
												.constraint(objectConstraint)
												.build();
		
		storeSchema(updatedSchema);
	}

	@Override
	public void updateSchema(ChampRelationshipConstraint relationshipConstraint) throws ChampSchemaViolationException {
		if (isShutdown()) throw new IllegalStateException("Cannot call updateSchema() after shutdown has been initiated");

		final ChampSchema currentSchema = retrieveSchema();
		final ChampSchema updatedSchema = new ChampSchema.Builder(currentSchema)
												.constraint(relationshipConstraint)
												.build();
		
		storeSchema(updatedSchema);
	}

	@Override
	public void deleteSchema() {
		if (isShutdown()) throw new IllegalStateException("Cannot call deleteSchema() after shutdown has been initiated");
		this.schema = ChampSchema.emptySchema();
	}
}