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