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