2 * Copyright (c) 2015 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
9 package org.opendaylight.netconf.sal.connect.netconf.sal;
11 import com.google.common.collect.HashMultimap;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Multimap;
14 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
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;
24 public class NetconfDeviceNotificationService implements DOMNotificationService {
26 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceNotificationService.class);
28 private final Multimap<SchemaPath, DOMNotificationListener> listeners = HashMultimap.create();
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 public synchronized void publishNotification(final DOMNotification notification) {
33 for (final DOMNotificationListener domNotificationListener : listeners.get(notification.getType())) {
35 domNotificationListener.onNotification(notification);
36 } catch (final Exception e) {
37 LOG.warn("Listener {} threw an uncaught exception during processing notification {}",
38 domNotificationListener, notification, e);
44 public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(@Nonnull final T listener, @Nonnull final Collection<SchemaPath> types) {
45 for (final SchemaPath type : types) {
46 listeners.put(type, listener);
49 return new ListenerRegistration<T>() {
52 for (final SchemaPath type : types) {
53 listeners.remove(type, listener);
58 public T getInstance() {
65 public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(@Nonnull final T listener, final SchemaPath... types) {
66 return registerNotificationListener(listener, Lists.newArrayList(types));