fc4653965bd399deb0e9a014dc2d6bcf4bfaa53c
[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.DataObject;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26 import org.opendaylight.yangtools.yang.binding.NotificationListener;
27
28 @VisibleForTesting
29 // FIXME: 10.0.0: make this class final
30 public class BindingDOMNotificationServiceAdapter implements NotificationService {
31     public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
32
33     private final AdapterContext adapterContext;
34     private final DOMNotificationService domNotifService;
35
36     public BindingDOMNotificationServiceAdapter(final AdapterContext adapterContext,
37             final DOMNotificationService domNotifService) {
38         this.adapterContext = requireNonNull(adapterContext);
39         this.domNotifService = domNotifService;
40     }
41
42     @Override
43     public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
44         final BindingDOMNotificationListenerAdapter domListener
45                 = new BindingDOMNotificationListenerAdapter(adapterContext, listener);
46         final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
47                 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
48         return new ListenerRegistrationImpl<>(listener, domRegistration);
49     }
50
51     @Override
52     public <N extends Notification<N> & DataObject> Registration registerListener(final Class<N> type,
53             final Listener<N> listener, final Executor executor) {
54         final var domListener = new SingleBindingDOMNotificationAdapter<>(adapterContext, type, listener, executor);
55         return domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
56     }
57
58     public DOMNotificationService getDomService() {
59         return domNotifService;
60     }
61
62     private static class ListenerRegistrationImpl<T extends NotificationListener>
63             extends AbstractListenerRegistration<T> {
64         private final ListenerRegistration<?> listenerRegistration;
65
66         ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
67             super(listener);
68             this.listenerRegistration = listenerRegistration;
69         }
70
71         @Override
72         protected void removeRegistration() {
73             listenerRegistration.close();
74         }
75     }
76
77     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
78         Builder(final AdapterContext adapterContext) {
79             super(adapterContext);
80         }
81
82         @Override
83         protected NotificationService createInstance(final ClassToInstanceMap<DOMService> delegates) {
84             final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
85             return new BindingDOMNotificationServiceAdapter(adapterContext(), domNotification);
86         }
87
88         @Override
89         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
90             return ImmutableSet.of(DOMNotificationService.class);
91         }
92     }
93 }