aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/appc/ApplicationControllerSupport.java
blob: e3ed432dfcd1371ba7815ad0deade83f09876555 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * 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.mso.client.appc;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.springframework.stereotype.Component;

import org.onap.appc.client.lcm.api.LifeCycleManagerStateful;
import org.onap.appc.client.lcm.api.ResponseHandler;
import org.onap.appc.client.lcm.model.Status;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.att.eelf.configuration.EELFLogger.Level;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

@Component
public class ApplicationControllerSupport {

	private static final int ACCEPT_SERIES = 100;
	private static final int ERROR_SERIES = 200;
	private static final int REJECT_SERIES = 300;
	private static final int SUCCESS_SERIES = 400;
	private static final int SUCCESS_STATUS = SUCCESS_SERIES;
	private static final int PARTIAL_SERIES = 500;
	private static final int PARTIAL_SUCCESS_STATUS = PARTIAL_SERIES;
	private static final int PARTIAL_FAILURE_STATUS = PARTIAL_SERIES + 1;

	protected final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
	private String lcmModelPackage = "org.onap.appc.client.lcm.model";

	/**
	 * @param action
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public Object getInput(String action) {
		try {
			return getInputClass(action).newInstance();
		} catch (IllegalAccessException | InstantiationException e) {
			throw new RuntimeException(
					String.format("%s : %s", "Unable to instantiate viable LCM Kit input class for action", action), e);
		}
	}

	/**
	 * @param action
	 * @return
	 * @throws ClassNotFoundException
	 */
	public Method getAPIMethod(String action, LifeCycleManagerStateful lcmStateful, boolean async) {
		Method[] methods = lcmStateful.getClass().getMethods();
		for (Method method : methods) {
			if (method.getName().equalsIgnoreCase(action)) {
				Class<?>[] methodParameterTypes = method.getParameterTypes();
				if (methodParameterTypes.length > 0) {
					if (getInputClass(action).equals(methodParameterTypes[0])) {
						if (async) {
							if (methodParameterTypes.length == 2
									&& ResponseHandler.class.isAssignableFrom(methodParameterTypes[1])) {
								return method;
							}
						} else if (methodParameterTypes.length == 1) {
							return method;
						}
					}
				}
			}
		}
		throw new RuntimeException(String.format("%s : %s, async=%b",
				"Unable to derive viable LCM Kit API method for action", action, async));
	}

	public Status getStatusFromGenericResponse(Object response) {
		Method statusReader = getBeanPropertyMethodFor(response.getClass(), "status", false);
		if (statusReader != null) {
			try {
				return (Status) statusReader.invoke(response);
			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
				auditLogger.log(Level.ERROR, "Unable to obtain status from LCM Kit response", e, e.getMessage());
			}
		}
		return new Status();
	}

	public static StatusCategory getCategoryOf(Status status) {
		int codeSeries = status.getCode() - (status.getCode() % 100);
		switch (codeSeries) {
		case ACCEPT_SERIES:
			return StatusCategory.NORMAL;
		case ERROR_SERIES:
		case REJECT_SERIES:
			return StatusCategory.ERROR;
		case SUCCESS_SERIES:
			return status.getCode() == SUCCESS_STATUS ? StatusCategory.NORMAL : StatusCategory.ERROR;
		case PARTIAL_SERIES:
			switch (status.getCode()) {
			case PARTIAL_SUCCESS_STATUS:
				return StatusCategory.NORMAL;
			case PARTIAL_FAILURE_STATUS:
				return StatusCategory.ERROR;
			default:
				return StatusCategory.WARNING;
			}
		default:
			return StatusCategory.WARNING;
		}
	}

	public static boolean getFinalityOf(Status status) {
		int codeSeries = status.getCode() - (status.getCode() % 100);
		switch (codeSeries) {
		case ACCEPT_SERIES:
		case PARTIAL_SERIES:
			return false;
		case ERROR_SERIES:
		case REJECT_SERIES:
		case SUCCESS_SERIES:
			return true;
		default:
			return true;
		}
	}

	private Method getBeanPropertyMethodFor(Class<?> clazz, String propertyName, boolean isWriter) {
		BeanInfo beanInfo;
		try {
			beanInfo = Introspector.getBeanInfo(clazz, Object.class);
		} catch (IntrospectionException e) {
			throw new RuntimeException(
					String.format("Unable to produce bean property method for class : %s, property : %s, writer=%b",
							clazz.getName(), propertyName, isWriter),
					e);
		}
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
			if (propertyDescriptor.getName().equals(propertyName)) {
				return isWriter ? propertyDescriptor.getWriteMethod() : propertyDescriptor.getReadMethod();
			}
		}
		throw new RuntimeException(
				String.format("Unable to produce bean property method for class : %s, property : %s, writer=%b",
						clazz.getName(), propertyName, isWriter));
	}

	/**
	 * @param action
	 * @return
	 * @throws ClassNotFoundException
	 */
	private Class<?> getInputClass(String action) {
		try {
			return Class.forName(lcmModelPackage + '.' + action + "Input");
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(String.format("%s : %s using package : %s",
					"Unable to identify viable LCM Kit input class for action", action, lcmModelPackage), e);
		}
	}

	public enum StatusCategory {
		NORMAL("normal"), WARNING("warning"), ERROR("error");

		private final String category;

		private StatusCategory(final String category) {
			this.category = category;
		}

		@Override
		public String toString() {
			return category;
		}
	}

	public void logLCMMessage(Object message) {
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.setSerializationInclusion(Include.NON_NULL);
		ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();
		String inputAsJSON;
		try {
			inputAsJSON = writer.writeValueAsString(message);
			auditLogger.log(Level.INFO, "\nLCM Kit input message follows: \n" + inputAsJSON, null);
		} catch (JsonProcessingException e) {
			auditLogger.log(Level.ERROR, "Error in logging LCM Message: ", e, e.getMessage());
		}
	}
}