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

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