2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.openecomp.mso.asdc.util;
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;
37 import org.openecomp.mso.logger.MsoLogger;
38 import org.onap.sdc.api.notification.INotificationData;
41 public class NotificationLogging implements InvocationHandler {
43 private static Map<Object, List<Method>> objectMethodsToLog = new HashMap<>();
45 protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC);
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...
54 return arg1.invoke(arg0, arg2);
57 public static InvocationHandler getHandler() {
64 private NotificationLogging() {}
66 private static final String[] GETTER_PREFIXES = { "get", "is" };
68 public static String logNotification(INotificationData iNotif) {
73 Class<? extends INotificationData> clazz = iNotif.getClass();
75 Method[] declaredMethods = clazz.getDeclaredMethods();
77 if (declaredMethods == null || declaredMethods.length == 0) {
78 return "EMPTY"; // No declared methods in this class !!!
81 StringBuilder buffer = new StringBuilder("ASDC Notification:");
82 buffer.append(System.lineSeparator());
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()));
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");
99 buffer.append(System.lineSeparator());
103 return buffer.toString();
106 private static boolean isGetter(Method method) {
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)) {
120 // Must not take any input arguments
121 if (method.getParameterTypes().length != 0) {
125 // Must not have return type 'void'
126 if (void.class.equals(method.getReturnType())) {
131 if (!Modifier.isPublic(method.getModifiers())) {
138 private static String testNull(Object object) {
139 if (object == null) {
141 } else if (object instanceof Integer) {
142 return object.toString();
143 } else if (object instanceof String) {
144 return (String) object;
146 return "Type not recognized";
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<>());
158 private static <T> void methodToLog(T methodCall) {
163 public Object invoke(Object proxy, Method method, Object[] args)
165 // TODO Auto-generated method stub