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.dom.broker.impl;
10 import java.util.Collection;
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;
21 import com.google.common.collect.HashMultimap;
22 import com.google.common.collect.Multimap;
23 import com.google.common.collect.Multimaps;
25 public class NotificationRouterImpl implements NotificationRouter {
26 private static Logger log = LoggerFactory.getLogger(NotificationRouterImpl.class);
28 private final Multimap<QName, MyListenerRegistration> listeners = Multimaps.synchronizedSetMultimap(HashMultimap.<QName, MyListenerRegistration>create());
29 // private Registration<NotificationListener> defaultListener;
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);
36 if ((toNotify == null) || toNotify.isEmpty()) {
37 log.debug("No listener registered for handling of notification {}", type);
41 for (MyListenerRegistration listener : toNotify) {
43 // FIXME: ensure that notification is immutable
44 listener.getInstance().onNotification(notification);
45 } catch (Exception e) {
46 log.error("Uncaught exception in NotificationListener", e);
52 public void publish(CompositeNode notification) {
53 sendNotification(notification);
57 public ListenerRegistration<NotificationListener> addNotificationListener(QName notification, NotificationListener listener) {
58 MyListenerRegistration ret = new MyListenerRegistration(notification, listener);
59 listeners.put(notification, ret);
63 private class MyListenerRegistration extends AbstractListenerRegistration<NotificationListener> {
67 public MyListenerRegistration(QName type, NotificationListener instance) {
73 protected void removeRegistration() {
74 listeners.remove(type, this);