2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import static java.util.Objects.requireNonNull;
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;
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;
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;
36 private final AdapterContext adapterContext;
37 private final DOMNotificationService domNotifService;
39 public BindingDOMNotificationServiceAdapter(final AdapterContext adapterContext,
40 final DOMNotificationService domNotifService) {
41 this.adapterContext = requireNonNull(adapterContext);
42 this.domNotifService = domNotifService;
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()));
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());
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);
69 return domNotifService.registerNotificationListeners(listeners);
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;
77 ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
79 this.listenerRegistration = listenerRegistration;
83 protected void removeRegistration() {
84 listenerRegistration.close();
88 private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
89 Builder(final AdapterContext adapterContext) {
90 super(adapterContext);
94 protected NotificationService createInstance(final ClassToInstanceMap<DOMService> delegates) {
95 final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
96 return new BindingDOMNotificationServiceAdapter(adapterContext(), domNotification);
100 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
101 return ImmutableSet.of(DOMNotificationService.class);