Add NotificationService.registerListener()
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationServiceAdapter.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.mdsal.binding.dom.adapter;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.collect.ClassToInstanceMap;
14 import com.google.common.collect.ImmutableSet;
15 import java.util.Set;
16 import java.util.concurrent.Executor;
17 import org.opendaylight.mdsal.binding.api.NotificationService;
18 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
19 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
20 import org.opendaylight.mdsal.dom.api.DOMService;
21 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.concepts.Registration;
24 import org.opendaylight.yangtools.yang.binding.Notification;
25 import org.opendaylight.yangtools.yang.binding.NotificationListener;
26
27 @VisibleForTesting
28 public class BindingDOMNotificationServiceAdapter implements NotificationService {
29     public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
30
31     private final AdapterContext adapterContext;
32     private final DOMNotificationService domNotifService;
33
34     public BindingDOMNotificationServiceAdapter(final AdapterContext adapterContext,
35             final DOMNotificationService domNotifService) {
36         this.adapterContext = requireNonNull(adapterContext);
37         this.domNotifService = domNotifService;
38     }
39
40     @Override
41     public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
42         final BindingDOMNotificationListenerAdapter domListener
43                 = new BindingDOMNotificationListenerAdapter(adapterContext, listener);
44         final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
45                 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
46         return new ListenerRegistrationImpl<>(listener, domRegistration);
47     }
48
49     @Override
50     public <N extends Notification> Registration registerListener(final Class<N> type, final Listener<N> listener,
51             final Executor executor) {
52         final var domListener = new SingleBindingDOMNotificationAdapter<>(adapterContext, type, listener, executor);
53         return domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
54     }
55
56     public DOMNotificationService getDomService() {
57         return domNotifService;
58     }
59
60     private static class ListenerRegistrationImpl<T extends NotificationListener>
61             extends AbstractListenerRegistration<T> {
62         private final ListenerRegistration<?> listenerRegistration;
63
64         ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
65             super(listener);
66             this.listenerRegistration = listenerRegistration;
67         }
68
69         @Override
70         protected void removeRegistration() {
71             listenerRegistration.close();
72         }
73     }
74
75     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
76         Builder(final AdapterContext adapterContext) {
77             super(adapterContext);
78         }
79
80         @Override
81         protected NotificationService createInstance(final ClassToInstanceMap<DOMService> delegates) {
82             final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
83             return new BindingDOMNotificationServiceAdapter(adapterContext(), domNotification);
84         }
85
86         @Override
87         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
88             return ImmutableSet.of(DOMNotificationService.class);
89         }
90     }
91 }