Merge "Bug 849: Fixed NPE in Translated Data Change Events."
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / AbstractForwardedDataBroker.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.binding.impl;
9
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import java.util.Set;
16
17 import org.opendaylight.controller.md.sal.binding.api.BindingDataChangeListener;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
23 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentConnector;
24 import org.opendaylight.controller.sal.binding.impl.forward.DomForwardedBroker;
25 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
26 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
27 import org.opendaylight.yangtools.concepts.Delegator;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
33 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import com.google.common.base.Objects;
40 import com.google.common.base.Optional;
41
42 public abstract class AbstractForwardedDataBroker implements Delegator<DOMDataBroker>, DomForwardedBroker,
43         SchemaContextListener {
44
45     private static final Logger LOG = LoggerFactory.getLogger(AbstractForwardedDataBroker.class);
46     // The Broker to whom we do all forwarding
47     private final DOMDataBroker domDataBroker;
48
49     // Mapper to convert from Binding Independent objects to Binding Aware
50     // objects
51     private final BindingIndependentMappingService mappingService;
52
53     private final BindingToNormalizedNodeCodec codec;
54     private BindingIndependentConnector connector;
55     private ProviderSession context;
56
57     protected AbstractForwardedDataBroker(final DOMDataBroker domDataBroker,
58             final BindingIndependentMappingService mappingService) {
59         this.domDataBroker = domDataBroker;
60         this.mappingService = mappingService;
61         this.codec = new BindingToNormalizedNodeCodec(mappingService);
62     }
63
64     protected BindingToNormalizedNodeCodec getCodec() {
65         return codec;
66     }
67
68     protected BindingIndependentMappingService getMappingService() {
69         return mappingService;
70     }
71
72     @Override
73     public DOMDataBroker getDelegate() {
74         return domDataBroker;
75     }
76
77     @Override
78     public void onGlobalContextUpdated(final SchemaContext ctx) {
79         codec.onGlobalContextUpdated(ctx);
80     }
81
82     public ListenerRegistration<BindingDataChangeListener> registerDataChangeListener(final LogicalDatastoreType store,
83             final InstanceIdentifier<?> path, final BindingDataChangeListener listener,
84             final DataChangeScope triggeringScope) {
85         DOMDataChangeListener domDataChangeListener = new TranslatingDataChangeInvoker(store, path, listener,
86                 triggeringScope);
87         org.opendaylight.yangtools.yang.data.api.InstanceIdentifier domPath = codec.toNormalized(path);
88         ListenerRegistration<DOMDataChangeListener> domRegistration = domDataBroker.registerDataChangeListener(store,
89                 domPath, domDataChangeListener, triggeringScope);
90         return new ListenerRegistrationImpl(listener, domRegistration);
91     }
92
93     protected Map<InstanceIdentifier<?>, DataObject> toBinding(
94             final Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, ? extends NormalizedNode<?, ?>> normalized) {
95         Map<InstanceIdentifier<?>, DataObject> newMap = new HashMap<>();
96         for (Map.Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, ? extends NormalizedNode<?, ?>> entry : normalized
97                 .entrySet()) {
98             try {
99                 Entry<InstanceIdentifier<? extends DataObject>, DataObject> binding = getCodec().toBinding(entry);
100                 newMap.put(binding.getKey(), binding.getValue());
101             } catch (DeserializationException e) {
102                 LOG.warn("Failed to transform {}, omitting it", entry, e);
103             }
104         }
105         return newMap;
106     }
107
108     protected Set<InstanceIdentifier<?>> toBinding(
109             final Set<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier> normalized) {
110         Set<InstanceIdentifier<?>> hashSet = new HashSet<>();
111         for (org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalizedPath : normalized) {
112             try {
113                 InstanceIdentifier<? extends DataObject> binding = getCodec().toBinding(normalizedPath);
114                 hashSet.add(binding);
115             } catch (DeserializationException e) {
116                 LOG.warn("Failed to transform {}, omitting it", normalizedPath, e);
117             }
118         }
119         return hashSet;
120     }
121
122     protected Optional<DataObject> toBindingData(final InstanceIdentifier<?> path, final NormalizedNode<?, ?> data) {
123         if(path.isWildcarded()) {
124             return Optional.absent();
125         }
126
127         try {
128             return Optional.fromNullable(getCodec().toBinding(path, data));
129         } catch (DeserializationException e) {
130             return Optional.absent();
131         }
132     }
133
134     private class TranslatingDataChangeInvoker implements DOMDataChangeListener {
135         private final BindingDataChangeListener bindingDataChangeListener;
136         private final LogicalDatastoreType store;
137         private final InstanceIdentifier<?> path;
138         private final DataChangeScope triggeringScope;
139
140         public TranslatingDataChangeInvoker(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
141                 final BindingDataChangeListener bindingDataChangeListener, final DataChangeScope triggeringScope) {
142             this.store = store;
143             this.path = path;
144             this.bindingDataChangeListener = bindingDataChangeListener;
145             this.triggeringScope = triggeringScope;
146         }
147
148         @Override
149         public void onDataChanged(
150                 final AsyncDataChangeEvent<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> change) {
151             bindingDataChangeListener.onDataChanged(new TranslatedDataChangeEvent(change, path));
152         }
153     }
154
155     private class TranslatedDataChangeEvent implements AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> {
156         private final AsyncDataChangeEvent<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> domEvent;
157         private final InstanceIdentifier<?> path;
158
159         private Map<InstanceIdentifier<?>, DataObject> createdCache;
160         private Map<InstanceIdentifier<?>, DataObject> updatedCache;
161         private Map<InstanceIdentifier<?>, ? extends DataObject> originalCache;
162         private Set<InstanceIdentifier<?>> removedCache;
163         private Optional<DataObject> originalDataCache;
164         private Optional<DataObject> updatedDataCache;
165
166         public TranslatedDataChangeEvent(
167                 final AsyncDataChangeEvent<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> change,
168                 final InstanceIdentifier<?> path) {
169             this.domEvent = change;
170             this.path = path;
171         }
172
173         @Override
174         public Map<InstanceIdentifier<?>, DataObject> getCreatedData() {
175             if (createdCache == null) {
176                 createdCache = Collections.unmodifiableMap(toBinding(domEvent.getCreatedData()));
177             }
178             return createdCache;
179         }
180
181         @Override
182         public Map<InstanceIdentifier<?>, DataObject> getUpdatedData() {
183             if (updatedCache == null) {
184                 updatedCache = Collections.unmodifiableMap(toBinding(domEvent.getUpdatedData()));
185             }
186             return updatedCache;
187
188         }
189
190         @Override
191         public Set<InstanceIdentifier<?>> getRemovedPaths() {
192             if (removedCache == null) {
193                 removedCache = Collections.unmodifiableSet(toBinding(domEvent.getRemovedPaths()));
194             }
195             return removedCache;
196         }
197
198         @Override
199         public Map<InstanceIdentifier<?>, ? extends DataObject> getOriginalData() {
200             if (originalCache == null) {
201                 originalCache = Collections.unmodifiableMap(toBinding(domEvent.getOriginalData()));
202             }
203             return originalCache;
204
205         }
206
207         @Override
208         public DataObject getOriginalSubtree() {
209             if (originalDataCache == null) {
210                 if(domEvent.getOriginalSubtree() != null) {
211                     originalDataCache = toBindingData(path, domEvent.getOriginalSubtree());
212                 } else {
213                     originalDataCache = Optional.absent();
214                 }
215             }
216             return originalDataCache.orNull();
217         }
218
219         @Override
220         public DataObject getUpdatedSubtree() {
221             if (updatedDataCache == null) {
222                 if(domEvent.getUpdatedSubtree() != null) {
223                     updatedDataCache = toBindingData(path, domEvent.getUpdatedSubtree());
224                 } else {
225                     updatedDataCache = Optional.absent();
226                 }
227             }
228             return updatedDataCache.orNull();
229         }
230
231         @Override
232         public String toString() {
233             return Objects.toStringHelper(TranslatedDataChangeEvent.class) //
234                 .add("created", getCreatedData()) //
235                 .add("updated", getUpdatedData()) //
236                 .add("removed", getRemovedPaths()) //
237                 .add("dom", domEvent) //
238                 .toString();
239         }
240     }
241
242     private static class ListenerRegistrationImpl extends AbstractListenerRegistration<BindingDataChangeListener> {
243         private final ListenerRegistration<DOMDataChangeListener> registration;
244
245         public ListenerRegistrationImpl(final BindingDataChangeListener listener,
246                 final ListenerRegistration<DOMDataChangeListener> registration) {
247             super(listener);
248             this.registration = registration;
249         }
250
251         @Override
252         protected void removeRegistration() {
253             registration.close();
254         }
255     }
256
257     @Override
258     public BindingIndependentConnector getConnector() {
259         return this.connector;
260     }
261
262     @Override
263     public ProviderSession getDomProviderContext() {
264         return this.context;
265     }
266
267     @Override
268     public void setConnector(final BindingIndependentConnector connector) {
269         this.connector = connector;
270     }
271
272     @Override
273     public void setDomProviderContext(final ProviderSession domProviderContext) {
274         this.context = domProviderContext;
275     }
276
277     @Override
278     public void startForwarding() {
279         // NOOP
280     }
281
282 }

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