1 package test.mock.util;
3 import com.google.common.collect.LinkedHashMultimap;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
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;
17 public class NotificationProviderServiceHelper {
19 private static final Logger LOG = LoggerFactory.getLogger(NotificationProviderServiceHelper.class);
21 private NotificationProviderService notifBroker = new NotificationProviderServiceDummyImpl();
23 public NotificationProviderService getNotifBroker() {
27 public void pushDelayedNotification(final Notification notification, int delay) {
28 new Timer().schedule(new TimerTask() {
31 notifBroker.publish(notification);
36 public void pushNotification(final Notification notification) {
37 notifBroker.publish(notification);
40 private static class NotificationListenerExecTuple {
42 org.opendaylight.yangtools.yang.binding.NotificationListener listenerInst;
44 void propagateNotification(Notification notification) {
46 m.invoke(listenerInst, notification);
47 } catch (IllegalAccessException | InvocationTargetException e) {
48 LOG.error("Exception occurred: {} ", e.getMessage(), e);
53 public int hashCode() {
54 return listenerInst.hashCode();
58 public boolean equals(Object obj) {
59 return listenerInst.equals(obj);
64 private static class NotificationProviderServiceDummyImpl implements NotificationProviderService {
65 private LinkedHashMultimap<Class, NotificationListenerExecTuple> listenerRegistry = LinkedHashMultimap.create();
69 public void publish(Notification notification) {
70 Set<NotificationListenerExecTuple> execPack = listenerRegistry.get(notification.getImplementedInterface());
71 for (NotificationListenerExecTuple notificationListenerExecTuple : execPack) {
72 notificationListenerExecTuple.propagateNotification(notification);
77 public void publish(Notification notification, ExecutorService executorService) {
78 throw new IllegalAccessError("publish with executorService not supported");
82 public ListenerRegistration<NotificationInterestListener> registerInterestListener(NotificationInterestListener notificationInterestListener) {
83 throw new IllegalAccessError("registering of interest listener not supported");
87 public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(Class<T> aClass, NotificationListener<T> notificationListener) {
88 throw new IllegalAccessError("registering with class not supported");
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;
100 listeners.add(execPack);