2 * ============LICENSE_START=======================================================
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
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.openecomp.aai.extensions;
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;
28 import java.lang.reflect.Method;
30 public class ExtensionController {
32 private static final EELFLogger LOGGER = EELFManager.getInstance().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,
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";
53 String configOption = "aai.extensions." + apiVersion.toLowerCase()
54 + "." + namespace.toLowerCase() + "."
55 + resourceName.toLowerCase() + ".enabled";
59 String extensionEnabled = AAIConfig.get(configOption, "true");
60 if (extensionEnabled.equalsIgnoreCase("false")) {
64 Class<?> clazz = Class.forName(extensionClassName);
66 Method extension = clazz.getMethod(methodName,
67 new Class[] { AAIExtensionMap.class });
68 if (extension != null) {
70 Object ret = extension.invoke(clazz.newInstance(), aaiExtMap);
72 if (ret instanceof Integer) {
75 if (isPreExtension == true) {
76 e = aaiExtMap.getPreExtException();
78 e = aaiExtMap.getPostExtException();
81 boolean failOnError = true;
82 if (isPreExtension == true) {
83 failOnError = aaiExtMap.getPreExtFailOnError();
85 failOnError = aaiExtMap.getPostExtFailOnError();
89 boolean handleException = true;
90 if (isPreExtension == true) {
91 if (aaiExtMap.getPreExtSkipErrorCallback() == true) {
92 handleException = false;
95 if (aaiExtMap.getPostExtSkipErrorCallback() == true) {
96 handleException = false;
99 if (handleException == true) {
100 Method errorCallback = null;
101 if (isPreExtension == true) {
102 errorCallback = aaiExtMap
103 .getPreExtErrorCallback();
105 errorCallback = aaiExtMap
106 .getPostExtErrorCallback();
109 if (errorCallback != null) {
110 errorCallback.invoke(clazz.newInstance(),
113 Method defaultErrorCallbackExtension = clazz
115 defaultErrorCallback,
116 new Class[] { AAIExtensionMap.class });
117 defaultErrorCallbackExtension.invoke(
118 clazz.newInstance(), aaiExtMap);
123 if (failOnError == true && e != null) {
125 } else if (failOnError == false && e != null) { // in this
131 LOGGER.warn("Error while processing extension - " + aaiExtMap.getMessage());
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) {
141 } catch (Exception e) {
142 throw new AAIException("AAI_5105", e);