Initial OpenECOMP MSO commit
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / util / NotificationLogging.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
22 package org.openecomp.mso.asdc.util;
23
24
25
26 import java.lang.reflect.InvocationHandler;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29 import java.lang.reflect.Modifier;
30 import java.lang.reflect.Proxy;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import org.openecomp.sdc.api.notification.INotificationData;
37
38
39 public class NotificationLogging implements InvocationHandler {
40         
41         private static Map<Object, List<Method>> objectMethodsToLog = new HashMap<>();
42
43         private static InvocationHandler handler = new InvocationHandler() {
44                 @Override
45                 public Object invoke(Object arg0, Method arg1, Object[] arg2)
46                                 throws Throwable {
47                         List<Method> methods = objectMethodsToLog.get(arg0);
48                         if ((methods == null) || (methods.isEmpty())) {
49                                 // Do nothing for now...
50                                 return null;
51                         }
52                         methods.add(arg1);
53                         return arg1.invoke(arg0, arg2);
54                 }
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                 StringBuffer buffer = new StringBuffer("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                                         buffer.append("UNREADABLE");
97                                 }
98                                 buffer.append(System.lineSeparator());
99                         }
100                 }
101                 
102                 return buffer.toString();
103         }
104         
105         private static final boolean isGetter(Method method) {
106
107                 // Must start with a valid (and known) prefix
108                 boolean prefixFound = false;
109                 for (String prefix : GETTER_PREFIXES) {
110                         if (method.getName().startsWith(prefix)) {
111                                 prefixFound = true;
112                                 break;
113                         }
114                 }
115                 if (!prefixFound) {
116                         return false;
117                 }
118
119                 // Must not take any input arguments
120                 if (method.getParameterTypes().length != 0) {
121                         return false;  
122                 }
123                 
124                 // Must not have return type 'void'
125                 if (void.class.equals(method.getReturnType())) {
126                         return false;
127                 }
128                 
129                 // Must be public
130                 if (!Modifier.isPublic(method.getModifiers())) {
131                         return false;
132                 }
133                 
134                 return true;
135         }
136         
137         private static String testNull(Object object) {
138                 if (object == null) {
139                         return "NULL";
140                 } else if (object instanceof Integer) {
141                         return object.toString();
142                 } else if (object instanceof String) {
143                         return (String) object;
144                 } else {
145                         return "Type not recognized";
146                 }
147         }
148         
149         private static void registerForLog(INotificationData objectToLog) {
150                 INotificationData proxy = (INotificationData) Proxy.newProxyInstance(
151                                 INotificationData.class.getClassLoader(),
152                                 new Class[] { INotificationData.class },
153                                 NotificationLogging.getHandler());
154                 objectMethodsToLog.put(proxy, new ArrayList<Method>());
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)
163                         throws Throwable {
164                 // TODO Auto-generated method stub
165                 return null;
166         }
167
168 }