/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.mdsal.binding.dom.adapter; import static java.util.Objects.requireNonNull; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ClassToInstanceMap; import com.google.common.collect.ImmutableSet; import java.util.Set; import java.util.concurrent.Executor; import org.opendaylight.mdsal.binding.api.NotificationService; import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory; import org.opendaylight.mdsal.dom.api.DOMNotificationService; import org.opendaylight.mdsal.dom.api.DOMService; import org.opendaylight.yangtools.concepts.AbstractListenerRegistration; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.Notification; import org.opendaylight.yangtools.yang.binding.NotificationListener; @VisibleForTesting // FIXME: 10.0.0: make this class final public class BindingDOMNotificationServiceAdapter implements NotificationService { public static final Factory BUILDER_FACTORY = Builder::new; private final AdapterContext adapterContext; private final DOMNotificationService domNotifService; public BindingDOMNotificationServiceAdapter(final AdapterContext adapterContext, final DOMNotificationService domNotifService) { this.adapterContext = requireNonNull(adapterContext); this.domNotifService = domNotifService; } @Override public ListenerRegistration registerNotificationListener(final T listener) { final BindingDOMNotificationListenerAdapter domListener = new BindingDOMNotificationListenerAdapter(adapterContext, listener); final ListenerRegistration domRegistration = domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications()); return new ListenerRegistrationImpl<>(listener, domRegistration); } @Override public & DataObject> Registration registerListener(final Class type, final Listener listener, final Executor executor) { final var domListener = new SingleBindingDOMNotificationAdapter<>(adapterContext, type, listener, executor); return domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications()); } public DOMNotificationService getDomService() { return domNotifService; } private static class ListenerRegistrationImpl extends AbstractListenerRegistration { private final ListenerRegistration listenerRegistration; ListenerRegistrationImpl(final T listener, final ListenerRegistration listenerRegistration) { super(listener); this.listenerRegistration = listenerRegistration; } @Override protected void removeRegistration() { listenerRegistration.close(); } } private static class Builder extends BindingDOMAdapterBuilder { Builder(final AdapterContext adapterContext) { super(adapterContext); } @Override protected NotificationService createInstance(final ClassToInstanceMap delegates) { final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class); return new BindingDOMNotificationServiceAdapter(adapterContext(), domNotification); } @Override public Set> getRequiredDelegates() { return ImmutableSet.of(DOMNotificationService.class); } } }