Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / sideeffect / SideEffectRunner.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.onap.aai.introspection.sideeffect;
22
23 import java.io.UnsupportedEncodingException;
24 import java.lang.reflect.InvocationTargetException;
25 import java.net.URISyntaxException;
26 import java.util.LinkedHashSet;
27 import java.util.Set;
28
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.Introspector;
32 import org.onap.aai.serialization.db.DBSerializer;
33 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
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
41     protected SideEffectRunner(Builder builder) {
42         this.dbEngine = builder.getDbEngine();
43         this.serializer = builder.getSerializer();
44         this.sideEffects = builder.getSideEffects();
45     }
46
47     public void execute(Introspector obj, Vertex self) throws AAIException {
48
49         for (Class<? extends SideEffect> se : sideEffects) {
50             try {
51                 se.getConstructor(Introspector.class, Vertex.class, TransactionalGraphEngine.class, DBSerializer.class)
52                         .newInstance(obj, self, dbEngine, serializer).execute();
53             } catch (UnsupportedEncodingException | InstantiationException | IllegalAccessException
54                     | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException
55                     | URISyntaxException e) {
56                 throw new AAIException("strange exception", e);
57             }
58         }
59     }
60
61     public static class Builder {
62
63         private final TransactionalGraphEngine dbEngine;
64         private final DBSerializer serializer;
65         private final Set<Class<? extends SideEffect>> sideEffects;
66
67         public Builder(final TransactionalGraphEngine dbEngine, final DBSerializer serializer) {
68             this.dbEngine = dbEngine;
69             this.serializer = serializer;
70             this.sideEffects = new LinkedHashSet<>();
71         }
72
73         public Builder addSideEffect(Class<? extends SideEffect> se) {
74             sideEffects.add(se);
75             return this;
76         }
77
78         public Builder addSideEffects(Class<? extends SideEffect>... sideEffects) {
79             for (Class<? extends SideEffect> se : sideEffects) {
80                 this.addSideEffect(se);
81             }
82             return this;
83         }
84
85         public SideEffectRunner build() {
86             return new SideEffectRunner(this);
87         }
88
89         protected TransactionalGraphEngine getDbEngine() {
90             return dbEngine;
91         }
92
93         protected DBSerializer getSerializer() {
94             return serializer;
95         }
96
97         protected Set<Class<? extends SideEffect>> getSideEffects() {
98             return sideEffects;
99         }
100     }
101 }