Bug 1245: Dropped Binding prefix from Binding Data APIs.
[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.DataChangeListener;
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.controller.sal.core.api.model.SchemaService;
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.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
34 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
37 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import com.google.common.base.Objects;
42 import com.google.common.base.Optional;
43
44 public abstract class AbstractForwardedDataBroker implements Delegator<DOMDataBroker>, DomForwardedBroker,
45         SchemaContextListener, AutoCloseable {
46
47     private static final Logger LOG = LoggerFactory.getLogger(AbstractForwardedDataBroker.class);
48     // The Broker to whom we do all forwarding
49     private final DOMDataBroker domDataBroker;
50
51     // Mapper to convert from Binding Independent objects to Binding Aware
52     // objects
53     private final BindingIndependentMappingService mappingService;
54
55     private final BindingToNormalizedNodeCodec codec;
56     private BindingIndependentConnector connector;
57     private ProviderSession context;
58     private final ListenerRegistration<SchemaServiceListener> schemaListenerRegistration;
59
60     protected AbstractForwardedDataBroker(final DOMDataBroker domDataBroker,
61             final BindingIndependentMappingService mappingService,final SchemaService schemaService) {
62         this.domDataBroker = domDataBroker;
63         this.mappingService = mappingService;
64         this.codec = new BindingToNormalizedNodeCodec(mappingService);
65         this.schemaListenerRegistration = schemaService.registerSchemaServiceListener(this);
66     }
67
68     protected BindingToNormalizedNodeCodec getCodec() {
69         return codec;
70     }
71
72     protected BindingIndependentMappingService getMappingService() {
73         return mappingService;
74     }
75
76     @Override
77     public DOMDataBroker getDelegate() {
78         return domDataBroker;
79     }
80
81     @Override
82     public void onGlobalContextUpdated(final SchemaContext ctx) {
83         codec.onGlobalContextUpdated(ctx);
84     }
85
86     public ListenerRegistration<DataChangeListener> registerDataChangeListener(final LogicalDatastoreType store,
87             final InstanceIdentifier<?> path, final DataChangeListener listener,
88             final DataChangeScope triggeringScope) {
89         DOMDataChangeListener domDataChangeListener = new TranslatingDataChangeInvoker(store, path, listener,
90                 triggeringScope);
91         org.opendaylight.yangtools.yang.data.api.InstanceIdentifier domPath = codec.toNormalized(path);
92         ListenerRegistration<DOMDataChangeListener> domRegistration = domDataBroker.registerDataChangeListener(store,
93                 domPath, domDataChangeListener, triggeringScope);
94         return new ListenerRegistrationImpl(listener, domRegistration);
95     }
96
97     protected Map<InstanceIdentifier<?>, DataObject> toBinding(
98             final Map<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, ? extends NormalizedNode<?, ?>> normalized) {
99         Map<InstanceIdentifier<?>, DataObject> newMap = new HashMap<>();
100         for (Map.Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, ? extends NormalizedNode<?, ?>> entry : normalized
101                 .entrySet()) {
102             try {
103                 Optional<Entry<InstanceIdentifier<? extends DataObject>, DataObject>> potential = getCodec().toBinding(
104                         entry);
105                 if (potential.isPresent()) {
106                     Entry<InstanceIdentifier<? extends DataObject>, DataObject> binding = potential.get();
107                     newMap.put(binding.getKey(), binding.getValue());
108                 }
109             } catch (DeserializationException e) {
110                 LOG.warn("Failed to transform {}, omitting it", entry, e);
111             }
112         }
113         return newMap;
114     }
115
116     protected Set<InstanceIdentifier<?>> toBinding(
117             final Set<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier> normalized) {
118         Set<InstanceIdentifier<?>> hashSet = new HashSet<>();
119         for (org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalizedPath : normalized) {
120             try {
121                 Optional<InstanceIdentifier<? extends DataObject>> potential = getCodec().toBinding(normalizedPath);
122                 if (potential.isPresent()) {
123                     InstanceIdentifier<? extends DataObject> binding = potential.get();
124                     hashSet.add(binding);
125                 }
126             } catch (DeserializationException e) {
127                 LOG.warn("Failed to transform {}, omitting it", normalizedPath, e);
128             }
129         }
130         return hashSet;
131     }
132
133     protected Optional<DataObject> toBindingData(final InstanceIdentifier<?> path, final NormalizedNode<?, ?> data) {
134         if (path.isWildcarded()) {
135             return Optional.absent();
136         }
137
138         try {
139             return Optional.fromNullable(getCodec().toBinding(path, data));
140         } catch (DeserializationException e) {
141             return Optional.absent();
142         }
143     }
144
145     private class TranslatingDataChangeInvoker implements DOMDataChangeListener {
146         private final DataChangeListener bindingDataChangeListener;
147         private final LogicalDatastoreType store;
148         private final InstanceIdentifier<?> path;
149         private final DataChangeScope triggeringScope;
150
151         public TranslatingDataChangeInvoker(final LogicalDatastoreType store, final InstanceIdentifier<?> path,
152                 final DataChangeListener bindingDataChangeListener, final DataChangeScope triggeringScope) {
153             this.store = store;
154             this.path = path;
155             this.bindingDataChangeListener = bindingDataChangeListener;
156             this.triggeringScope = triggeringScope;
157         }
158
159         @Override
160         public void onDataChanged(
161                 final AsyncDataChangeEvent<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> change) {
162             bindingDataChangeListener.onDataChanged(new TranslatedDataChangeEvent(change, path));
163         }
164     }
165
166     private class TranslatedDataChangeEvent implements AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> {
167         private final AsyncDataChangeEvent<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> domEvent;
168         private final InstanceIdentifier<?> path;
169
170         private Map<InstanceIdentifier<?>, DataObject> createdCache;
171         private Map<InstanceIdentifier<?>, DataObject> updatedCache;
172         private Map<InstanceIdentifier<?>, ? extends DataObject> originalCache;
173         private Set<InstanceIdentifier<?>> removedCache;
174         private Optional<DataObject> originalDataCache;
175         private Optional<DataObject> updatedDataCache;
176
177         public TranslatedDataChangeEvent(
178                 final AsyncDataChangeEvent<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, 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(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(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(domEvent.getRemovedPaths()));
205             }
206             return removedCache;
207         }
208
209         @Override
210         public Map<InstanceIdentifier<?>, ? extends DataObject> getOriginalData() {
211             if (originalCache == null) {
212                 originalCache = Collections.unmodifiableMap(toBinding(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 Objects.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 BindingIndependentConnector getConnector() {
270         return this.connector;
271     }
272
273     @Override
274     public ProviderSession getDomProviderContext() {
275         return this.context;
276     }
277
278     @Override
279     public void setConnector(final BindingIndependentConnector connector) {
280         this.connector = connector;
281     }
282
283     @Override
284     public void setDomProviderContext(final ProviderSession domProviderContext) {
285         this.context = domProviderContext;
286     }
287
288     @Override
289     public void startForwarding() {
290         // NOOP
291     }
292
293     @Override
294     public void close() throws Exception {
295         this.schemaListenerRegistration.close();
296     }
297
298 }

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