ec32ff9526827f2bf551da9df2625de47cc7d20b
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / compat / HeliumNotificationProviderServiceWithInterestListeners.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.controller.md.sal.binding.compat;
9
10 import com.google.common.collect.Sets;
11 import java.util.Collections;
12 import java.util.Iterator;
13 import java.util.Set;
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.mdsal.binding.spec.reflect.BindingReflections;
22 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.util.ListenerRegistry;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Deprecated
31 public class HeliumNotificationProviderServiceWithInterestListeners extends HeliumNotificationProviderServiceAdapter {
32
33     private static final Logger LOG = LoggerFactory.getLogger(
34             HeliumNotificationProviderServiceWithInterestListeners.class);
35
36     private final ListenerRegistry<NotificationInterestListener> interestListeners = ListenerRegistry.create();
37     private final ListenerRegistration<Listener> domListener;
38     private final DOMNotificationService domService;
39     private final BindingToNormalizedNodeCodec codec;
40
41     public HeliumNotificationProviderServiceWithInterestListeners(
42             final BindingDOMNotificationPublishServiceAdapter publishService,
43             final BindingDOMNotificationServiceAdapter listenService,
44             final DOMNotificationSubscriptionListenerRegistry registry) {
45         super(publishService, listenService);
46         this.codec = publishService.getCodecRegistry();
47         this.domListener = registry.registerSubscriptionListener(new Listener());
48         this.domService = listenService.getDomService();
49     }
50
51     @Override
52     public ListenerRegistration<NotificationInterestListener> registerInterestListener(
53             final NotificationInterestListener listener) {
54         notifyListener(listener, translate(domListener.getInstance().getAllObserved()));
55         return interestListeners.register(listener);
56     }
57
58     private Set<Class<? extends Notification>> translate(final Set<SchemaPath> added) {
59         return codec.getNotificationClasses(added);
60     }
61
62     @SuppressWarnings("checkstyle:IllegalCatch")
63     private void notifyAllListeners(final Set<SchemaPath> added) {
64         final Iterator<? extends ListenerRegistration<? extends NotificationInterestListener>> listeners =
65                 interestListeners.getRegistrations().iterator();
66         if (listeners.hasNext()) {
67             final Set<Class<? extends Notification>> baEvent = translate(added);
68             while (listeners.hasNext()) {
69                 final NotificationInterestListener listenerRef = listeners.next().getInstance();
70                 try {
71                     notifyListener(listenerRef, baEvent);
72                 } catch (RuntimeException  e) {
73                     LOG.warn("Unhandled exception during invoking listener {}", listenerRef, e);
74                 }
75             }
76         }
77     }
78
79     @Override
80     public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(
81             final Class<T> type, final NotificationListener<T> listener) {
82
83         final FunctionalNotificationListenerAdapter<T> adapter =
84                 new FunctionalNotificationListenerAdapter<>(codec, type, listener);
85         final SchemaPath domType = SchemaPath.create(true, BindingReflections.findQName(type));
86         final ListenerRegistration<?> domReg = domService.registerNotificationListener(adapter, domType);
87         return new AbstractListenerRegistration<NotificationListener<T>>(listener) {
88             @Override
89             protected void removeRegistration() {
90                 domReg.close();
91             }
92         };
93     }
94
95     private static void notifyListener(final NotificationInterestListener listener,
96             final Set<Class<? extends Notification>> baEvent) {
97         for (final Class<? extends Notification> event: baEvent) {
98             listener.onNotificationSubscribtion(event);
99         }
100     }
101
102     private final class Listener implements DOMNotificationSubscriptionListener {
103
104         private volatile Set<SchemaPath> allObserved = Collections.emptySet();
105
106         @Override
107         public void onSubscriptionChanged(final Set<SchemaPath> currentTypes) {
108             final Set<SchemaPath> added = Sets.difference(currentTypes, allObserved).immutableCopy();
109             notifyAllListeners(added);
110             allObserved = Sets.union(allObserved, added).immutableCopy();
111         }
112
113         Set<SchemaPath> getAllObserved() {
114             return allObserved;
115         }
116     }
117
118     @Override
119     public void close() {
120         super.close();
121         domListener.close();
122     }
123 }