2ec305618980720c947d54411b30762e21bea8f3
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / extensions / ExtensionController.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.extensions;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.util.AAIConfig;
28
29 import java.lang.reflect.Method;
30
31 public class ExtensionController {
32
33         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ExtensionController.class);
34
35         /**
36          * Run extension.
37          *
38          * @param apiVersion the api version
39          * @param namespace the namespace
40          * @param resourceName the resource name
41          * @param methodName the method name
42          * @param aaiExtMap the aai ext map
43          * @param isPreExtension the is pre extension
44          * @throws AAIException the AAI exception
45          */
46         public void runExtension(String apiVersion, String namespace,
47                         String resourceName, String methodName, AAIExtensionMap aaiExtMap,
48                         boolean isPreExtension) throws AAIException {
49                 String extensionClassName = "org.onap.aai.extensions."
50                                 + apiVersion.toLowerCase() + "." + namespace + "."
51                                 + resourceName + "Extension";
52                 String defaultErrorCallback = resourceName + "ExtensionErrorCallback";
53
54                 String configOption = "aai.extensions." + apiVersion.toLowerCase()
55                                 + "." + namespace.toLowerCase() + "."
56                                 + resourceName.toLowerCase() + ".enabled";
57
58                 try {
59
60                         String extensionEnabled = AAIConfig.get(configOption, "true");
61                         if (extensionEnabled.equalsIgnoreCase("false")) {
62                                 return;
63                         }
64
65                         Class<?> clazz = Class.forName(extensionClassName);
66
67                         Method extension = clazz.getMethod(methodName,
68                                         new Class[] { AAIExtensionMap.class });
69                         if (extension != null) {
70
71                                 Object ret = extension.invoke(clazz.newInstance(), aaiExtMap);
72
73                                 if (ret instanceof Integer) {
74                                         Exception e = null;
75                                         
76                                         if (isPreExtension == true) {
77                                                 e = aaiExtMap.getPreExtException();
78                                         } else {
79                                                 e = aaiExtMap.getPostExtException();
80                                         }
81
82                                         boolean failOnError = true;
83                                         if (isPreExtension == true) {
84                                                 failOnError = aaiExtMap.getPreExtFailOnError();
85                                         } else {
86                                                 failOnError = aaiExtMap.getPostExtFailOnError();
87                                         }
88
89                                         if (e != null) {
90                                                 boolean handleException = true;
91                                                 if (isPreExtension == true) {
92                                                         if (aaiExtMap.getPreExtSkipErrorCallback() == true) { 
93                                                                 handleException = false;
94                                                         }
95                                                 } else {
96                                                         if (aaiExtMap.getPostExtSkipErrorCallback() == true) { 
97                                                                 handleException = false;
98                                                         }
99                                                 }
100                                                 if (handleException == true) {
101                                                         Method errorCallback = null;
102                                                         if (isPreExtension == true) {
103                                                                 errorCallback = aaiExtMap
104                                                                                 .getPreExtErrorCallback();
105                                                         } else {
106                                                                 errorCallback = aaiExtMap
107                                                                                 .getPostExtErrorCallback();
108                                                         }
109
110                                                         if (errorCallback != null) {
111                                                                 errorCallback.invoke(clazz.newInstance(),
112                                                                                 aaiExtMap);
113                                                         } else {
114                                                                 Method defaultErrorCallbackExtension = clazz
115                                                                                 .getMethod(
116                                                                                                 defaultErrorCallback,
117                                                                                                 new Class[] { AAIExtensionMap.class });
118                                                                 defaultErrorCallbackExtension.invoke(
119                                                                                 clazz.newInstance(), aaiExtMap);
120                                                         }
121                                                 }
122                                         }
123
124                                         if (failOnError == true && e != null) {
125                                                 throw e;
126                                         } else if (failOnError == false && e != null) { // in this
127                                                                                                                                         // case, we
128                                                                                                                                         // just note
129                                                                                                                                         // the error
130                                                                                                                                         // without
131                                                                                                                                         // stopping
132                                                 LOGGER.warn("Error while processing extension - " + aaiExtMap.getMessage());
133                                         }
134                                 }
135                         }
136                 } catch (ClassNotFoundException ex) {
137                         LOGGER.debug("Extension class not found: " + extensionClassName + ", method: " + methodName + ".");
138                 } catch (NoSuchMethodException e) {
139                         LOGGER.debug("Method " + methodName + " does not exist for class " + extensionClassName); 
140                 } catch (AAIException e) {
141                         throw e;
142                 } catch (Exception e) {
143                         throw new AAIException("AAI_5105", e);
144                 } 
145         }
146 }