Make LambdaTarget a record
[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.HashMap;
16 import java.util.Set;
17 import java.util.concurrent.Executor;
18 import org.opendaylight.mdsal.binding.api.NotificationService;
19 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
20 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
21 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
22 import org.opendaylight.mdsal.dom.api.DOMService;
23 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.concepts.Registration;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.Notification;
28 import org.opendaylight.yangtools.yang.binding.NotificationListener;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
30
31 @VisibleForTesting
32 // FIXME: 10.0.0: make this class final
33 public class BindingDOMNotificationServiceAdapter implements NotificationService {
34     public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
35
36     private final AdapterContext adapterContext;
37     private final DOMNotificationService domNotifService;
38
39     public BindingDOMNotificationServiceAdapter(final AdapterContext adapterContext,
40             final DOMNotificationService domNotifService) {
41         this.adapterContext = requireNonNull(adapterContext);
42         this.domNotifService = domNotifService;
43     }
44
45     @Override
46     @Deprecated(since = "10.0.0", forRemoval = true)
47     public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
48         final var domListener = new BindingDOMNotificationListenerAdapter(adapterContext, listener);
49         return new ListenerRegistrationImpl<>(listener,
50             domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications()));
51     }
52
53     @Override
54     public <N extends Notification<N> & DataObject> Registration registerListener(final Class<N> type,
55             final Listener<N> listener, final Executor executor) {
56         final var domListener = new SingleBindingDOMNotificationAdapter<>(adapterContext, type, listener, executor);
57         return domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
58     }
59
60     @Override
61     public Registration registerCompositeListener(final CompositeListener listener, final Executor executor) {
62         final var exec = requireNonNull(executor);
63         final var listeners = new HashMap<Absolute, DOMNotificationListener>();
64         for (var constituent : listener.constituents()) {
65             final var domListener = new SingleBindingDOMNotificationAdapter<>(adapterContext, constituent, exec);
66             listeners.put(domListener.getSupportedNotifications().iterator().next(), domListener);
67         }
68
69         return domNotifService.registerNotificationListeners(listeners);
70     }
71
72     @Deprecated(since = "10.0.0", forRemoval = true)
73     private static final class ListenerRegistrationImpl<T extends NotificationListener>
74             extends AbstractListenerRegistration<T> {
75         private final ListenerRegistration<?> listenerRegistration;
76
77         ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
78             super(listener);
79             this.listenerRegistration = listenerRegistration;
80         }
81
82         @Override
83         protected void removeRegistration() {
84             listenerRegistration.close();
85         }
86     }
87
88     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
89         Builder(final AdapterContext adapterContext) {
90             super(adapterContext);
91         }
92
93         @Override
94         protected NotificationService createInstance(final ClassToInstanceMap<DOMService> delegates) {
95             final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
96             return new BindingDOMNotificationServiceAdapter(adapterContext(), domNotification);
97         }
98
99         @Override
100         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
101             return ImmutableSet.of(DOMNotificationService.class);
102         }
103     }
104 }