aboutsummaryrefslogtreecommitdiffstats
path: root/ajsc-aai/src/main/java/org/openecomp/aai/extensions/ExtensionController.java
blob: 4bcd307afd1b707375ea20d2c98f6dc1b5f087be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*-
 * ============LICENSE_START=======================================================
 * org.openecomp.aai
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.aai.extensions;

import java.lang.reflect.Method;

import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.logging.AAILogger;
import org.openecomp.aai.logging.LogLine;
import org.openecomp.aai.util.AAIConfig;

public class ExtensionController {

	/**
	 * Run extension.
	 *
	 * @param apiVersion the api version
	 * @param namespace the namespace
	 * @param resourceName the resource name
	 * @param methodName the method name
	 * @param aaiExtMap the aai ext map
	 * @param isPreExtension the is pre extension
	 * @throws AAIException the AAI exception
	 */
	public void runExtension(String apiVersion, String namespace,
			String resourceName, String methodName, AAIExtensionMap aaiExtMap,
			boolean isPreExtension) throws AAIException {
		String fromAppId = aaiExtMap.getFromAppId();
		String transId = aaiExtMap.getTransId();
		String extensionClassName = "org.openecomp.aai.extensions."
				+ apiVersion.toLowerCase() + "." + namespace + "."
				+ resourceName + "Extension";
		String defaultErrorCallback = resourceName + "ExtensionErrorCallback";
		LogLine eLogline = new LogLine();
		AAILogger aaiLogger = aaiExtMap.getAaiLogger();

		String configOption = "aai.extensions." + apiVersion.toLowerCase()
				+ "." + namespace.toLowerCase() + "."
				+ resourceName.toLowerCase() + ".enabled";
		
		try {
			String extensionEnabled = AAIConfig.get(configOption, "true");
			if (extensionEnabled.equalsIgnoreCase("false")) {
				return;
			}
			Class<?> clazz = Class.forName(extensionClassName);

			Method extension = clazz.getMethod(methodName,
					new Class[] { AAIExtensionMap.class });
			if (extension != null) {
				
				eLogline.init("runExtension", transId, fromAppId, methodName);
				
				Object ret = extension.invoke(clazz.newInstance(), aaiExtMap);
				// reset
				if (ret instanceof Integer) {
					//aaiLogger.debug(logline, methodName + "Returned "
					//		+ (int) ret + " " + aaiExtMap.getMessage());

					Exception e = null;
					if (isPreExtension == true) {
						e = aaiExtMap.getPreExtException();
					} else {
						e = aaiExtMap.getPostExtException();
					}

					boolean failOnError = true;
					if (isPreExtension == true) {
						failOnError = aaiExtMap.getPreExtFailOnError();
					} else {
						failOnError = aaiExtMap.getPostExtFailOnError();
					}

					if (e != null) {
						boolean handleException = true;
						if (isPreExtension == true) {
							if (aaiExtMap.getPreExtSkipErrorCallback() == true) { 
								handleException = false;
							}
						} else {
							if (aaiExtMap.getPostExtSkipErrorCallback() == true) { 
								handleException = false;
							}
						}
						if (handleException == true) {
							Method errorCallback = null;
							if (isPreExtension == true) {
								errorCallback = aaiExtMap
										.getPreExtErrorCallback();
							} else {
								errorCallback = aaiExtMap
										.getPostExtErrorCallback();
							}

							if (errorCallback != null) {
								//aaiLogger.debug(logline,
										//"Calling custom error callback: "
									//			+ errorCallback.getName());
								errorCallback.invoke(clazz.newInstance(),
										aaiExtMap);
							} else {
								Method defaultErrorCallbackExtension = clazz
										.getMethod(
												defaultErrorCallback,
												new Class[] { AAIExtensionMap.class });
								//aaiLogger.debug(
										//logline,
										//"Calling default error callback: "
											//	+ defaultErrorCallbackExtension
											//			.getName());
								defaultErrorCallbackExtension.invoke(
										clazz.newInstance(), aaiExtMap);
							}
						}
					}

					if (failOnError == true && e != null) {
						throw e;
					} else if (failOnError == false && e != null) { // in this
																	// case, we
																	// just note
																	// the error
																	// without
																	// stopping
						eLogline.add(methodName + " Message",
								aaiExtMap.getMessage());
						aaiLogger.info(eLogline, true, "0");
					} else { 
						aaiLogger.info(eLogline, true, "0");
					}
				}
			}
		} catch (ClassNotFoundException ex) {
			// do nothing, this is normal
			//aaiLogger.debug(logline, "Extension class not found in "
					//+ extensionClassName + ", skipping " + methodName + ".");
		} catch (NoSuchMethodException e) {
			// The extension class might exist but there is not a matching
			// method in the extension class
			//aaiLogger.debug(logline, methodName + " not found in " + extensionClassName + 
					//". This is likely normal, this message is for devs to debug extensions.");
		} catch (AAIException e) {
			aaiLogger.info(eLogline, false, e.getErrorObject().getErrorCodeString());
			throw e;
		} catch (Exception e) {
			aaiLogger.info(eLogline, false, "AAI_5105");
			throw new AAIException("AAI_5105", e);
		} 
	}
}