Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / client / appc / ApplicationControllerSupport.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.mso.client.appc;
22
23 import java.beans.BeanInfo;
24 import java.beans.IntrospectionException;
25 import java.beans.Introspector;
26 import java.beans.PropertyDescriptor;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29
30 import org.springframework.stereotype.Component;
31
32 import org.onap.appc.client.lcm.api.LifeCycleManagerStateful;
33 import org.onap.appc.client.lcm.api.ResponseHandler;
34 import org.onap.appc.client.lcm.model.Status;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.att.eelf.configuration.EELFLogger.Level;
38 import com.fasterxml.jackson.annotation.JsonInclude.Include;
39 import com.fasterxml.jackson.core.JsonProcessingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.databind.ObjectWriter;
42
43 @Component
44 public class ApplicationControllerSupport {
45
46         private static final int ACCEPT_SERIES = 100;
47         private static final int ERROR_SERIES = 200;
48         private static final int REJECT_SERIES = 300;
49         private static final int SUCCESS_SERIES = 400;
50         private static final int SUCCESS_STATUS = SUCCESS_SERIES;
51         private static final int PARTIAL_SERIES = 500;
52         private static final int PARTIAL_SUCCESS_STATUS = PARTIAL_SERIES;
53         private static final int PARTIAL_FAILURE_STATUS = PARTIAL_SERIES + 1;
54
55         protected final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
56         private String lcmModelPackage = "org.onap.appc.client.lcm.model";
57
58         /**
59          * @param action
60          * @return
61          * @throws ClassNotFoundException
62          * @throws InstantiationException
63          * @throws IllegalAccessException
64          */
65         public Object getInput(String action) {
66                 try {
67                         return getInputClass(action).newInstance();
68                 } catch (IllegalAccessException | InstantiationException e) {
69                         throw new RuntimeException(
70                                         String.format("%s : %s", "Unable to instantiate viable LCM Kit input class for action", action), e);
71                 }
72         }
73
74         /**
75          * @param action
76          * @return
77          * @throws ClassNotFoundException
78          */
79         public Method getAPIMethod(String action, LifeCycleManagerStateful lcmStateful, boolean async) {
80                 Method[] methods = lcmStateful.getClass().getMethods();
81                 for (Method method : methods) {
82                         if (method.getName().equalsIgnoreCase(action)) {
83                                 Class<?>[] methodParameterTypes = method.getParameterTypes();
84                                 if (methodParameterTypes.length > 0) {
85                                         if (getInputClass(action).equals(methodParameterTypes[0])) {
86                                                 if (async) {
87                                                         if (methodParameterTypes.length == 2
88                                                                         && ResponseHandler.class.isAssignableFrom(methodParameterTypes[1])) {
89                                                                 return method;
90                                                         }
91                                                 } else if (methodParameterTypes.length == 1) {
92                                                         return method;
93                                                 }
94                                         }
95                                 }
96                         }
97                 }
98                 throw new RuntimeException(String.format("%s : %s, async=%b",
99                                 "Unable to derive viable LCM Kit API method for action", action, async));
100         }
101
102         public Status getStatusFromGenericResponse(Object response) {
103                 Method statusReader = getBeanPropertyMethodFor(response.getClass(), "status", false);
104                 if (statusReader != null) {
105                         try {
106                                 return (Status) statusReader.invoke(response);
107                         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
108                                 auditLogger.log(Level.ERROR, "Unable to obtain status from LCM Kit response", e, e.getMessage());
109                         }
110                 }
111                 return new Status();
112         }
113
114         public static StatusCategory getCategoryOf(Status status) {
115                 int codeSeries = status.getCode() - (status.getCode() % 100);
116                 switch (codeSeries) {
117                 case ACCEPT_SERIES:
118                         return StatusCategory.NORMAL;
119                 case ERROR_SERIES:
120                 case REJECT_SERIES:
121                         return StatusCategory.ERROR;
122                 case SUCCESS_SERIES:
123                         return status.getCode() == SUCCESS_STATUS ? StatusCategory.NORMAL : StatusCategory.ERROR;
124                 case PARTIAL_SERIES:
125                         switch (status.getCode()) {
126                         case PARTIAL_SUCCESS_STATUS:
127                                 return StatusCategory.NORMAL;
128                         case PARTIAL_FAILURE_STATUS:
129                                 return StatusCategory.ERROR;
130                         default:
131                                 return StatusCategory.WARNING;
132                         }
133                 default:
134                         return StatusCategory.WARNING;
135                 }
136         }
137
138         public static boolean getFinalityOf(Status status) {
139                 int codeSeries = status.getCode() - (status.getCode() % 100);
140                 switch (codeSeries) {
141                 case ACCEPT_SERIES:
142                 case PARTIAL_SERIES:
143                         return false;
144                 case ERROR_SERIES:
145                 case REJECT_SERIES:
146                 case SUCCESS_SERIES:
147                         return true;
148                 default:
149                         return true;
150                 }
151         }
152
153         private Method getBeanPropertyMethodFor(Class<?> clazz, String propertyName, boolean isWriter) {
154                 BeanInfo beanInfo;
155                 try {
156                         beanInfo = Introspector.getBeanInfo(clazz, Object.class);
157                 } catch (IntrospectionException e) {
158                         throw new RuntimeException(
159                                         String.format("Unable to produce bean property method for class : %s, property : %s, writer=%b",
160                                                         clazz.getName(), propertyName, isWriter),
161                                         e);
162                 }
163                 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
164                 for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
165                         if (propertyDescriptor.getName().equals(propertyName)) {
166                                 return isWriter ? propertyDescriptor.getWriteMethod() : propertyDescriptor.getReadMethod();
167                         }
168                 }
169                 throw new RuntimeException(
170                                 String.format("Unable to produce bean property method for class : %s, property : %s, writer=%b",
171                                                 clazz.getName(), propertyName, isWriter));
172         }
173
174         /**
175          * @param action
176          * @return
177          * @throws ClassNotFoundException
178          */
179         private Class<?> getInputClass(String action) {
180                 try {
181                         return Class.forName(lcmModelPackage + '.' + action + "Input");
182                 } catch (ClassNotFoundException e) {
183                         throw new RuntimeException(String.format("%s : %s using package : %s",
184                                         "Unable to identify viable LCM Kit input class for action", action, lcmModelPackage), e);
185                 }
186         }
187
188         public enum StatusCategory {
189                 NORMAL("normal"), WARNING("warning"), ERROR("error");
190
191                 private final String category;
192
193                 private StatusCategory(final String category) {
194                         this.category = category;
195                 }
196
197                 @Override
198                 public String toString() {
199                         return category;
200                 }
201         }
202
203         public void logLCMMessage(Object message) {
204                 ObjectMapper objectMapper = new ObjectMapper();
205                 objectMapper.setSerializationInclusion(Include.NON_NULL);
206                 ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();
207                 String inputAsJSON;
208                 try {
209                         inputAsJSON = writer.writeValueAsString(message);
210                         auditLogger.log(Level.INFO, "\nLCM Kit input message follows: \n" + inputAsJSON, null);
211                 } catch (JsonProcessingException e) {
212                         auditLogger.log(Level.ERROR, "Error in logging LCM Message: ", e, e.getMessage());
213                 }
214         }
215 }