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