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