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
8 package org.opendaylight.controller.md.sal.binding.compat;
10 import com.google.common.collect.Sets;
11 import java.util.Collections;
12 import java.util.Iterator;
14 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationPublishServiceAdapter;
15 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationServiceAdapter;
16 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
18 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListener;
19 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
20 import org.opendaylight.controller.sal.binding.api.NotificationListener;
21 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.util.ListenerRegistry;
24 import org.opendaylight.yangtools.yang.binding.Notification;
25 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class HeliumNotificationProviderServiceWithInterestListeners extends HeliumNotificationProviderServiceAdapter {
32 private static final Logger LOG = LoggerFactory.getLogger(HeliumNotificationProviderServiceWithInterestListeners.class);
34 private final ListenerRegistry<NotificationInterestListener> interestListeners = ListenerRegistry.create();
35 private final ListenerRegistration<Listener> domListener;
36 private final DOMNotificationService domService;
37 private final BindingToNormalizedNodeCodec codec;
39 public HeliumNotificationProviderServiceWithInterestListeners(
40 final BindingDOMNotificationPublishServiceAdapter publishService, final BindingDOMNotificationServiceAdapter listenService, final DOMNotificationSubscriptionListenerRegistry registry) {
41 super(publishService, listenService);
42 this.codec = publishService.getCodecRegistry();
43 this.domListener = registry.registerSubscriptionListener(new Listener());
44 this.domService = listenService.getDomService();
48 public ListenerRegistration<NotificationInterestListener> registerInterestListener(
49 final NotificationInterestListener listener) {
50 notifyListener(listener, translate(domListener.getInstance().getAllObserved()));
51 return interestListeners.register(listener);
54 private Set<Class<? extends Notification>> translate(final Set<SchemaPath> added) {
55 return codec.getNotificationClasses(added);
58 private void notifyAllListeners(final Set<SchemaPath> added) {
59 final Iterator<ListenerRegistration<NotificationInterestListener>> listeners = interestListeners.iterator();
60 if(listeners.hasNext()) {
61 final Set<Class<? extends Notification>> baEvent = translate(added);
62 while(listeners.hasNext()) {
63 final NotificationInterestListener listenerRef = listeners.next().getInstance();
65 notifyListener(listenerRef,baEvent);
66 } catch (final Exception e) {
67 LOG.warn("Unhandled exception during invoking listener {}",e, listenerRef);
74 public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(
75 final Class<T> type, final NotificationListener<T> listener) {
77 final FunctionalNotificationListenerAdapter<T> adapter = new FunctionalNotificationListenerAdapter<>(codec, type, listener);
78 final SchemaPath domType = SchemaPath.create(true, BindingReflections.findQName(type));
79 final ListenerRegistration<?> domReg = domService.registerNotificationListener(adapter, domType);
80 return new AbstractListenerRegistration<NotificationListener<T>>(listener) {
82 protected void removeRegistration() {
89 private void notifyListener(final NotificationInterestListener listener, final Set<Class<? extends Notification>> baEvent) {
90 for(final Class<? extends Notification> event: baEvent) {
91 listener.onNotificationSubscribtion(event);
95 private final class Listener implements DOMNotificationSubscriptionListener {
97 private volatile Set<SchemaPath> allObserved = Collections.emptySet();
100 public void onSubscriptionChanged(final Set<SchemaPath> currentTypes) {
101 final Set<SchemaPath> added = Sets.difference(currentTypes, allObserved).immutableCopy();
102 notifyAllListeners(added);
103 allObserved = Sets.union(allObserved, added).immutableCopy();
106 Set<SchemaPath> getAllObserved() {
112 public void close() throws Exception {