Bump upstream versions
[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 java.util.Map;
15 import org.opendaylight.mdsal.dom.api.DOMNotification;
16 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
17 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
18 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class NetconfDeviceNotificationService implements DOMNotificationService {
26
27     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceNotificationService.class);
28
29     private final Multimap<Absolute, DOMNotificationListener> listeners = HashMultimap.create();
30
31     // Notification publish is very simple and hijacks the thread of the caller
32     // TODO shouldnt we reuse the implementation for notification router from sal-broker-impl ?
33     @SuppressWarnings("checkstyle:IllegalCatch")
34     public synchronized void publishNotification(final DOMNotification notification) {
35         for (final DOMNotificationListener domNotificationListener : listeners.get(notification.getType())) {
36             try {
37                 domNotificationListener.onNotification(notification);
38             } catch (final Exception e) {
39                 LOG.warn("Listener {} threw an uncaught exception during processing notification {}",
40                         domNotificationListener, notification, e);
41             }
42         }
43     }
44
45     @Override
46     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
47             final T listener, final Collection<Absolute> types) {
48         for (final Absolute type : types) {
49             listeners.put(type, listener);
50         }
51
52         return new AbstractListenerRegistration<>(listener) {
53             @Override
54             protected void removeRegistration() {
55                 for (final Absolute type : types) {
56                     listeners.remove(type, listener);
57                 }
58             }
59         };
60     }
61
62     @Override
63     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
64             final T listener, final Absolute... types) {
65         return registerNotificationListener(listener, Lists.newArrayList(types));
66     }
67
68     @Override
69     public synchronized Registration registerNotificationListeners(
70             final Map<Absolute, DOMNotificationListener> typeToListener) {
71         // FIXME: implement this
72         throw new UnsupportedOperationException();
73     }
74 }