BUG-8733: Add ListenableDOMDataTreeShard
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / ShardedDOMDataTreeListenerContext.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.dom.broker;
9
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;
16 import java.util.Map;
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;
27
28 @Deprecated
29 class ShardedDOMDataTreeListenerContext<T extends DOMDataTreeListener> implements AutoCloseable {
30
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<>();
35
36     // FIXME: Probably should be encapsulated into state object
37     @GuardedBy("this")
38     private Collection<DataTreeCandidate> unreported = new ArrayList<>();
39     @GuardedBy("this")
40     private Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> currentData = Collections.emptyMap();
41
42     private ShardedDOMDataTreeListenerContext(final T listener) {
43         for (LogicalDatastoreType type : LogicalDatastoreType.values()) {
44             storeListeners.put(type, new StoreListener(type));
45         }
46         this.listener = Preconditions.checkNotNull(listener, "listener");
47     }
48
49     static <T extends DOMDataTreeListener> ShardedDOMDataTreeListenerContext<T> create(final T listener) {
50         return new ShardedDOMDataTreeListenerContext<>(listener);
51     }
52
53     synchronized void notifyListener() {
54         Collection<DataTreeCandidate> changesToNotify = unreported;
55         unreported = new ArrayList<>();
56         listener.onDataTreeChanged(changesToNotify, currentData);
57     }
58
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);
64     }
65
66     private final class StoreListener implements DOMDataTreeChangeListener {
67
68         private final LogicalDatastoreType type;
69
70         StoreListener(final LogicalDatastoreType type) {
71             this.type = type;
72         }
73
74         @Override
75         public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
76             receivedDataTreeChanges(type, changes);
77             scheduleNotification();
78         }
79
80     }
81
82     // FIXME: Should be able to run parallel to notifyListener and should honor
83     // allowRxMerges
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());
96             } else {
97                 updatedData.remove(treeId);
98             }
99         }
100         currentData = MapAdaptor.getDefaultInstance().optimize(updatedData);
101     }
102
103     void scheduleNotification() {
104         // FIXME: This callout should schedule delivery task
105         notifyListener();
106     }
107
108     @Override
109     public void close() {
110         for (ListenerRegistration<?> reg : registrations) {
111             reg.close();
112         }
113     }
114
115     DOMDataTreeListener getListener() {
116         return listener;
117     }
118 }