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

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