2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.binding.compat;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.HashSet;
18 import java.util.concurrent.ExecutionException;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
24 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
27 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
28 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
29 import org.opendaylight.yangtools.concepts.Delegator;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.util.ListenerRegistry;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 public class HydrogenDataBrokerAdapter implements DataProviderService, AutoCloseable {
41 private static final Logger LOG = LoggerFactory.getLogger(HydrogenDataBrokerAdapter.class);
43 private final DataBroker delegate;
45 public HydrogenDataBrokerAdapter(final DataBroker dataBroker) {
46 delegate = dataBroker;
47 LOG.info("ForwardedBackwardsCompatibleBroker started.");
51 public DataModificationTransaction beginTransaction() {
52 return new ForwardedBackwardsCompatibleTransacion(delegate.newReadWriteTransaction());
56 public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
57 final DataModificationTransaction tx = beginTransaction();
58 return tx.readConfigurationData(path);
62 public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
63 final DataModificationTransaction tx = beginTransaction();
64 return tx.readOperationalData(path);
68 public ListenerRegistration<DataChangeListener> registerDataChangeListener(
69 final InstanceIdentifier<? extends DataObject> path, final DataChangeListener listener) {
72 final org.opendaylight.controller.md.sal.binding.api.DataChangeListener asyncOperListener = new BackwardsCompatibleOperationalDataChangeInvoker(listener);
73 final org.opendaylight.controller.md.sal.binding.api.DataChangeListener asyncCfgListener = new BackwardsCompatibleConfigurationDataChangeInvoker(listener);
75 final ListenerRegistration<org.opendaylight.controller.md.sal.binding.api.DataChangeListener> cfgReg = delegate.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, path, asyncCfgListener, DataChangeScope.SUBTREE);
76 final ListenerRegistration<org.opendaylight.controller.md.sal.binding.api.DataChangeListener> operReg = delegate.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, path, asyncOperListener, DataChangeScope.SUBTREE);
78 return new LegacyListenerRegistration(listener,cfgReg,operReg);
82 private class ForwardedBackwardsCompatibleTransacion implements DataModificationTransaction {
84 private final ListenerRegistry<DataTransactionListener> listeners = ListenerRegistry.create();
85 private final Map<InstanceIdentifier<? extends DataObject>, DataObject> updated = new HashMap<>();
86 private final Map<InstanceIdentifier<? extends DataObject>, DataObject> created = new HashMap<>();
87 private final Set<InstanceIdentifier<? extends DataObject>> removed = new HashSet<>();
88 private final Map<InstanceIdentifier<? extends DataObject>, DataObject> original = new HashMap<>();
89 private TransactionStatus status = TransactionStatus.NEW;
91 private final Set<InstanceIdentifier<? extends DataObject>> posponedRemovedOperational = new HashSet<>();
92 private final Set<InstanceIdentifier<? extends DataObject>> posponedRemovedConfiguration = new HashSet<>();
94 private final ReadWriteTransaction delegate;
98 public final TransactionStatus getStatus() {
102 protected ForwardedBackwardsCompatibleTransacion(final ReadWriteTransaction delegate) {
103 this.delegate = delegate;
104 LOG.debug("Tx {} allocated.",getIdentifier());
108 public void putOperationalData(final InstanceIdentifier<? extends DataObject> path, final DataObject data) {
109 final boolean previouslyRemoved = posponedRemovedOperational.remove(path);
111 @SuppressWarnings({ "rawtypes", "unchecked" })
112 final InstanceIdentifier<DataObject> castedPath = (InstanceIdentifier) path;
113 if(previouslyRemoved) {
114 delegate.put(LogicalDatastoreType.OPERATIONAL, castedPath, data,true);
116 delegate.merge(LogicalDatastoreType.OPERATIONAL, castedPath, data,true);
121 public void putConfigurationData(final InstanceIdentifier<? extends DataObject> path, final DataObject data) {
122 final boolean previouslyRemoved = posponedRemovedConfiguration.remove(path);
123 final DataObject originalObj = readConfigurationData(path);
124 if (originalObj != null) {
125 original.put(path, originalObj);
128 created.put(path, data);
130 updated.put(path, data);
131 @SuppressWarnings({"rawtypes","unchecked"})
132 final InstanceIdentifier<DataObject> castedPath = (InstanceIdentifier) path;
133 if(previouslyRemoved) {
134 delegate.put(LogicalDatastoreType.CONFIGURATION, castedPath, data,true);
136 delegate.merge(LogicalDatastoreType.CONFIGURATION, castedPath, data,true);
141 public void removeOperationalData(final InstanceIdentifier<? extends DataObject> path) {
142 posponedRemovedOperational.add(path);
146 public void removeConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
147 posponedRemovedConfiguration.add(path);
151 public Map<InstanceIdentifier<? extends DataObject>, DataObject> getCreatedOperationalData() {
152 return Collections.emptyMap();
156 public Map<InstanceIdentifier<? extends DataObject>, DataObject> getCreatedConfigurationData() {
161 public Map<InstanceIdentifier<? extends DataObject>, DataObject> getUpdatedOperationalData() {
162 return Collections.emptyMap();
166 public Map<InstanceIdentifier<? extends DataObject>, DataObject> getUpdatedConfigurationData() {
171 public Set<InstanceIdentifier<? extends DataObject>> getRemovedConfigurationData() {
176 public Set<InstanceIdentifier<? extends DataObject>> getRemovedOperationalData() {
177 return Collections.emptySet();
181 public Map<InstanceIdentifier<? extends DataObject>, DataObject> getOriginalConfigurationData() {
186 public Map<InstanceIdentifier<? extends DataObject>, DataObject> getOriginalOperationalData() {
187 return Collections.emptyMap();
191 public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
193 return delegate.read(LogicalDatastoreType.OPERATIONAL, path).get().orNull();
194 } catch (InterruptedException | ExecutionException e) {
195 LOG.error("Read of {} failed.", path,e);
201 public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
203 return delegate.read(LogicalDatastoreType.CONFIGURATION, path).get().orNull();
204 } catch (InterruptedException | ExecutionException e) {
205 LOG.error("Read of {} failed.", path,e);
210 private void changeStatus(final TransactionStatus status) {
211 LOG.trace("Transaction {} changed status to {}", getIdentifier(), status);
212 this.status = status;
214 for(final ListenerRegistration<DataTransactionListener> listener : listeners) {
216 listener.getInstance().onStatusUpdated(this, status);
217 } catch (final Exception e) {
218 LOG.error("Error during invoking transaction listener {}",listener.getInstance(),e);
224 public ListenableFuture<RpcResult<TransactionStatus>> commit() {
226 for(final InstanceIdentifier<? extends DataObject> path : posponedRemovedConfiguration) {
227 delegate.delete(LogicalDatastoreType.CONFIGURATION, path);
230 for(final InstanceIdentifier<? extends DataObject> path : posponedRemovedOperational) {
231 delegate.delete(LogicalDatastoreType.OPERATIONAL, path);
234 changeStatus(TransactionStatus.SUBMITED);
236 final ListenableFuture<RpcResult<TransactionStatus>> f = delegate.commit();
238 Futures.addCallback(f, new FutureCallback<RpcResult<TransactionStatus>>() {
240 public void onSuccess(final RpcResult<TransactionStatus> result) {
241 changeStatus(result.getResult());
245 public void onFailure(final Throwable t) {
246 LOG.error("Transaction {} failed to complete", getIdentifier(), t);
247 changeStatus(TransactionStatus.FAILED);
255 public ListenerRegistration<DataTransactionListener> registerListener(final DataTransactionListener listener) {
256 return listeners.register(listener);
260 public Object getIdentifier() {
261 // TODO Auto-generated method stub
267 private static final class LegacyListenerRegistration implements ListenerRegistration<DataChangeListener> {
269 private final DataChangeListener instance;
270 private final ListenerRegistration<org.opendaylight.controller.md.sal.binding.api.DataChangeListener> cfgReg;
271 private final ListenerRegistration<org.opendaylight.controller.md.sal.binding.api.DataChangeListener> operReg;
273 public LegacyListenerRegistration(final DataChangeListener listener,
274 final ListenerRegistration<org.opendaylight.controller.md.sal.binding.api.DataChangeListener> cfgReg,
275 final ListenerRegistration<org.opendaylight.controller.md.sal.binding.api.DataChangeListener> operReg) {
276 this.instance = listener;
277 this.cfgReg = cfgReg;
278 this.operReg = operReg;
282 public DataChangeListener getInstance() {
287 public void close() {
294 private static class BackwardsCompatibleOperationalDataChangeInvoker implements org.opendaylight.controller.md.sal.binding.api.DataChangeListener, Delegator<DataChangeListener> {
296 private final org.opendaylight.controller.md.sal.common.api.data.DataChangeListener<?,?> delegate;
299 public BackwardsCompatibleOperationalDataChangeInvoker(final DataChangeListener listener) {
300 this.delegate = listener;
303 @SuppressWarnings({ "unchecked", "rawtypes" })
305 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
307 final DataChangeEvent legacyChange = HydrogenDataChangeEvent.createOperational(change);
308 delegate.onDataChanged(legacyChange);
313 public DataChangeListener getDelegate() {
314 return (DataChangeListener) delegate;
319 private static class BackwardsCompatibleConfigurationDataChangeInvoker implements org.opendaylight.controller.md.sal.binding.api.DataChangeListener, Delegator<DataChangeListener> {
320 private final org.opendaylight.controller.md.sal.common.api.data.DataChangeListener<?,?> delegate;
322 public BackwardsCompatibleConfigurationDataChangeInvoker(final DataChangeListener listener) {
323 this.delegate = listener;
326 @SuppressWarnings({ "unchecked", "rawtypes" })
328 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
330 final DataChangeEvent legacyChange = HydrogenDataChangeEvent.createConfiguration(change);
332 delegate.onDataChanged(legacyChange);
337 public DataChangeListener getDelegate() {
338 return (DataChangeListener) delegate;
344 public void close() throws Exception {
345 // TODO Auto-generated method stub