Merge "Reorder modifiers"
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / util / NotificationLogging.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22
23 package org.openecomp.mso.asdc.util;
24
25
26
27 import java.lang.reflect.InvocationHandler;
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Method;
30 import java.lang.reflect.Modifier;
31 import java.lang.reflect.Proxy;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 import org.openecomp.mso.logger.MsoLogger;
38 import org.onap.sdc.api.notification.INotificationData;
39
40
41 public class NotificationLogging implements InvocationHandler {
42         
43         private static Map<Object, List<Method>> objectMethodsToLog = new HashMap<>();
44
45         protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC);
46
47         private static InvocationHandler handler = (arg0, arg1, arg2) -> {
48                 List<Method> methods = objectMethodsToLog.get(arg0);
49                 if ((methods == null) || (methods.isEmpty())) {
50                         // Do nothing for now...
51                         return null;
52                 }
53                 methods.add(arg1);
54                 return arg1.invoke(arg0, arg2);
55         };
56         
57         public static InvocationHandler getHandler() {
58                 return handler;
59         }
60         
61         /**
62          * 
63          */
64         private NotificationLogging() {}
65         
66         private static final String[] GETTER_PREFIXES = { "get", "is" };
67         
68         public static String logNotification(INotificationData iNotif) {
69                 if (iNotif == null) {
70                         return "NULL";
71                 }
72
73                 Class<? extends INotificationData> clazz = iNotif.getClass();
74                 
75                 Method[] declaredMethods = clazz.getDeclaredMethods();
76                 
77                 if (declaredMethods == null || declaredMethods.length == 0) {
78                         return "EMPTY"; // No declared methods in this class !!!
79                 }
80                 
81                 StringBuilder buffer = new StringBuilder("ASDC Notification:");
82                 buffer.append(System.lineSeparator());
83                 
84                 for (Method m : declaredMethods) {
85                         if ((m != null) && isGetter(m)) {
86                                 for (String prefix : GETTER_PREFIXES) {
87                                         if (m.getName().startsWith(prefix)) {
88                                                 buffer.append(m.getName().substring(prefix.length()));
89                                                 break;
90                                         }
91                                 }
92                                 try {
93                                         buffer.append(testNull(m.invoke(iNotif, (Object[])null)));
94                                 } catch (IllegalAccessException | IllegalArgumentException
95                                                 | InvocationTargetException e) {
96                                         LOGGER.debug("Exception :"+e);
97                                         buffer.append("UNREADABLE");
98                                 }
99                                 buffer.append(System.lineSeparator());
100                         }
101                 }
102                 
103                 return buffer.toString();
104         }
105         
106         private static boolean isGetter(Method method) {
107
108                 // Must start with a valid (and known) prefix
109                 boolean prefixFound = false;
110                 for (String prefix : GETTER_PREFIXES) {
111                         if (method.getName().startsWith(prefix)) {
112                                 prefixFound = true;
113                                 break;
114                         }
115                 }
116                 if (!prefixFound) {
117                         return false;
118                 }
119
120                 // Must not take any input arguments
121                 if (method.getParameterTypes().length != 0) {
122                         return false;  
123                 }
124                 
125                 // Must not have return type 'void'
126                 if (void.class.equals(method.getReturnType())) {
127                         return false;
128                 }
129                 
130                 // Must be public
131                 if (!Modifier.isPublic(method.getModifiers())) {
132                         return false;
133                 }
134                 
135                 return true;
136         }
137         
138         private static String testNull(Object object) {
139                 if (object == null) {
140                         return "NULL";
141                 } else if (object instanceof Integer) {
142                         return object.toString();
143                 } else if (object instanceof String) {
144                         return (String) object;
145                 } else {
146                         return "Type not recognized";
147                 }
148         }
149         
150         private static void registerForLog(INotificationData objectToLog) {
151                 INotificationData proxy = (INotificationData) Proxy.newProxyInstance(
152                                 INotificationData.class.getClassLoader(),
153                                 new Class[] { INotificationData.class },
154                                 NotificationLogging.getHandler());
155                 objectMethodsToLog.put(proxy, new ArrayList<>());
156         }
157         
158         private static <T> void methodToLog(T methodCall) {
159                 //
160         }
161
162         @Override
163         public Object invoke(Object proxy, Method method, Object[] args)
164                         throws Throwable {
165                 // TODO Auto-generated method stub
166                 return null;
167         }
168
169 }