Bug 499: Added support for change listeners.
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / DOMImmutableDataChangeEvent.java
1 package org.opendaylight.controller.md.sal.dom.store.impl;
2
3 import java.util.Map;
4 import java.util.Set;
5
6 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
7 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
8 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
9
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.ImmutableSet;
12
13 public final class DOMImmutableDataChangeEvent implements
14         AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> {
15
16     private final NormalizedNode<?, ?> original;
17     private final NormalizedNode<?, ?> updated;
18     private final Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> originalData;
19     private final Map<InstanceIdentifier, NormalizedNode<?, ?>> createdData;
20     private final Map<InstanceIdentifier, NormalizedNode<?, ?>> updatedData;
21     private final Set<InstanceIdentifier> removedPaths;
22
23     private DOMImmutableDataChangeEvent(final Builder change) {
24         original = change.before;
25         updated = change.after;
26         originalData = change.original.build();
27         createdData = change.created.build();
28         updatedData = change.updated.build();
29         removedPaths = change.removed.build();
30     }
31
32     public static final Builder builder() {
33         return new Builder();
34     }
35
36     @Override
37     public NormalizedNode<?, ?> getOriginalSubtree() {
38         return original;
39     }
40
41     @Override
42     public NormalizedNode<?, ?> getUpdatedSubtree() {
43         return updated;
44     }
45
46     @Override
47     public Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> getOriginalData() {
48         return originalData;
49     }
50
51     @Override
52     public Map<InstanceIdentifier, NormalizedNode<?, ?>> getCreatedData() {
53         return createdData;
54     }
55
56     @Override
57     public Map<InstanceIdentifier, NormalizedNode<?, ?>> getUpdatedData() {
58         return updatedData;
59     }
60
61     @Override
62     public Set<InstanceIdentifier> getRemovedPaths() {
63         return removedPaths;
64     }
65
66     public static class Builder {
67
68         private NormalizedNode<?, ?> after;
69         private NormalizedNode<?, ?> before;
70
71         private final ImmutableMap.Builder<InstanceIdentifier, NormalizedNode<?, ?>> original = ImmutableMap.builder();
72         private final ImmutableMap.Builder<InstanceIdentifier, NormalizedNode<?, ?>> created = ImmutableMap.builder();
73         private final ImmutableMap.Builder<InstanceIdentifier, NormalizedNode<?, ?>> updated = ImmutableMap.builder();
74         private final ImmutableSet.Builder<InstanceIdentifier> removed = ImmutableSet.builder();
75
76
77         private Builder() {
78
79         }
80
81         public Builder setAfter(final NormalizedNode<?, ?> node) {
82             after = node;
83             return this;
84         }
85
86         public DOMImmutableDataChangeEvent build() {
87
88             return new DOMImmutableDataChangeEvent(this);
89         }
90
91         public void merge(final DOMImmutableDataChangeEvent nestedChanges) {
92
93             original.putAll(nestedChanges.getOriginalData());
94             created.putAll(nestedChanges.getCreatedData());
95             updated.putAll(nestedChanges.getUpdatedData());
96             removed.addAll(nestedChanges.getRemovedPaths());
97
98         }
99
100         public Builder setBefore(final NormalizedNode<?, ?> node) {
101             this.before = node;
102             return this;
103         }
104
105         public Builder addCreated(final InstanceIdentifier path, final NormalizedNode<?, ?> node) {
106             created.put(path, node);
107             return this;
108         }
109
110         public Builder addRemoved(final InstanceIdentifier path, final NormalizedNode<?, ?> node) {
111             original.put(path, node);
112             removed.add(path);
113             return this;
114         }
115
116         public Builder addUpdated(final InstanceIdentifier path, final NormalizedNode<?, ?> before,
117                 final NormalizedNode<?, ?> after) {
118             original.put(path, before);
119             updated.put(path, after);
120             return this;
121         }
122     }
123
124 }
125

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.