ffd9a8c8e4e13fdd3069c02852735719e3c0b378
[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 com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25
26 import java.io.UnsupportedEncodingException;
27 import java.lang.reflect.InvocationTargetException;
28 import java.net.URISyntaxException;
29 import java.util.LinkedHashSet;
30 import java.util.Set;
31
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.introspection.Introspector;
35 import org.onap.aai.serialization.db.DBSerializer;
36 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
37
38 public class SideEffectRunner {
39
40     protected final TransactionalGraphEngine dbEngine;
41     protected final DBSerializer serializer;
42     protected final Set<Class<? extends SideEffect>> sideEffects;
43
44     protected SideEffectRunner(Builder builder) {
45         this.dbEngine = builder.getDbEngine();
46         this.serializer = builder.getSerializer();
47         this.sideEffects = builder.getSideEffects();
48     }
49
50     public void execute(Introspector obj, Vertex self) throws AAIException {
51
52         for (Class<? extends SideEffect> se : sideEffects) {
53             try {
54                 se.getConstructor(Introspector.class, Vertex.class, TransactionalGraphEngine.class, DBSerializer.class)
55                         .newInstance(obj, self, dbEngine, serializer).execute();
56             } catch (UnsupportedEncodingException | InstantiationException | IllegalAccessException
57                     | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException
58                     | URISyntaxException e) {
59                 throw new AAIException("strange exception", e);
60             }
61         }
62     }
63
64     public static class Builder {
65
66         private final TransactionalGraphEngine dbEngine;
67         private final DBSerializer serializer;
68         private final Set<Class<? extends SideEffect>> sideEffects;
69
70         public Builder(final TransactionalGraphEngine dbEngine, final DBSerializer serializer) {
71             this.dbEngine = dbEngine;
72             this.serializer = serializer;
73             this.sideEffects = new LinkedHashSet<>();
74         }
75
76         public Builder addSideEffect(Class<? extends SideEffect> se) {
77             sideEffects.add(se);
78             return this;
79         }
80
81         public Builder addSideEffects(Class<? extends SideEffect>... sideEffects) {
82             for (Class<? extends SideEffect> se : sideEffects) {
83                 this.addSideEffect(se);
84             }
85             return this;
86         }
87
88         public SideEffectRunner build() {
89             return new SideEffectRunner(this);
90         }
91
92         protected TransactionalGraphEngine getDbEngine() {
93             return dbEngine;
94         }
95
96         protected DBSerializer getSerializer() {
97             return serializer;
98         }
99
100         protected Set<Class<? extends SideEffect>> getSideEffects() {
101             return sideEffects;
102         }
103     }
104 }