Migrate netconf to MD-SAL APIs
[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 javax.annotation.Nonnull;
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.yang.model.api.SchemaPath;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class NetconfDeviceNotificationService implements DOMNotificationService {
25
26     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceNotificationService.class);
27
28     private final Multimap<SchemaPath, DOMNotificationListener> listeners = HashMultimap.create();
29
30     // Notification publish is very simple and hijacks the thread of the caller
31     // TODO shouldnt we reuse the implementation for notification router from sal-broker-impl ?
32     @SuppressWarnings("checkstyle:IllegalCatch")
33     public synchronized void publishNotification(final DOMNotification notification) {
34         for (final DOMNotificationListener domNotificationListener : listeners.get(notification.getType())) {
35             try {
36                 domNotificationListener.onNotification(notification);
37             } catch (final Exception e) {
38                 LOG.warn("Listener {} threw an uncaught exception during processing notification {}",
39                         domNotificationListener, notification, e);
40             }
41         }
42     }
43
44     @Override
45     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
46             @Nonnull final T listener, @Nonnull final Collection<SchemaPath> types) {
47         for (final SchemaPath type : types) {
48             listeners.put(type, listener);
49         }
50
51         return new AbstractListenerRegistration<T>(listener) {
52             @Override
53             protected void removeRegistration() {
54                 for (final SchemaPath type : types) {
55                     listeners.remove(type, listener);
56                 }
57             }
58         };
59     }
60
61     @Override
62     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
63             @Nonnull final T listener, final SchemaPath... types) {
64         return registerNotificationListener(listener, Lists.newArrayList(types));
65     }
66 }