Resolve Bug:445 Remove freemarker from config code generator.
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / DataChangeEventResolver.java
1 package org.opendaylight.controller.md.sal.dom.store.impl;
2
3 import static org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.builder;
4 import static org.opendaylight.controller.md.sal.dom.store.impl.StoreUtils.append;
5 import static org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils.getChild;
6
7 import java.util.Collection;
8 import java.util.HashSet;
9 import java.util.Set;
10
11 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
12 import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent.Builder;
13 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerRegistrationNode;
14 import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.common.base.Optional;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableSet;
25
26 public class DataChangeEventResolver {
27     private static final Logger LOG = LoggerFactory.getLogger(DataChangeEventResolver.class);
28     private static final DOMImmutableDataChangeEvent NO_CHANGE = builder().build();
29     private final ImmutableList.Builder<ChangeListenerNotifyTask> tasks = ImmutableList.builder();
30     private InstanceIdentifier rootPath;
31     private ListenerRegistrationNode listenerRoot;
32     private NodeModification modificationRoot;
33     private Optional<StoreMetadataNode> beforeRoot;
34     private Optional<StoreMetadataNode> afterRoot;
35
36     protected InstanceIdentifier getRootPath() {
37         return rootPath;
38     }
39
40     protected DataChangeEventResolver setRootPath(final InstanceIdentifier rootPath) {
41         this.rootPath = rootPath;
42         return this;
43     }
44
45     protected ListenerRegistrationNode getListenerRoot() {
46         return listenerRoot;
47     }
48
49     protected DataChangeEventResolver setListenerRoot(final ListenerRegistrationNode listenerRoot) {
50         this.listenerRoot = listenerRoot;
51         return this;
52     }
53
54     protected NodeModification getModificationRoot() {
55         return modificationRoot;
56     }
57
58     protected DataChangeEventResolver setModificationRoot(final NodeModification modificationRoot) {
59         this.modificationRoot = modificationRoot;
60         return this;
61     }
62
63     protected Optional<StoreMetadataNode> getBeforeRoot() {
64         return beforeRoot;
65     }
66
67     protected DataChangeEventResolver setBeforeRoot(final Optional<StoreMetadataNode> beforeRoot) {
68         this.beforeRoot = beforeRoot;
69         return this;
70     }
71
72     protected Optional<StoreMetadataNode> getAfterRoot() {
73         return afterRoot;
74     }
75
76     protected DataChangeEventResolver setAfterRoot(final Optional<StoreMetadataNode> afterRoot) {
77         this.afterRoot = afterRoot;
78         return this;
79     }
80
81     public Iterable<ChangeListenerNotifyTask> resolve() {
82         LOG.trace("Resolving events for {}" ,modificationRoot);
83         resolveAnyChangeEvent(rootPath, Optional.of(listenerRoot), modificationRoot, beforeRoot, afterRoot);
84         return tasks.build();
85     }
86
87     private DOMImmutableDataChangeEvent resolveAnyChangeEvent(final InstanceIdentifier path,
88             final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
89             final Optional<StoreMetadataNode> before, final Optional<StoreMetadataNode> after) {
90         // No listeners are present in listener registration subtree
91         // no before and after state is present
92         if (!before.isPresent() && !after.isPresent()) {
93             return NO_CHANGE;
94         }
95         switch (modification.getModificationType()) {
96         case SUBTREE_MODIFIED:
97             return resolveSubtreeChangeEvent(path, listeners, modification, before.get(), after.get());
98         case WRITE:
99             if (before.isPresent()) {
100                 return resolveReplacedEvent(path, listeners, modification, before.get(), after.get());
101             } else {
102                 return resolveCreateEvent(path, listeners, after.get());
103             }
104         case DELETE:
105             return resolveDeleteEvent(path, listeners, before.get());
106         default:
107             return NO_CHANGE;
108         }
109
110     }
111
112     /**
113      * Resolves create events deep down the interest listener tree.
114      *
115      *
116      * @param path
117      * @param listeners
118      * @param afterState
119      * @return
120      */
121     private DOMImmutableDataChangeEvent resolveCreateEvent(final InstanceIdentifier path,
122             final Optional<ListenerRegistrationNode> listeners, final StoreMetadataNode afterState) {
123         final NormalizedNode<?, ?> node = afterState.getData();
124         Builder builder = builder().setAfter(node).addCreated(path, node);
125
126         for (StoreMetadataNode child : afterState.getChildren()) {
127             PathArgument childId = child.getIdentifier();
128             Optional<ListenerRegistrationNode> childListeners = getChild(listeners, childId);
129
130             InstanceIdentifier childPath = StoreUtils.append(path, childId);
131             builder.merge(resolveCreateEvent(childPath, childListeners, child));
132         }
133
134         return addNotifyTask(listeners, builder.build());
135     }
136
137     private DOMImmutableDataChangeEvent resolveDeleteEvent(final InstanceIdentifier path,
138             final Optional<ListenerRegistrationNode> listeners, final StoreMetadataNode beforeState) {
139         final NormalizedNode<?, ?> node = beforeState.getData();
140         Builder builder = builder().setBefore(node).addRemoved(path, node);
141
142         for (StoreMetadataNode child : beforeState.getChildren()) {
143             PathArgument childId = child.getIdentifier();
144             Optional<ListenerRegistrationNode> childListeners = getChild(listeners, childId);
145             InstanceIdentifier childPath = StoreUtils.append(path, childId);
146             builder.merge(resolveDeleteEvent(childPath, childListeners, child));
147         }
148         return addNotifyTask(listeners, builder.build());
149     }
150
151     private DOMImmutableDataChangeEvent resolveSubtreeChangeEvent(final InstanceIdentifier path,
152             final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
153             final StoreMetadataNode before, final StoreMetadataNode after) {
154
155         Builder one = builder().setBefore(before.getData()).setAfter(after.getData());
156
157         Builder subtree = builder();
158
159         for (NodeModification childMod : modification.getModifications()) {
160             PathArgument childId = childMod.getIdentifier();
161             InstanceIdentifier childPath = append(path, childId);
162             Optional<ListenerRegistrationNode> childListen = getChild(listeners, childId);
163
164             Optional<StoreMetadataNode> childBefore = before.getChild(childId);
165             Optional<StoreMetadataNode> childAfter = after.getChild(childId);
166
167             switch (childMod.getModificationType()) {
168             case WRITE:
169             case DELETE:
170                 one.merge(resolveAnyChangeEvent(childPath, childListen, childMod, childBefore, childAfter));
171                 break;
172             case SUBTREE_MODIFIED:
173                 subtree.merge(resolveSubtreeChangeEvent(childPath, childListen, childMod, childBefore.get(),
174                         childAfter.get()));
175                 break;
176             case UNMODIFIED:
177                 // no-op
178                 break;
179             }
180         }
181         DOMImmutableDataChangeEvent oneChangeEvent = one.build();
182         subtree.merge(oneChangeEvent);
183         DOMImmutableDataChangeEvent subtreeEvent = subtree.build();
184         if (listeners.isPresent()) {
185             addNotifyTask(listeners.get(), DataChangeScope.ONE, oneChangeEvent);
186             addNotifyTask(listeners.get(), DataChangeScope.SUBTREE, subtreeEvent);
187         }
188         return subtreeEvent;
189     }
190
191     private DOMImmutableDataChangeEvent resolveReplacedEvent(final InstanceIdentifier path,
192             final Optional<ListenerRegistrationNode> listeners, final NodeModification modification,
193             final StoreMetadataNode before, final StoreMetadataNode after) {
194         // FIXME Add task
195         return builder().build();
196     }
197
198     private DOMImmutableDataChangeEvent addNotifyTask(final Optional<ListenerRegistrationNode> listeners, final DOMImmutableDataChangeEvent event) {
199         if (listeners.isPresent()) {
200             final Collection<DataChangeListenerRegistration<?>> l = listeners.get().getListeners();
201             if (!l.isEmpty()) {
202                 tasks.add(new ChangeListenerNotifyTask(ImmutableSet.copyOf(l), event));
203             }
204         }
205
206         return event;
207     }
208
209     private void addNotifyTask(final ListenerRegistrationNode listenerRegistrationNode, final DataChangeScope scope,
210             final DOMImmutableDataChangeEvent event) {
211         Collection<DataChangeListenerRegistration<?>> potential = listenerRegistrationNode.getListeners();
212         if(!potential.isEmpty()) {
213             final Set<DataChangeListenerRegistration<?>> toNotify = new HashSet<>(potential.size());
214             for(DataChangeListenerRegistration<?> listener : potential) {
215                 if(scope.equals(listener.getScope())) {
216                     toNotify.add(listener);
217                 }
218             }
219
220             if (!toNotify.isEmpty()) {
221                 tasks.add(new ChangeListenerNotifyTask(toNotify, event));
222             }
223         }
224     }
225
226     public static DataChangeEventResolver create() {
227         return new DataChangeEventResolver();
228     }
229 }