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