Report ExactDataObjectStep from DataObjectModification
[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.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;
27
28 @VisibleForTesting
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;
32
33     private final AdapterContext adapterContext;
34     private final DOMNotificationService domNotifService;
35
36     public BindingDOMNotificationServiceAdapter(final AdapterContext adapterContext,
37             final DOMNotificationService domNotifService) {
38         this.adapterContext = requireNonNull(adapterContext);
39         this.domNotifService = domNotifService;
40     }
41
42     @Override
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()));
47     }
48
49     @Override
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);
56         }
57
58         return domNotifService.registerNotificationListeners(listeners);
59     }
60
61     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
62         Builder(final AdapterContext adapterContext) {
63             super(adapterContext);
64         }
65
66         @Override
67         protected NotificationService createInstance(final ClassToInstanceMap<DOMService<?, ?>> delegates) {
68             return new BindingDOMNotificationServiceAdapter(adapterContext(),
69                 delegates.getInstance(DOMNotificationService.class));
70         }
71
72         @Override
73         public Set<? extends Class<? extends DOMService<?, ?>>> getRequiredDelegates() {
74             return ImmutableSet.of(DOMNotificationService.class);
75         }
76     }
77 }