17127da7fdd544f3f12f32169f1153356522d2d3
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / introspection / sideeffect / SideEffectRunner.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11      http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.aai.introspection.sideeffect;
22
23 import org.apache.tinkerpop.gremlin.structure.Vertex;
24 import org.openecomp.aai.exceptions.AAIException;
25 import org.openecomp.aai.introspection.Introspector;
26 import org.openecomp.aai.serialization.db.DBSerializer;
27 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
28
29 import java.io.UnsupportedEncodingException;
30 import java.lang.reflect.InvocationTargetException;
31 import java.net.URISyntaxException;
32 import java.util.LinkedHashSet;
33 import java.util.Set;
34
35 public class SideEffectRunner {
36
37         protected final TransactionalGraphEngine dbEngine;
38         protected final DBSerializer serializer;
39         protected final Set<Class<? extends SideEffect>> sideEffects;
40         protected SideEffectRunner(Builder builder) {
41                 this.dbEngine = builder.getDbEngine();
42                 this.serializer = builder.getSerializer();
43                 this.sideEffects = builder.getSideEffects();
44         }
45         
46         public void execute(Introspector obj, Vertex self) throws AAIException {
47                 
48                 for (Class<? extends SideEffect> se : sideEffects) {
49                         try {
50                                 se.getConstructor(Introspector.class, Vertex.class, TransactionalGraphEngine.class, DBSerializer.class)
51                                 .newInstance(obj, self, dbEngine, serializer).execute();
52                         } catch (UnsupportedEncodingException | InstantiationException | IllegalAccessException
53                                         | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException
54                                         | URISyntaxException e) {
55                                 throw new AAIException("strange exception", e);
56                         }
57                 }               
58         }
59
60         public static class Builder {
61                 
62                 private final TransactionalGraphEngine dbEngine;
63                 private final DBSerializer serializer;
64                 private final Set<Class<? extends SideEffect>> sideEffects;
65                 
66                 public Builder(final TransactionalGraphEngine dbEngine, final DBSerializer serializer) {
67                         this.dbEngine = dbEngine;
68                         this.serializer = serializer;
69                         this.sideEffects = new LinkedHashSet<>();
70                 }
71                 
72                 public Builder addSideEffect(Class<? extends SideEffect> se) {
73                         sideEffects.add(se);
74                         return this;
75                 }
76                 
77                 public Builder addSideEffects(Class<? extends SideEffect>... sideEffects) {
78                         for (Class<? extends SideEffect> se : sideEffects) {
79                                 this.addSideEffect(se);
80                         }
81                         return this;
82                 }
83                 
84                 public SideEffectRunner build() {
85                         return new SideEffectRunner(this);
86                 }
87                 protected TransactionalGraphEngine getDbEngine() {
88                         return dbEngine;
89                 }
90
91                 protected DBSerializer getSerializer() {
92                         return serializer;
93                 }
94
95                 protected Set<Class<? extends SideEffect>> getSideEffects() {
96                         return sideEffects;
97                 }
98         }
99 }