13c2de9470e22dae0dcf0bb6812dc923110d8285
[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 com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25
26 import java.lang.reflect.Method;
27
28 import org.onap.aai.exceptions.AAIException;
29 import org.onap.aai.util.AAIConfig;
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, String resourceName, String methodName,
47             AAIExtensionMap aaiExtMap, boolean isPreExtension) throws AAIException {
48         String extensionClassName = "org.onap.aai.extensions." + apiVersion.toLowerCase() + "." + namespace + "."
49                 + resourceName + "Extension";
50         String defaultErrorCallback = resourceName + "ExtensionErrorCallback";
51
52         String configOption = "aai.extensions." + apiVersion.toLowerCase() + "." + namespace.toLowerCase() + "."
53                 + resourceName.toLowerCase() + ".enabled";
54
55         try {
56
57             String extensionEnabled = AAIConfig.get(configOption, "true");
58             if (extensionEnabled.equalsIgnoreCase("false")) {
59                 return;
60             }
61
62             Class<?> clazz = Class.forName(extensionClassName);
63
64             Method extension = clazz.getMethod(methodName, new Class[] {AAIExtensionMap.class});
65             if (extension != null) {
66
67                 Object ret = extension.invoke(clazz.newInstance(), aaiExtMap);
68
69                 if (ret instanceof Integer) {
70                     Exception e = null;
71
72                     if (isPreExtension == true) {
73                         e = aaiExtMap.getPreExtException();
74                     } else {
75                         e = aaiExtMap.getPostExtException();
76                     }
77
78                     boolean failOnError = true;
79                     if (isPreExtension == true) {
80                         failOnError = aaiExtMap.getPreExtFailOnError();
81                     } else {
82                         failOnError = aaiExtMap.getPostExtFailOnError();
83                     }
84
85                     if (e != null) {
86                         boolean handleException = true;
87                         if (isPreExtension == true) {
88                             if (aaiExtMap.getPreExtSkipErrorCallback() == true) {
89                                 handleException = false;
90                             }
91                         } else {
92                             if (aaiExtMap.getPostExtSkipErrorCallback() == true) {
93                                 handleException = false;
94                             }
95                         }
96                         if (handleException == true) {
97                             Method errorCallback = null;
98                             if (isPreExtension == true) {
99                                 errorCallback = aaiExtMap.getPreExtErrorCallback();
100                             } else {
101                                 errorCallback = aaiExtMap.getPostExtErrorCallback();
102                             }
103
104                             if (errorCallback != null) {
105                                 errorCallback.invoke(clazz.newInstance(), aaiExtMap);
106                             } else {
107                                 Method defaultErrorCallbackExtension =
108                                         clazz.getMethod(defaultErrorCallback, new Class[] {AAIExtensionMap.class});
109                                 defaultErrorCallbackExtension.invoke(clazz.newInstance(), aaiExtMap);
110                             }
111                         }
112                     }
113
114                     if (failOnError == true && e != null) {
115                         throw e;
116                     } else if (failOnError == false && e != null) { // in this
117                                                                     // case, we
118                                                                     // just note
119                                                                     // the error
120                                                                     // without
121                                                                     // stopping
122                         LOGGER.warn("Error while processing extension - " + aaiExtMap.getMessage());
123                     }
124                 }
125             }
126         } catch (ClassNotFoundException ex) {
127             LOGGER.debug("Extension class not found: " + extensionClassName + ", method: " + methodName + ".");
128         } catch (NoSuchMethodException e) {
129             LOGGER.debug("Method " + methodName + " does not exist for class " + extensionClassName);
130         } catch (AAIException e) {
131             throw e;
132         } catch (Exception e) {
133             throw new AAIException("AAI_5105", e);
134         }
135     }
136 }