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