BUG-1092: adjust to YangInstanceIdentifier
[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.YangInstanceIdentifier;
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.YangInstanceIdentifier, CompositeNode> {
27
28     private TranslatingDataChangeEvent() {
29     }
30
31     public static DataChangeEvent<YangInstanceIdentifier, CompositeNode> createOperational(
32             final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change, final DataNormalizer normalizer) {
33         return new OperationalChangeEvent(change, normalizer);
34     }
35
36     public static DataChangeEvent<YangInstanceIdentifier, CompositeNode> createConfiguration(
37             final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change, final DataNormalizer normalizer) {
38         return new ConfigurationChangeEvent(change, normalizer);
39     }
40
41     @Override
42     public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getCreatedOperationalData() {
43         return Collections.emptyMap();
44     }
45
46     @Override
47     public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getCreatedConfigurationData() {
48         return Collections.emptyMap();
49     }
50
51     @Override
52     public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getUpdatedOperationalData() {
53         return Collections.emptyMap();
54     }
55
56     @Override
57     public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getUpdatedConfigurationData() {
58         return Collections.emptyMap();
59     }
60
61     @Override
62     public Set<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier> getRemovedConfigurationData() {
63         return Collections.emptySet();
64     }
65
66     @Override
67     public Set<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier> getRemovedOperationalData() {
68         return Collections.emptySet();
69     }
70
71     @Override
72     public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getOriginalConfigurationData() {
73         return Collections.emptyMap();
74     }
75
76     @Override
77     public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, 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     private final static class OperationalChangeEvent extends TranslatingDataChangeEvent {
102
103         private final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> delegate;
104         private final DataNormalizer normalizer;
105         private Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> updatedCache;
106
107         public OperationalChangeEvent(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change,
108                 final DataNormalizer normalizer) {
109             this.delegate = change;
110             this.normalizer = normalizer;
111         }
112
113         @Override
114         public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getCreatedOperationalData() {
115             return transformToLegacy(normalizer, delegate.getCreatedData());
116         }
117
118
119         @Override
120         public Set<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier> getRemovedOperationalData() {
121             return delegate.getRemovedPaths();
122         }
123
124         @Override
125         public CompositeNode getOriginalOperationalSubtree() {
126             // first argument is unused
127             return normalizer.toLegacy(null, delegate.getOriginalSubtree());
128         }
129
130         @Override
131         public CompositeNode getUpdatedOperationalSubtree() {
132             // first argument is unused
133             return normalizer.toLegacy(null, delegate.getUpdatedSubtree());
134         }
135
136         @Override
137         public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getOriginalOperationalData() {
138             return transformToLegacy(normalizer, delegate.getOriginalData());
139         }
140
141         @Override
142         public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getUpdatedOperationalData() {
143             if(updatedCache == null) {
144                 final Map<YangInstanceIdentifier, CompositeNode> updated = transformToLegacy(normalizer, delegate.getUpdatedData());
145                 final Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> created = getCreatedConfigurationData();
146                 final HashMap<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> updatedComposite = new HashMap<>(created.size() + updated.size());
147                 updatedComposite.putAll(created);
148                 updatedComposite.putAll(updated);
149                 updatedCache = Collections.unmodifiableMap(updatedComposite);
150             }
151             return updatedCache;
152         }
153
154         @Override
155         public String toString() {
156             return "OperationalChangeEvent [delegate=" + delegate + "]";
157         }
158
159     }
160
161     private static Map<YangInstanceIdentifier, CompositeNode> transformToLegacy(final DataNormalizer normalizer, final Map<YangInstanceIdentifier, ? extends NormalizedNode<?, ?>> nodes) {
162         final Map<YangInstanceIdentifier, CompositeNode> legacy = Maps.newHashMap();
163
164         for (final Map.Entry<YangInstanceIdentifier, ? extends NormalizedNode<?, ?>> entry : nodes.entrySet()) {
165             try {
166                 legacy.put(normalizer.toLegacy(entry.getKey()), normalizer.toLegacy(entry.getKey(), entry.getValue()));
167             } catch (final DataNormalizationException e) {
168                 throw new IllegalStateException("Unable to transform data change event to legacy format", e);
169             }
170         }
171         return legacy;
172     }
173
174     private final static class ConfigurationChangeEvent extends TranslatingDataChangeEvent {
175
176         private final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> delegate;
177         private final DataNormalizer normalizer;
178         private Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> updatedCache;
179
180         public ConfigurationChangeEvent(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change,
181                 final DataNormalizer normalizer) {
182             this.delegate = change;
183             this.normalizer = normalizer;
184         }
185
186         @Override
187         public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getCreatedConfigurationData() {
188             return transformToLegacy(normalizer, delegate.getCreatedData());
189         }
190
191
192         @Override
193         public Set<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier> getRemovedConfigurationData() {
194             return delegate.getRemovedPaths();
195         }
196
197         @Override
198         public CompositeNode getOriginalConfigurationSubtree() {
199             // first argument is unused
200             return normalizer.toLegacy(null, delegate.getOriginalSubtree());
201         }
202
203         @Override
204         public CompositeNode getUpdatedConfigurationSubtree() {
205             // first argument is unused
206             return normalizer.toLegacy(null, delegate.getUpdatedSubtree());
207         }
208
209         @Override
210         public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getOriginalConfigurationData() {
211             return transformToLegacy(normalizer, delegate.getOriginalData());
212         }
213
214         @Override
215         public Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> getUpdatedConfigurationData() {
216             if(updatedCache == null) {
217                 final Map<YangInstanceIdentifier, CompositeNode> updated = transformToLegacy(normalizer, delegate.getUpdatedData());
218                 final Map<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> created = getCreatedConfigurationData();
219                 final HashMap<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> updatedComposite = new HashMap<>(created.size() + updated.size());
220                 updatedComposite.putAll(created);
221                 updatedComposite.putAll(updated);
222                 updatedCache = Collections.unmodifiableMap(updatedComposite);
223             }
224             return updatedCache;
225         }
226
227         @Override
228         public String toString() {
229             return "ConfigurationChangeEvent [delegate=" + delegate + "]";
230         }
231     }
232 }

©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.