BUG-8733: Add ListenableDOMDataTreeShard
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / shard / CompatDOMDataTreeListenerRegistry.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.ro. 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.dom.spi.shard;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Optional;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.Iterables;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
21 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 /**
26  * Compatibility bridge between {@link DOMDataTreeListenerRegistry} and {@link DOMStoreTreeChangePublisher}.
27  */
28 @Beta
29 @Deprecated
30 public final class CompatDOMDataTreeListenerRegistry implements DOMDataTreeListenerRegistry {
31
32     private final DOMStoreTreeChangePublisher publisher;
33
34     public CompatDOMDataTreeListenerRegistry(final DOMStoreTreeChangePublisher publisher) {
35         this.publisher = requireNonNull(publisher);
36     }
37
38     @Override
39     public <T extends DOMDataTreeListener> ListenerRegistration<T> registerListener(final T listener,
40             final Collection<DOMDataTreeIdentifier> subtrees, final boolean allowRxMerges) {
41         if (subtrees.size() == 1) {
42             final DOMDataTreeIdentifier treeId = Iterables.getOnlyElement(subtrees);
43
44             final ListenerRegistration<?> reg = publisher.registerTreeChangeListener(treeId.getRootIdentifier(),
45                 changes -> {
46                     final Optional<NormalizedNode<?, ?>> last = Iterables.getLast(changes).getRootNode().getDataAfter();
47                     if (last.isPresent()) {
48                         listener.onDataTreeChanged(changes, ImmutableMap.of(treeId, last.get()));
49                     } else {
50                         listener.onDataTreeChanged(changes, ImmutableMap.of());
51                     }
52                 });
53             return new AbstractListenerRegistration<T>(listener) {
54                 @Override
55                 protected void removeRegistration() {
56                     reg.close();
57                 }
58             };
59         }
60
61         final int size = subtrees.size();
62         final Collection<ListenerRegistration<?>> regs = new ArrayList<>(size);
63         final DOMDataTreeChangeListenerAggregator aggregator = new DOMDataTreeChangeListenerAggregator(size,
64             allowRxMerges);
65         for (DOMDataTreeIdentifier treeId : subtrees) {
66             regs.add(publisher.registerTreeChangeListener(treeId.getRootIdentifier(),
67                 aggregator.createListener(treeId)));
68         }
69
70         return aggregator.start(listener, regs);
71     }
72 }