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;
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;
29 public class BindingDOMNotificationServiceAdapter implements NotificationService {
30 public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
32 private final AdapterContext adapterContext;
33 private final DOMNotificationService domNotifService;
35 public BindingDOMNotificationServiceAdapter(final AdapterContext adapterContext,
36 final DOMNotificationService domNotifService) {
37 this.adapterContext = requireNonNull(adapterContext);
38 this.domNotifService = domNotifService;
42 public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
43 final BindingDOMNotificationListenerAdapter domListener
44 = new BindingDOMNotificationListenerAdapter(adapterContext, listener);
45 final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
46 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
47 return new ListenerRegistrationImpl<>(listener, domRegistration);
51 public <N extends Notification<N> & DataObject> Registration registerListener(final Class<N> type,
52 final Listener<N> listener, final Executor executor) {
53 final var domListener = new SingleBindingDOMNotificationAdapter<>(adapterContext, type, listener, executor);
54 return domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
57 public DOMNotificationService getDomService() {
58 return domNotifService;
61 private static class ListenerRegistrationImpl<T extends NotificationListener>
62 extends AbstractListenerRegistration<T> {
63 private final ListenerRegistration<?> listenerRegistration;
65 ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
67 this.listenerRegistration = listenerRegistration;
71 protected void removeRegistration() {
72 listenerRegistration.close();
76 private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
77 Builder(final AdapterContext adapterContext) {
78 super(adapterContext);
82 protected NotificationService createInstance(final ClassToInstanceMap<DOMService> delegates) {
83 final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
84 return new BindingDOMNotificationServiceAdapter(adapterContext(), domNotification);
88 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
89 return ImmutableSet.of(DOMNotificationService.class);