b9f849e7be2cad6a04148af812bdaa589ffcbcf4
[openflowplugin.git] / applications / statistics-manager / src / test / java / test / mock / util / NotificationProviderServiceHelper.java
1 package test.mock.util;
2
3 import com.google.common.collect.LinkedHashMultimap;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6 import java.util.Set;
7 import java.util.Timer;
8 import java.util.TimerTask;
9 import java.util.concurrent.ExecutorService;
10 import org.opendaylight.controller.sal.binding.api.NotificationListener;
11 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
12 import org.opendaylight.yangtools.concepts.ListenerRegistration;
13 import org.opendaylight.yangtools.yang.binding.Notification;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public class NotificationProviderServiceHelper {
18
19     private static final Logger LOG = LoggerFactory.getLogger(NotificationProviderServiceHelper.class);
20
21     private NotificationProviderService notifBroker = new NotificationProviderServiceDummyImpl();
22
23     public NotificationProviderService getNotifBroker() {
24         return notifBroker;
25     }
26
27     public void pushDelayedNotification(final Notification notification, int delay) {
28         new Timer().schedule(new TimerTask() {
29             @Override
30             public void run() {
31                 notifBroker.publish(notification);
32             }
33         }, delay);
34     }
35
36     public void pushNotification(final Notification notification) {
37         notifBroker.publish(notification);
38     }
39
40     private static class NotificationListenerExecTuple {
41         Method m;
42         org.opendaylight.yangtools.yang.binding.NotificationListener listenerInst;
43
44         void propagateNotification(Notification notification) {
45             try {
46                 m.invoke(listenerInst, notification);
47             } catch (IllegalAccessException | InvocationTargetException e) {
48                 LOG.error("Exception occurred: {} ", e.getMessage(), e);
49             }
50         }
51
52         @Override
53         public int hashCode() {
54             return listenerInst.hashCode();
55         }
56
57         @Override
58         public boolean equals(Object obj) {
59             return listenerInst.equals(obj);
60         }
61
62     }
63
64     private static class NotificationProviderServiceDummyImpl implements NotificationProviderService {
65         private LinkedHashMultimap<Class, NotificationListenerExecTuple> listenerRegistry = LinkedHashMultimap.create();
66
67         @Override
68
69         public void publish(Notification notification) {
70             Set<NotificationListenerExecTuple> execPack = listenerRegistry.get(notification.getImplementedInterface());
71             for (NotificationListenerExecTuple notificationListenerExecTuple : execPack) {
72                 notificationListenerExecTuple.propagateNotification(notification);
73             }
74         }
75
76         @Override
77         public void publish(Notification notification, ExecutorService executorService) {
78             throw new IllegalAccessError("publish with executorService not supported");
79         }
80
81         @Override
82         public ListenerRegistration<NotificationInterestListener> registerInterestListener(NotificationInterestListener notificationInterestListener) {
83             throw new IllegalAccessError("registering of interest listener not supported");
84         }
85
86         @Override
87         public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(Class<T> aClass, NotificationListener<T> notificationListener) {
88             throw new IllegalAccessError("registering with class not supported");
89         }
90
91         @Override
92         public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener notificationListener) {
93             for (Method m : notificationListener.getClass().getMethods()) {
94                 if (m.getName().startsWith("on") && m.getParameterTypes().length == 1) {
95                     Class<?> key = m.getParameterTypes()[0];
96                     Set<NotificationListenerExecTuple> listeners = listenerRegistry.get(key);
97                     NotificationListenerExecTuple execPack = new NotificationListenerExecTuple();
98                     execPack.listenerInst = notificationListener;
99                     execPack.m = m;
100                     listeners.add(execPack);
101                 }
102             }
103             return null;
104         }
105     }
106 }