Change openecomp to onap and update 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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.introspection.sideeffect;
23
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.onap.aai.exceptions.AAIException;
26 import org.onap.aai.introspection.Introspector;
27 import org.onap.aai.serialization.db.DBSerializer;
28 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
29
30 import java.io.UnsupportedEncodingException;
31 import java.lang.reflect.InvocationTargetException;
32 import java.net.URISyntaxException;
33 import java.util.LinkedHashSet;
34 import java.util.Set;
35
36 public class SideEffectRunner {
37
38         protected final TransactionalGraphEngine dbEngine;
39         protected final DBSerializer serializer;
40         protected final Set<Class<? extends SideEffect>> sideEffects;
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                 protected TransactionalGraphEngine getDbEngine() {
89                         return dbEngine;
90                 }
91
92                 protected DBSerializer getSerializer() {
93                         return serializer;
94                 }
95
96                 protected Set<Class<? extends SideEffect>> getSideEffects() {
97                         return sideEffects;
98                 }
99         }
100 }