Replaced all tabs with spaces in java and pom.xml
[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 import org.onap.sdc.api.notification.INotificationData;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43 public class NotificationLogging implements InvocationHandler {
44
45     private static Map<Object, List<Method>> objectMethodsToLog = new HashMap<>();
46
47     protected static final Logger logger = LoggerFactory.getLogger(NotificationLogging.class);
48
49     private static InvocationHandler handler = (arg0, arg1, arg2) -> {
50         List<Method> methods = objectMethodsToLog.get(arg0);
51         if ((methods == null) || (methods.isEmpty())) {
52             // Do nothing for now...
53             return null;
54         }
55         methods.add(arg1);
56         return arg1.invoke(arg0, arg2);
57     };
58
59     public static InvocationHandler getHandler() {
60         return handler;
61     }
62
63     /**
64      * 
65      */
66     private NotificationLogging() {}
67
68     private static final String[] GETTER_PREFIXES = {"get", "is"};
69
70     public static String logNotification(INotificationData iNotif) {
71         if (iNotif == null) {
72             return "NULL";
73         }
74
75         Class<? extends INotificationData> clazz = iNotif.getClass();
76
77         Method[] declaredMethods = clazz.getDeclaredMethods();
78
79         if (declaredMethods == null || declaredMethods.length == 0) {
80             return "EMPTY"; // No declared methods in this class !!!
81         }
82
83         StringBuilder buffer = new StringBuilder("ASDC Notification:");
84         buffer.append(System.lineSeparator());
85
86         for (Method m : declaredMethods) {
87             if ((m != null) && isGetter(m)) {
88                 for (String prefix : GETTER_PREFIXES) {
89                     if (m.getName().startsWith(prefix)) {
90                         buffer.append(m.getName().substring(prefix.length()));
91                         break;
92                     }
93                 }
94                 try {
95                     buffer.append(testNull(m.invoke(iNotif, (Object[]) null)));
96                 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
97                     logger.debug("Exception", e);
98                     buffer.append("UNREADABLE");
99                 }
100                 buffer.append(System.lineSeparator());
101             }
102         }
103
104         return buffer.toString();
105     }
106
107     private static boolean isGetter(Method method) {
108
109         // Must start with a valid (and known) prefix
110         boolean prefixFound = false;
111         for (String prefix : GETTER_PREFIXES) {
112             if (method.getName().startsWith(prefix)) {
113                 prefixFound = true;
114                 break;
115             }
116         }
117         if (!prefixFound) {
118             return false;
119         }
120
121         // Must not take any input arguments
122         if (method.getParameterTypes().length != 0) {
123             return false;
124         }
125
126         // Must not have return type 'void'
127         if (void.class.equals(method.getReturnType())) {
128             return false;
129         }
130
131         // Must be public
132         if (!Modifier.isPublic(method.getModifiers())) {
133             return false;
134         }
135
136         return true;
137     }
138
139     private static String testNull(Object object) {
140         if (object == null) {
141             return "NULL";
142         } else if (object instanceof Integer) {
143             return object.toString();
144         } else if (object instanceof String) {
145             return (String) object;
146         } else {
147             return "Type not recognized";
148         }
149     }
150
151     private static void registerForLog(INotificationData objectToLog) {
152         INotificationData proxy = (INotificationData) Proxy.newProxyInstance(INotificationData.class.getClassLoader(),
153                 new Class[] {INotificationData.class}, NotificationLogging.getHandler());
154         objectMethodsToLog.put(proxy, new ArrayList<>());
155     }
156
157     private static <T> void methodToLog(T methodCall) {
158         //
159     }
160
161     @Override
162     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
163         // TODO Auto-generated method stub
164         return null;
165     }
166
167 }