Bug 3868: Added support for DOMDataTreeListener
[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 class ShardedDOMDataTreeListenerContext<T extends DOMDataTreeListener> implements AutoCloseable {
29
30     private final DOMDataTreeListener listener;
31     private final EnumMap<LogicalDatastoreType, StoreListener> storeListeners = new EnumMap<>(
32             LogicalDatastoreType.class);
33     private final Collection<ListenerRegistration<?>> registrations = new ArrayList<>();
34
35     // FIXME: Probably should be encapsulated into state object
36     @GuardedBy("this")
37     private Collection<DataTreeCandidate> unreported = new ArrayList<>();
38     @GuardedBy("this")
39     private Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> currentData = Collections.emptyMap();
40
41     private ShardedDOMDataTreeListenerContext(T listener, Collection<DOMDataTreeIdentifier> subtrees,
42             boolean allowRxMerges) {
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             final Collection<DOMDataTreeIdentifier> subtrees, final boolean allowRxMerges) {
51         return new ShardedDOMDataTreeListenerContext<>(listener, subtrees, allowRxMerges);
52     }
53
54     synchronized void notifyListener() {
55         Collection<DataTreeCandidate> changesToNotify = unreported;
56         unreported = new ArrayList<>();
57         listener.onDataTreeChanged(changesToNotify, currentData);
58
59     }
60
61     void register(DOMDataTreeIdentifier subtree, DOMStoreTreeChangePublisher shard) {
62         ListenerRegistration<?> storeReg =
63                 shard.registerTreeChangeListener(subtree.getRootIdentifier(),
64                         storeListeners.get(subtree.getDatastoreType()));
65         registrations.add(storeReg);
66     }
67
68     private final class StoreListener implements DOMDataTreeChangeListener {
69
70         private final LogicalDatastoreType type;
71
72         public StoreListener(LogicalDatastoreType type) {
73             this.type = type;
74         }
75
76         @Override
77         public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
78             receivedDataTreeChanges(type, changes);
79             scheduleNotification();
80         }
81
82     }
83
84     // FIXME: Should be able to run parallel to notifyListener and should honor
85     // allowRxMerges
86     synchronized void receivedDataTreeChanges(LogicalDatastoreType type, Collection<DataTreeCandidate> changes) {
87         Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> updatedData =
88                 MapAdaptor.getDefaultInstance().takeSnapshot(currentData);
89         for (DataTreeCandidate change : changes) {
90             // FIXME: Make sure only one is reported / merged
91             unreported.add(change);
92             DOMDataTreeIdentifier treeId = new DOMDataTreeIdentifier(type, change.getRootPath());
93             // FIXME: Probably we should apply data tree candidate to previously observed state
94             Optional<NormalizedNode<?, ?>> dataAfter = change.getRootNode().getDataAfter();
95             if (dataAfter.isPresent()) {
96                 updatedData.put(treeId, dataAfter.get());
97             } else {
98                 updatedData.remove(treeId);
99             }
100         }
101         currentData = MapAdaptor.getDefaultInstance().optimize(updatedData);
102     }
103
104     void scheduleNotification() {
105         // FIXME: This callout should schedule delivery task
106         notifyListener();
107     }
108
109     @Override
110     public void close() {
111         for (ListenerRegistration<?> reg : registrations) {
112             reg.close();
113         }
114     }
115 }