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.Registration;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
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;
33 private final AdapterContext adapterContext;
34 private final DOMNotificationService domNotifService;
36 public BindingDOMNotificationServiceAdapter(final AdapterContext adapterContext,
37 final DOMNotificationService domNotifService) {
38 this.adapterContext = requireNonNull(adapterContext);
39 this.domNotifService = domNotifService;
43 public <N extends Notification<N> & DataObject> Registration registerListener(final Class<N> type,
44 final Listener<N> listener, final Executor executor) {
45 final var domListener = new BindingDOMNotificationListenerAdapter<>(adapterContext, type, listener, executor);
46 return domNotifService.registerNotificationListener(domListener, Set.of(domListener.schemaPath()));
50 public Registration registerCompositeListener(final CompositeListener listener, final Executor executor) {
51 final var exec = requireNonNull(executor);
52 final var listeners = new HashMap<Absolute, DOMNotificationListener>();
53 for (var constituent : listener.constituents()) {
54 final var domListener = new BindingDOMNotificationListenerAdapter<>(adapterContext, constituent, exec);
55 listeners.put(domListener.schemaPath(), domListener);
58 return domNotifService.registerNotificationListeners(listeners);
61 private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
62 Builder(final AdapterContext adapterContext) {
63 super(adapterContext);
67 protected NotificationService createInstance(final ClassToInstanceMap<DOMService<?, ?>> delegates) {
68 return new BindingDOMNotificationServiceAdapter(adapterContext(),
69 delegates.getInstance(DOMNotificationService.class));
73 public Set<? extends Class<? extends DOMService<?, ?>>> getRequiredDelegates() {
74 return ImmutableSet.of(DOMNotificationService.class);