2 * Copyright (c) 2017 Pantheon Technologies, s.ro. 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.dom.spi.shard;
10 import static java.util.Objects.requireNonNull;
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;
26 * Compatibility bridge between {@link DOMDataTreeListenerRegistry} and {@link DOMStoreTreeChangePublisher}.
30 public final class CompatDOMDataTreeListenerRegistry implements DOMDataTreeListenerRegistry {
32 private final DOMStoreTreeChangePublisher publisher;
34 public CompatDOMDataTreeListenerRegistry(final DOMStoreTreeChangePublisher publisher) {
35 this.publisher = requireNonNull(publisher);
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);
44 final ListenerRegistration<?> reg = publisher.registerTreeChangeListener(treeId.getRootIdentifier(),
46 final Optional<NormalizedNode<?, ?>> last = Iterables.getLast(changes).getRootNode().getDataAfter();
47 if (last.isPresent()) {
48 listener.onDataTreeChanged(changes, ImmutableMap.of(treeId, last.get()));
50 listener.onDataTreeChanged(changes, ImmutableMap.of());
53 return new AbstractListenerRegistration<T>(listener) {
55 protected void removeRegistration() {
61 final int size = subtrees.size();
62 final Collection<ListenerRegistration<?>> regs = new ArrayList<>(size);
63 final DOMDataTreeChangeListenerAggregator aggregator = new DOMDataTreeChangeListenerAggregator(size,
65 for (DOMDataTreeIdentifier treeId : subtrees) {
66 regs.add(publisher.registerTreeChangeListener(treeId.getRootIdentifier(),
67 aggregator.createListener(treeId)));
70 return aggregator.start(listener, regs);