2 * Copyright (c) 2013 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 org.opendaylight.controller.sal.binding.impl
10 import org.opendaylight.controller.sal.binding.api.NotificationProviderService
11 import org.opendaylight.yangtools.yang.binding.Notification
12 import com.google.common.collect.Multimap
13 import org.opendaylight.controller.sal.binding.api.NotificationListener
14 import com.google.common.collect.HashMultimap
15 import java.util.concurrent.ExecutorService
16 import java.util.Collection
17 import org.opendaylight.yangtools.concepts.Registration
19 class NotificationBrokerImpl implements NotificationProviderService {
21 val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;
22 val ExecutorService executor;
24 new(ExecutorService executor) {
25 listeners = HashMultimap.create()
26 this.executor = executor;
29 override <T extends Notification> addNotificationListener(Class<T> notificationType,
30 NotificationListener<T> listener) {
31 listeners.put(notificationType, listener)
34 override <T extends Notification> removeNotificationListener(Class<T> notificationType,
35 NotificationListener<T> listener) {
36 listeners.remove(notificationType, listener)
39 override notify(Notification notification) {
43 def getNotificationTypes(Notification notification) {
44 notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]
47 @SuppressWarnings("unchecked")
48 def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {
49 listeners.forEach[(it as NotificationListener).onNotification(notification)]
52 override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
53 throw new UnsupportedOperationException("TODO: auto-generated method stub")
57 override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
58 throw new UnsupportedOperationException("TODO: auto-generated method stub")
61 override notify(Notification notification, ExecutorService service) {
65 override publish(Notification notification) {
66 notification.notificationTypes.forEach [
67 listeners.get(it as Class<? extends Notification>)?.notifyAll(notification)
71 override publish(Notification notification, ExecutorService service) {
75 override <T extends Notification> registerNotificationListener(Class<T> notificationType,
76 NotificationListener<T> listener) {
77 val reg = new GenericNotificationRegistration<T>(notificationType,listener,this);
78 listeners.put(notificationType,listener);
82 override registerNotificationListener(
83 org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
88 protected def unregisterListener(GenericNotificationRegistration<?> reg) {
89 listeners.remove(reg.type,reg.instance);
92 class GenericNotificationRegistration<T extends Notification> implements Registration<NotificationListener<T>> {
95 var NotificationListener<T> instance;
101 val NotificationBrokerImpl notificationBroker;
103 public new(Class<T> type, NotificationListener<T> instance,NotificationBrokerImpl broker) {
104 _instance = instance;
106 notificationBroker = broker;
110 notificationBroker.unregisterListener(this);