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 java.lang.reflect.Method;
25 import org.onap.aai.exceptions.AAIException;
26 import org.onap.aai.util.AAIConfig;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class ExtensionController {
32 private static final Logger LOGGER = LoggerFactory.getLogger(ExtensionController.class);
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
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";
51 String configOption = "aai.extensions." + apiVersion.toLowerCase() + "." + namespace.toLowerCase() + "."
52 + resourceName.toLowerCase() + ".enabled";
56 String extensionEnabled = AAIConfig.get(configOption, "true");
57 if (extensionEnabled.equalsIgnoreCase("false")) {
61 Class<?> clazz = Class.forName(extensionClassName);
63 Method extension = clazz.getMethod(methodName, new Class[] {AAIExtensionMap.class});
64 if (extension != null) {
66 Object ret = extension.invoke(clazz.newInstance(), aaiExtMap);
68 if (ret instanceof Integer) {
71 if (isPreExtension == true) {
72 e = aaiExtMap.getPreExtException();
74 e = aaiExtMap.getPostExtException();
77 boolean failOnError = true;
78 if (isPreExtension == true) {
79 failOnError = aaiExtMap.getPreExtFailOnError();
81 failOnError = aaiExtMap.getPostExtFailOnError();
85 boolean handleException = true;
86 if (isPreExtension == true) {
87 if (aaiExtMap.getPreExtSkipErrorCallback() == true) {
88 handleException = false;
91 if (aaiExtMap.getPostExtSkipErrorCallback() == true) {
92 handleException = false;
95 if (handleException == true) {
96 Method errorCallback = null;
97 if (isPreExtension == true) {
98 errorCallback = aaiExtMap.getPreExtErrorCallback();
100 errorCallback = aaiExtMap.getPostExtErrorCallback();
103 if (errorCallback != null) {
104 errorCallback.invoke(clazz.newInstance(), aaiExtMap);
106 Method defaultErrorCallbackExtension =
107 clazz.getMethod(defaultErrorCallback, new Class[] {AAIExtensionMap.class});
108 defaultErrorCallbackExtension.invoke(clazz.newInstance(), aaiExtMap);
113 if (failOnError == true && e != null) {
115 } else if (failOnError == false && e != null) { // in this
121 LOGGER.warn("Error while processing extension - " + aaiExtMap.getMessage());
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) {
131 } catch (Exception e) {
132 throw new AAIException("AAI_5105", e);