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