2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.extensions;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
26 import java.lang.reflect.Method;
28 import org.onap.aai.exceptions.AAIException;
29 import org.onap.aai.util.AAIConfig;
31 public class ExtensionController {
33 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ExtensionController.class);
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
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";
52 String configOption = "aai.extensions." + apiVersion.toLowerCase() + "." + namespace.toLowerCase() + "."
53 + resourceName.toLowerCase() + ".enabled";
57 String extensionEnabled = AAIConfig.get(configOption, "true");
58 if (extensionEnabled.equalsIgnoreCase("false")) {
62 Class<?> clazz = Class.forName(extensionClassName);
64 Method extension = clazz.getMethod(methodName, new Class[] {AAIExtensionMap.class});
65 if (extension != null) {
67 Object ret = extension.invoke(clazz.newInstance(), aaiExtMap);
69 if (ret instanceof Integer) {
72 if (isPreExtension == true) {
73 e = aaiExtMap.getPreExtException();
75 e = aaiExtMap.getPostExtException();
78 boolean failOnError = true;
79 if (isPreExtension == true) {
80 failOnError = aaiExtMap.getPreExtFailOnError();
82 failOnError = aaiExtMap.getPostExtFailOnError();
86 boolean handleException = true;
87 if (isPreExtension == true) {
88 if (aaiExtMap.getPreExtSkipErrorCallback() == true) {
89 handleException = false;
92 if (aaiExtMap.getPostExtSkipErrorCallback() == true) {
93 handleException = false;
96 if (handleException == true) {
97 Method errorCallback = null;
98 if (isPreExtension == true) {
99 errorCallback = aaiExtMap.getPreExtErrorCallback();
101 errorCallback = aaiExtMap.getPostExtErrorCallback();
104 if (errorCallback != null) {
105 errorCallback.invoke(clazz.newInstance(), aaiExtMap);
107 Method defaultErrorCallbackExtension =
108 clazz.getMethod(defaultErrorCallback, new Class[] {AAIExtensionMap.class});
109 defaultErrorCallbackExtension.invoke(clazz.newInstance(), aaiExtMap);
114 if (failOnError == true && e != null) {
116 } else if (failOnError == false && e != null) { // in this
122 LOGGER.warn("Error while processing extension - " + aaiExtMap.getMessage());
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) {
132 } catch (Exception e) {
133 throw new AAIException("AAI_5105", e);