/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.netconf.topology.pipeline.tx; import akka.actor.ActorSystem; import akka.dispatch.OnComplete; import com.google.common.base.Function; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; import javax.annotation.Nullable; import org.opendaylight.controller.md.sal.common.api.TransactionStatus; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction; import org.opendaylight.netconf.topology.pipeline.ProxyNetconfDeviceDataBroker; import org.opendaylight.netconf.topology.util.messages.NormalizedNodeMessage; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import scala.concurrent.Future; public class ProxyWriteOnlyTransaction implements DOMDataWriteTransaction { private final ProxyNetconfDeviceDataBroker delegate; private final ActorSystem actorSystem; public ProxyWriteOnlyTransaction(ActorSystem actorSystem, final ProxyNetconfDeviceDataBroker delegate) { this.delegate = delegate; this.actorSystem = actorSystem; } @Override public void put (final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode < ?,?>data){ delegate.put(store, new NormalizedNodeMessage(path, data)); } @Override public void merge (final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode < ?,?>data){ delegate.merge(store, new NormalizedNodeMessage(path, data)); } @Override public boolean cancel () { return delegate.cancel(); } @Override public void delete (final LogicalDatastoreType store, final YangInstanceIdentifier path){ delegate.delete(store, path); } @Override public CheckedFuture submit() { final Future submit = delegate.submit(); final SettableFuture settableFuture = SettableFuture.create(); final CheckedFuture checkedFuture = Futures.makeChecked(settableFuture, new Function() { @Nullable @Override public TransactionCommitFailedException apply(Exception input) { return new TransactionCommitFailedException("Transaction commit failed", input); } }); submit.onComplete(new OnComplete() { @Override public void onComplete(Throwable throwable, Void aVoid) throws Throwable { if (throwable == null) { settableFuture.set(aVoid); } else { settableFuture.setException(throwable); } } }, actorSystem.dispatcher()); return checkedFuture; } @Override public ListenableFuture> commit () { final Future> commit = delegate.commit(); final SettableFuture> settableFuture = SettableFuture.create(); commit.onComplete(new OnComplete>() { @Override public void onComplete(Throwable throwable, RpcResult transactionStatusRpcResult) throws Throwable { if (throwable == null) { settableFuture.set(transactionStatusRpcResult); } else { settableFuture.setException(throwable); } } }, actorSystem.dispatcher()); return settableFuture; } @Override public Object getIdentifier () { return this; } }