BUG-509: break up SchemaAwareApplyOperation
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / compat / TranslatingDataChangeEvent.java
1 /*
2  * Copyright (c) 2014 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.controller.md.sal.dom.broker.impl.compat;
9
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.Set;
14
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
16 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
17 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
18 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22
23 import com.google.common.collect.Maps;
24
25 public abstract class TranslatingDataChangeEvent implements
26         DataChangeEvent<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> {
27
28     private TranslatingDataChangeEvent() {
29     }
30
31     public static DataChangeEvent<InstanceIdentifier, CompositeNode> createOperational(
32             final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change, final DataNormalizer normalizer) {
33         return new OperationalChangeEvent(change, normalizer);
34     }
35
36     public static DataChangeEvent<InstanceIdentifier, CompositeNode> createConfiguration(
37             final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change, final DataNormalizer normalizer) {
38         return new ConfigurationChangeEvent(change, normalizer);
39     }
40
41     @Override
42     public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getCreatedOperationalData() {
43         return Collections.emptyMap();
44     }
45
46     @Override
47     public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getCreatedConfigurationData() {
48         return Collections.emptyMap();
49     }
50
51     @Override
52     public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getUpdatedOperationalData() {
53         return Collections.emptyMap();
54     }
55
56     @Override
57     public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getUpdatedConfigurationData() {
58         return Collections.emptyMap();
59     }
60
61     @Override
62     public Set<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier> getRemovedConfigurationData() {
63         return Collections.emptySet();
64     }
65
66     @Override
67     public Set<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier> getRemovedOperationalData() {
68         return Collections.emptySet();
69     }
70
71     @Override
72     public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getOriginalConfigurationData() {
73         return Collections.emptyMap();
74     }
75
76     @Override
77     public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getOriginalOperationalData() {
78         return Collections.emptyMap();
79     }
80
81     @Override
82     public CompositeNode getOriginalConfigurationSubtree() {
83         return null;
84     }
85
86     @Override
87     public CompositeNode getOriginalOperationalSubtree() {
88         return null;
89     }
90
91     @Override
92     public CompositeNode getUpdatedConfigurationSubtree() {
93         return null;
94     }
95
96     @Override
97     public CompositeNode getUpdatedOperationalSubtree() {
98         return null;
99     }
100
101     @SuppressWarnings({ "rawtypes", "unchecked" })
102     private final static class OperationalChangeEvent extends TranslatingDataChangeEvent {
103
104         private final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> delegate;
105         private final DataNormalizer normalizer;
106         private Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> updatedCache;
107
108         public OperationalChangeEvent(final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change,
109                                         final DataNormalizer normalizer) {
110             this.delegate = change;
111             this.normalizer = normalizer;
112         }
113
114         @Override
115         public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getCreatedOperationalData() {
116             return transformToLegacy(normalizer, delegate.getCreatedData());
117         }
118
119
120         @Override
121         public Set<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier> getRemovedOperationalData() {
122             return delegate.getRemovedPaths();
123         }
124
125         @Override
126         public CompositeNode getOriginalOperationalSubtree() {
127             // first argument is unused
128             return normalizer.toLegacy(null, delegate.getOriginalSubtree());
129         }
130
131         @Override
132         public CompositeNode getUpdatedOperationalSubtree() {
133             // first argument is unused
134             return normalizer.toLegacy(null, delegate.getUpdatedSubtree());
135         }
136
137         @Override
138         public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getOriginalOperationalData() {
139             return transformToLegacy(normalizer, delegate.getOriginalData());
140         }
141
142         @Override
143         public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getUpdatedOperationalData() {
144             if(updatedCache == null) {
145                 final Map<InstanceIdentifier, CompositeNode> updated = transformToLegacy(normalizer, delegate.getUpdatedData());
146                 final Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> created = getCreatedConfigurationData();
147                 final HashMap<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> updatedComposite = new HashMap<>(created.size() + updated.size());
148                 updatedComposite.putAll(created);
149                 updatedComposite.putAll(updated);
150                 updatedCache = Collections.unmodifiableMap(updatedComposite);
151             }
152             return updatedCache;
153         }
154
155         @Override
156         public String toString() {
157             return "OperationalChangeEvent [delegate=" + delegate + "]";
158         }
159
160     }
161
162     private static Map<InstanceIdentifier, CompositeNode> transformToLegacy(final DataNormalizer normalizer, final Map<InstanceIdentifier, ? extends NormalizedNode<?, ?>> nodes) {
163         final Map<InstanceIdentifier, CompositeNode> legacy = Maps.newHashMap();
164
165         for (final Map.Entry<InstanceIdentifier, ? extends NormalizedNode<?, ?>> entry : nodes.entrySet()) {
166             try {
167                 legacy.put(normalizer.toLegacy(entry.getKey()), normalizer.toLegacy(entry.getKey(), entry.getValue()));
168             } catch (final DataNormalizationException e) {
169                 throw new IllegalStateException("Unable to transform data change event to legacy format", e);
170             }
171         }
172         return legacy;
173     }
174
175     private final static class ConfigurationChangeEvent extends TranslatingDataChangeEvent {
176
177         private final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> delegate;
178         private final DataNormalizer normalizer;
179         private Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> updatedCache;
180
181         public ConfigurationChangeEvent(final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change,
182                                         final DataNormalizer normalizer) {
183             this.delegate = change;
184             this.normalizer = normalizer;
185         }
186
187         @Override
188         public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getCreatedConfigurationData() {
189             return transformToLegacy(normalizer, delegate.getCreatedData());
190         }
191
192
193         @Override
194         public Set<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier> getRemovedConfigurationData() {
195             return delegate.getRemovedPaths();
196         }
197
198         @Override
199         public CompositeNode getOriginalConfigurationSubtree() {
200             // first argument is unused
201             return normalizer.toLegacy(null, delegate.getOriginalSubtree());
202         }
203
204         @Override
205         public CompositeNode getUpdatedConfigurationSubtree() {
206             // first argument is unused
207             return normalizer.toLegacy(null, delegate.getUpdatedSubtree());
208         }
209
210         @Override
211         public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getOriginalConfigurationData() {
212             return transformToLegacy(normalizer, delegate.getOriginalData());
213         }
214
215         @Override
216         public Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> getUpdatedConfigurationData() {
217             if(updatedCache == null) {
218                 final Map<InstanceIdentifier, CompositeNode> updated = transformToLegacy(normalizer, delegate.getUpdatedData());
219                 final Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> created = getCreatedConfigurationData();
220                 final HashMap<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, CompositeNode> updatedComposite = new HashMap<>(created.size() + updated.size());
221                 updatedComposite.putAll(created);
222                 updatedComposite.putAll(updated);
223                 updatedCache = Collections.unmodifiableMap(updatedComposite);
224             }
225             return updatedCache;
226         }
227
228         @Override
229         public String toString() {
230             return "ConfigurationChangeEvent [delegate=" + delegate + "]";
231         }
232     }
233 }