Fix modules Restconf call for mounted devices
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / impl / NotificationRouterImpl.java
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.dom.broker.impl;
9
10 import java.util.Collection;
11
12 import org.opendaylight.controller.sal.core.api.notify.NotificationListener;
13 import org.opendaylight.controller.sal.dom.broker.spi.NotificationRouter;
14 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
15 import org.opendaylight.yangtools.concepts.ListenerRegistration;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.common.collect.HashMultimap;
22 import com.google.common.collect.Multimap;
23 import com.google.common.collect.Multimaps;
24
25 public class NotificationRouterImpl implements NotificationRouter {
26     private static Logger log = LoggerFactory.getLogger(NotificationRouterImpl.class);
27
28     private final Multimap<QName, MyListenerRegistration> listeners = Multimaps.synchronizedSetMultimap(HashMultimap.<QName, MyListenerRegistration>create());
29 //    private Registration<NotificationListener> defaultListener;
30
31     private void sendNotification(CompositeNode notification) {
32         final QName type = notification.getNodeType();
33         final Collection<MyListenerRegistration> toNotify = listeners.get(type);
34         log.trace("Publishing notification " + type);
35
36         if ((toNotify == null) || toNotify.isEmpty()) {
37             log.debug("No listener registered for handling of notification {}", type);
38             return;
39         }
40
41         for (MyListenerRegistration listener : toNotify) {
42             try {
43                 // FIXME: ensure that notification is immutable
44                 listener.getInstance().onNotification(notification);
45             } catch (Exception e) {
46                 log.error("Uncaught exception in NotificationListener", e);
47             }
48         }
49     }
50
51     @Override
52     public void publish(CompositeNode notification) {
53         sendNotification(notification);
54     }
55
56     @Override
57     public ListenerRegistration<NotificationListener> addNotificationListener(QName notification, NotificationListener listener) {
58         MyListenerRegistration ret = new MyListenerRegistration(notification, listener);
59         listeners.put(notification, ret);
60         return ret;
61     }
62
63     private class MyListenerRegistration extends AbstractListenerRegistration<NotificationListener> {
64
65         final QName type;
66
67         public MyListenerRegistration(QName type, NotificationListener instance) {
68             super(instance);
69             this.type = type;
70         }
71
72         @Override
73         protected void removeRegistration() {
74             listeners.remove(type, this);
75         }
76     }
77 }