Eliminate some more references to SchemaService
[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.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;
29
30 public class HeliumNotificationProviderServiceWithInterestListeners extends HeliumNotificationProviderServiceAdapter {
31
32     private static final Logger LOG = LoggerFactory.getLogger(HeliumNotificationProviderServiceWithInterestListeners.class);
33
34     private final ListenerRegistry<NotificationInterestListener> interestListeners = ListenerRegistry.create();
35     private final ListenerRegistration<Listener> domListener;
36     private final DOMNotificationService domService;
37     private final BindingToNormalizedNodeCodec codec;
38
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();
45     }
46
47     @Override
48     public ListenerRegistration<NotificationInterestListener> registerInterestListener(
49             final NotificationInterestListener listener) {
50         notifyListener(listener, translate(domListener.getInstance().getAllObserved()));
51         return interestListeners.register(listener);
52     }
53
54     private Set<Class<? extends Notification>> translate(final Set<SchemaPath> added) {
55         return codec.getNotificationClasses(added);
56     }
57
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();
64                 try {
65                     notifyListener(listenerRef,baEvent);
66                 } catch (final Exception e) {
67                     LOG.warn("Unhandled exception during invoking listener {}",e, listenerRef);
68                 }
69             }
70         }
71     }
72
73     @Override
74     public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(
75             final Class<T> type, final NotificationListener<T> listener) {
76
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) {
81             @Override
82             protected void removeRegistration() {
83                 domReg.close();
84             }
85
86         };
87     }
88
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);
92         }
93     }
94
95     private final class Listener implements DOMNotificationSubscriptionListener {
96
97         private volatile Set<SchemaPath> allObserved = Collections.emptySet();
98
99         @Override
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();
104         }
105
106         Set<SchemaPath> getAllObserved() {
107             return allObserved;
108         }
109     }
110
111     @Override
112     public void close() throws Exception {
113         super.close();
114         domListener.close();
115     }
116 }