Update to MD-SAL APIs
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / NotificationBrokerImpl.xtend
1 /*
2  * Copyright (c) 2013 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 org.opendaylight.controller.sal.binding.impl
9
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
18
19 class NotificationBrokerImpl implements NotificationProviderService {
20
21     val Multimap<Class<? extends Notification>, NotificationListener<?>> listeners;
22     val ExecutorService executor;
23
24     new(ExecutorService executor) {
25         listeners = HashMultimap.create()
26         this.executor = executor;
27     }
28
29     override <T extends Notification> addNotificationListener(Class<T> notificationType,
30         NotificationListener<T> listener) {
31         listeners.put(notificationType, listener)
32     }
33
34     override <T extends Notification> removeNotificationListener(Class<T> notificationType,
35         NotificationListener<T> listener) {
36         listeners.remove(notificationType, listener)
37     }
38
39     override notify(Notification notification) {
40         publish(notification)
41     }
42
43     def getNotificationTypes(Notification notification) {
44         notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]
45     }
46
47     @SuppressWarnings("unchecked")
48     def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {
49         listeners.forEach[(it as NotificationListener).onNotification(notification)]
50     }
51
52     override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
53         throw new UnsupportedOperationException("TODO: auto-generated method stub")
54
55     }
56
57     override removeNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
58         throw new UnsupportedOperationException("TODO: auto-generated method stub")
59     }
60
61     override notify(Notification notification, ExecutorService service) {
62         publish(notification)
63     }
64
65     override publish(Notification notification) {
66         notification.notificationTypes.forEach [
67             listeners.get(it as Class<? extends Notification>)?.notifyAll(notification)
68         ]
69     }
70
71     override publish(Notification notification, ExecutorService service) {
72         publish(notification)
73     }
74
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);
79         return reg;
80     }
81
82     override registerNotificationListener(
83         org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
84             
85     }
86     
87     
88     protected def unregisterListener(GenericNotificationRegistration<?> reg) {
89         listeners.remove(reg.type,reg.instance);
90     }
91 }
92 class GenericNotificationRegistration<T extends Notification> implements Registration<NotificationListener<T>> {
93     
94     @Property
95     var NotificationListener<T> instance;
96     
97     @Property
98     val Class<T> type;
99     
100     
101     val NotificationBrokerImpl notificationBroker;
102     
103     public new(Class<T> type, NotificationListener<T> instance,NotificationBrokerImpl broker) {
104         _instance = instance;
105         _type = type;
106         notificationBroker = broker;
107     }
108     
109     override close() {
110         notificationBroker.unregisterListener(this);
111     }
112 }