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=========================================================
20 package org.onap.aai.extensions;
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;
27 import java.lang.reflect.Method;
29 public class ExtensionController {
31 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ExtensionController.class);
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
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";
52 String configOption = "aai.extensions." + apiVersion.toLowerCase()
53 + "." + namespace.toLowerCase() + "."
54 + resourceName.toLowerCase() + ".enabled";
58 String extensionEnabled = AAIConfig.get(configOption, "true");
59 if (extensionEnabled.equalsIgnoreCase("false")) {
63 Class<?> clazz = Class.forName(extensionClassName);
65 Method extension = clazz.getMethod(methodName,
66 new Class[] { AAIExtensionMap.class });
67 if (extension != null) {
69 Object ret = extension.invoke(clazz.newInstance(), aaiExtMap);
71 if (ret instanceof Integer) {
74 if (isPreExtension == true) {
75 e = aaiExtMap.getPreExtException();
77 e = aaiExtMap.getPostExtException();
80 boolean failOnError = true;
81 if (isPreExtension == true) {
82 failOnError = aaiExtMap.getPreExtFailOnError();
84 failOnError = aaiExtMap.getPostExtFailOnError();
88 boolean handleException = true;
89 if (isPreExtension == true) {
90 if (aaiExtMap.getPreExtSkipErrorCallback() == true) {
91 handleException = false;
94 if (aaiExtMap.getPostExtSkipErrorCallback() == true) {
95 handleException = false;
98 if (handleException == true) {
99 Method errorCallback = null;
100 if (isPreExtension == true) {
101 errorCallback = aaiExtMap
102 .getPreExtErrorCallback();
104 errorCallback = aaiExtMap
105 .getPostExtErrorCallback();
108 if (errorCallback != null) {
109 errorCallback.invoke(clazz.newInstance(),
112 Method defaultErrorCallbackExtension = clazz
114 defaultErrorCallback,
115 new Class[] { AAIExtensionMap.class });
116 defaultErrorCallbackExtension.invoke(
117 clazz.newInstance(), aaiExtMap);
122 if (failOnError == true && e != null) {
124 } else if (failOnError == false && e != null) { // in this
130 LOGGER.warn("Error while processing extension - " + aaiExtMap.getMessage());
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) {
140 } catch (Exception e) {
141 throw new AAIException("AAI_5105", e);