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.dom.broker;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.EnumMap;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.util.MapAdaptor;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
29 class ShardedDOMDataTreeListenerContext<T extends DOMDataTreeListener> implements AutoCloseable {
31 private final DOMDataTreeListener listener;
32 private final EnumMap<LogicalDatastoreType, StoreListener> storeListeners = new EnumMap<>(
33 LogicalDatastoreType.class);
34 private final Collection<ListenerRegistration<?>> registrations = new ArrayList<>();
36 // FIXME: Probably should be encapsulated into state object
38 private Collection<DataTreeCandidate> unreported = new ArrayList<>();
40 private Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> currentData = Collections.emptyMap();
42 private ShardedDOMDataTreeListenerContext(final T listener) {
43 for (LogicalDatastoreType type : LogicalDatastoreType.values()) {
44 storeListeners.put(type, new StoreListener(type));
46 this.listener = Preconditions.checkNotNull(listener, "listener");
49 static <T extends DOMDataTreeListener> ShardedDOMDataTreeListenerContext<T> create(final T listener) {
50 return new ShardedDOMDataTreeListenerContext<>(listener);
53 synchronized void notifyListener() {
54 Collection<DataTreeCandidate> changesToNotify = unreported;
55 unreported = new ArrayList<>();
56 listener.onDataTreeChanged(changesToNotify, currentData);
59 void register(final DOMDataTreeIdentifier subtree, final DOMStoreTreeChangePublisher shard) {
60 ListenerRegistration<?> storeReg =
61 shard.registerTreeChangeListener(subtree.getRootIdentifier(),
62 storeListeners.get(subtree.getDatastoreType()));
63 registrations.add(storeReg);
66 private final class StoreListener implements DOMDataTreeChangeListener {
68 private final LogicalDatastoreType type;
70 StoreListener(final LogicalDatastoreType type) {
75 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
76 receivedDataTreeChanges(type, changes);
77 scheduleNotification();
82 // FIXME: Should be able to run parallel to notifyListener and should honor
84 synchronized void receivedDataTreeChanges(final LogicalDatastoreType type,
85 final Collection<DataTreeCandidate> changes) {
86 Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> updatedData =
87 MapAdaptor.getDefaultInstance().takeSnapshot(currentData);
88 for (DataTreeCandidate change : changes) {
89 // FIXME: Make sure only one is reported / merged
90 unreported.add(change);
91 DOMDataTreeIdentifier treeId = new DOMDataTreeIdentifier(type, change.getRootPath());
92 // FIXME: Probably we should apply data tree candidate to previously observed state
93 Optional<NormalizedNode<?, ?>> dataAfter = change.getRootNode().getDataAfter();
94 if (dataAfter.isPresent()) {
95 updatedData.put(treeId, dataAfter.get());
97 updatedData.remove(treeId);
100 currentData = MapAdaptor.getDefaultInstance().optimize(updatedData);
103 void scheduleNotification() {
104 // FIXME: This callout should schedule delivery task
109 public void close() {
110 for (ListenerRegistration<?> reg : registrations) {
115 DOMDataTreeListener getListener() {