2 * Copyright (c) 2014, 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package test.mock.util;
10 import com.google.common.collect.LinkedHashMultimap;
11 import java.lang.reflect.InvocationTargetException;
12 import java.lang.reflect.Method;
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;
24 public class NotificationProviderServiceHelper {
26 private static final Logger LOG = LoggerFactory.getLogger(NotificationProviderServiceHelper.class);
28 private NotificationProviderService notifBroker = new NotificationProviderServiceDummyImpl();
30 public NotificationProviderService getNotifBroker() {
34 public void pushDelayedNotification(final Notification notification, int delay) {
35 new Timer().schedule(new TimerTask() {
38 notifBroker.publish(notification);
43 public void pushNotification(final Notification notification) {
44 notifBroker.publish(notification);
47 private static class NotificationListenerExecTuple {
49 org.opendaylight.yangtools.yang.binding.NotificationListener listenerInst;
51 void propagateNotification(Notification notification) {
53 m.invoke(listenerInst, notification);
54 } catch (IllegalAccessException | InvocationTargetException e) {
55 LOG.error("Exception occurred: {} ", e.getMessage(), e);
60 public int hashCode() {
61 return listenerInst.hashCode();
65 public boolean equals(Object obj) {
66 return listenerInst.equals(obj);
71 private static class NotificationProviderServiceDummyImpl implements NotificationProviderService {
72 private LinkedHashMultimap<Class, NotificationListenerExecTuple> listenerRegistry = LinkedHashMultimap.create();
76 public void publish(Notification notification) {
77 Set<NotificationListenerExecTuple> execPack = listenerRegistry.get(notification.getImplementedInterface());
78 for (NotificationListenerExecTuple notificationListenerExecTuple : execPack) {
79 notificationListenerExecTuple.propagateNotification(notification);
84 public void publish(Notification notification, ExecutorService executorService) {
85 throw new IllegalAccessError("publish with executorService not supported");
89 public ListenerRegistration<NotificationInterestListener> registerInterestListener(NotificationInterestListener notificationInterestListener) {
90 throw new IllegalAccessError("registering of interest listener not supported");
94 public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(Class<T> aClass, NotificationListener<T> notificationListener) {
95 throw new IllegalAccessError("registering with class not supported");
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;
107 listeners.add(execPack);