c55643e6255c31b05cbc848bb2d6290462982d7a
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceNotificationService.java
1 /*
2  * Copyright (c) 2015 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.netconf.sal.connect.netconf.sal;
9
10 import com.google.common.collect.HashMultimap;
11 import com.google.common.collect.Lists;
12 import com.google.common.collect.Multimap;
13 import java.util.Collection;
14 import org.opendaylight.mdsal.dom.api.DOMNotification;
15 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
16 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
17 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class NetconfDeviceNotificationService implements DOMNotificationService {
24
25     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceNotificationService.class);
26
27     private final Multimap<Absolute, DOMNotificationListener> listeners = HashMultimap.create();
28
29     // Notification publish is very simple and hijacks the thread of the caller
30     // TODO shouldnt we reuse the implementation for notification router from sal-broker-impl ?
31     @SuppressWarnings("checkstyle:IllegalCatch")
32     public synchronized void publishNotification(final DOMNotification notification) {
33         for (final DOMNotificationListener domNotificationListener : listeners.get(notification.getType())) {
34             try {
35                 domNotificationListener.onNotification(notification);
36             } catch (final Exception e) {
37                 LOG.warn("Listener {} threw an uncaught exception during processing notification {}",
38                         domNotificationListener, notification, e);
39             }
40         }
41     }
42
43     @Override
44     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
45             final T listener, final Collection<Absolute> types) {
46         for (final Absolute type : types) {
47             listeners.put(type, listener);
48         }
49
50         return new AbstractListenerRegistration<>(listener) {
51             @Override
52             protected void removeRegistration() {
53                 for (final Absolute type : types) {
54                     listeners.remove(type, listener);
55                 }
56             }
57         };
58     }
59
60     @Override
61     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
62             final T listener, final Absolute... types) {
63         return registerNotificationListener(listener, Lists.newArrayList(types));
64     }
65 }