2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.extensions;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.util.AAIConfig;
29 import java.lang.reflect.Method;
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,
47 String resourceName, String methodName, AAIExtensionMap aaiExtMap,
48 boolean isPreExtension) throws AAIException {
49 String extensionClassName = "org.onap.aai.extensions."
50 + apiVersion.toLowerCase() + "." + namespace + "."
51 + resourceName + "Extension";
52 String defaultErrorCallback = resourceName + "ExtensionErrorCallback";
54 String configOption = "aai.extensions." + apiVersion.toLowerCase()
55 + "." + namespace.toLowerCase() + "."
56 + resourceName.toLowerCase() + ".enabled";
60 String extensionEnabled = AAIConfig.get(configOption, "true");
61 if (extensionEnabled.equalsIgnoreCase("false")) {
65 Class<?> clazz = Class.forName(extensionClassName);
67 Method extension = clazz.getMethod(methodName,
68 new Class[] { AAIExtensionMap.class });
69 if (extension != null) {
71 Object ret = extension.invoke(clazz.newInstance(), aaiExtMap);
73 if (ret instanceof Integer) {
76 if (isPreExtension == true) {
77 e = aaiExtMap.getPreExtException();
79 e = aaiExtMap.getPostExtException();
82 boolean failOnError = true;
83 if (isPreExtension == true) {
84 failOnError = aaiExtMap.getPreExtFailOnError();
86 failOnError = aaiExtMap.getPostExtFailOnError();
90 boolean handleException = true;
91 if (isPreExtension == true) {
92 if (aaiExtMap.getPreExtSkipErrorCallback() == true) {
93 handleException = false;
96 if (aaiExtMap.getPostExtSkipErrorCallback() == true) {
97 handleException = false;
100 if (handleException == true) {
101 Method errorCallback = null;
102 if (isPreExtension == true) {
103 errorCallback = aaiExtMap
104 .getPreExtErrorCallback();
106 errorCallback = aaiExtMap
107 .getPostExtErrorCallback();
110 if (errorCallback != null) {
111 errorCallback.invoke(clazz.newInstance(),
114 Method defaultErrorCallbackExtension = clazz
116 defaultErrorCallback,
117 new Class[] { AAIExtensionMap.class });
118 defaultErrorCallbackExtension.invoke(
119 clazz.newInstance(), aaiExtMap);
124 if (failOnError == true && e != null) {
126 } else if (failOnError == false && e != null) { // in this
132 LOGGER.warn("Error while processing extension - " + aaiExtMap.getMessage());
136 } catch (ClassNotFoundException ex) {
137 LOGGER.debug("Extension class not found: " + extensionClassName + ", method: " + methodName + ".");
138 } catch (NoSuchMethodException e) {
139 LOGGER.debug("Method " + methodName + " does not exist for class " + extensionClassName);
140 } catch (AAIException e) {
142 } catch (Exception e) {
143 throw new AAIException("AAI_5105", e);