273155bcf71a275b7335ad322cde99b92191922a
[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.Objects;
11 import com.google.common.base.Optional;
12
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Set;
19
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.sal.binding.impl.connect.dom.BindingIndependentConnector;
27 import org.opendaylight.controller.sal.binding.impl.forward.DomForwardedBroker;
28 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
29 import org.opendaylight.controller.sal.core.api.model.SchemaService;
30 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
31 import org.opendaylight.yangtools.concepts.Delegator;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public abstract class AbstractForwardedDataBroker implements Delegator<DOMDataBroker>, DomForwardedBroker,
44         SchemaContextListener, AutoCloseable {
45
46     private static final Logger LOG = LoggerFactory.getLogger(AbstractForwardedDataBroker.class);
47     // The Broker to whom we do all forwarding
48     private final DOMDataBroker domDataBroker;
49
50     private final BindingToNormalizedNodeCodec codec;
51     private BindingIndependentConnector connector;
52     private ProviderSession context;
53     private final ListenerRegistration<SchemaContextListener> schemaListenerRegistration;
54
55     protected AbstractForwardedDataBroker(final DOMDataBroker domDataBroker, final BindingToNormalizedNodeCodec codec,
56             final SchemaService schemaService) {
57         this.domDataBroker = domDataBroker;
58         this.codec = codec;
59         this.schemaListenerRegistration = schemaService.registerSchemaContextListener(this);
60     }
61
62     protected BindingToNormalizedNodeCodec getCodec() {
63         return codec;
64     }
65
66     @Override
67     public DOMDataBroker getDelegate() {
68         return domDataBroker;
69     }
70
71     @Override
72     public void onGlobalContextUpdated(final SchemaContext ctx) {
73         // NOOP
74     }
75
76     public ListenerRegistration<DataChangeListener> registerDataChangeListener(final LogicalDatastoreType store,
77             final InstanceIdentifier<?> path, final DataChangeListener listener, final DataChangeScope triggeringScope) {
78         DOMDataChangeListener domDataChangeListener = new TranslatingDataChangeInvoker(store, path, listener,
79                 triggeringScope);
80         YangInstanceIdentifier domPath = codec.toNormalized(path);
81         ListenerRegistration<DOMDataChangeListener> domRegistration = domDataBroker.registerDataChangeListener(store,
82                 domPath, domDataChangeListener, triggeringScope);
83         return new ListenerRegistrationImpl(listener, domRegistration);
84     }
85
86     protected Map<InstanceIdentifier<?>, DataObject> toBinding(final InstanceIdentifier<?> path,
87             final Map<YangInstanceIdentifier, ? extends NormalizedNode<?, ?>> normalized) {
88         Map<InstanceIdentifier<?>, DataObject> newMap = new HashMap<>();
89
90         for (Map.Entry<YangInstanceIdentifier, ? extends NormalizedNode<?, ?>> entry : normalized.entrySet()) {
91             try {
92                 Optional<Entry<InstanceIdentifier<? extends DataObject>, DataObject>> potential = getCodec().toBinding(entry);
93                 if (potential.isPresent()) {
94                     Entry<InstanceIdentifier<? extends DataObject>, DataObject> binding = potential.get();
95                     newMap.put(binding.getKey(), binding.getValue());
96                 }
97             } catch (DeserializationException e) {
98                 LOG.warn("Failed to transform {}, omitting it", entry, e);
99             }
100         }
101         return newMap;
102     }
103
104     protected Set<InstanceIdentifier<?>> toBinding(final InstanceIdentifier<?> path,
105             final Set<YangInstanceIdentifier> normalized) {
106         Set<InstanceIdentifier<?>> hashSet = new HashSet<>();
107         for (YangInstanceIdentifier normalizedPath : normalized) {
108             try {
109                 Optional<InstanceIdentifier<? extends DataObject>> potential = getCodec().toBinding(normalizedPath);
110                 if (potential.isPresent()) {
111                     InstanceIdentifier<? extends DataObject> binding = potential.get();
112                     hashSet.add(binding);
113                 } else if (normalizedPath.getLastPathArgument() instanceof YangInstanceIdentifier.AugmentationIdentifier) {
114                     hashSet.add(path);
115                 }
116             } catch (DeserializationException e) {
117                 LOG.warn("Failed to transform {}, omitting it", normalizedPath, e);
118             }
119         }
120         return hashSet;
121     }
122
123     protected Optional<DataObject> toBindingData(final InstanceIdentifier<?> path, final NormalizedNode<?, ?> data) {
124         if (path.isWildcarded()) {
125             return Optional.absent();
126         }
127         return (Optional<DataObject>) getCodec().deserializeFunction(path).apply(Optional.<NormalizedNode<?, ?>> of(data));
128     }
129
130     private class TranslatingDataChangeInvoker implements DOMDataChangeListener {
131         private final DataChangeListener bindingDataChangeListener;
132         private final LogicalDatastoreType store;
133         private final InstanceIdentifier<?> path;
134         private final DataChangeScope triggeringScope;
135
136         public TranslatingDataChangeInvoker(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
137                 final DataChangeListener bindingDataChangeListener, final DataChangeScope triggeringScope) {
138             this.store = store;
139             this.path = path;
140             this.bindingDataChangeListener = bindingDataChangeListener;
141             this.triggeringScope = triggeringScope;
142         }
143
144         @Override
145         public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
146             bindingDataChangeListener.onDataChanged(new TranslatedDataChangeEvent(change, path));
147         }
148
149         @Override
150         public String toString() {
151             return bindingDataChangeListener.getClass().getName();
152         }
153     }
154
155     private class TranslatedDataChangeEvent implements AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> {
156         private final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> domEvent;
157         private final InstanceIdentifier<?> path;
158
159         private Map<InstanceIdentifier<?>, DataObject> createdCache;
160         private Map<InstanceIdentifier<?>, DataObject> updatedCache;
161         private Map<InstanceIdentifier<?>, DataObject> originalCache;
162         private Set<InstanceIdentifier<?>> removedCache;
163         private Optional<DataObject> originalDataCache;
164         private Optional<DataObject> updatedDataCache;
165
166         public TranslatedDataChangeEvent(
167                 final AsyncDataChangeEvent<YangInstanceIdentifier, 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(path, 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(path, 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(path, domEvent.getRemovedPaths()));
194             }
195             return removedCache;
196         }
197
198         @Override
199         public Map<InstanceIdentifier<?>, DataObject> getOriginalData() {
200             if (originalCache == null) {
201                 originalCache = Collections.unmodifiableMap(toBinding(path, 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<DataChangeListener> {
243         private final ListenerRegistration<DOMDataChangeListener> registration;
244
245         public ListenerRegistrationImpl(final DataChangeListener 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     @Override
283     public void close() throws Exception {
284         this.schemaListenerRegistration.close();
285     }
286
287 }

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