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

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