2 * Copyright (c) 2016 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
9 package org.opendaylight.netconf.topology.singleton.impl.tx;
11 import akka.actor.ActorSystem;
12 import akka.dispatch.OnComplete;
13 import com.google.common.base.Function;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.SettableFuture;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
25 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import scala.concurrent.Future;
34 public class NetconfWriteOnlyTransaction implements DOMDataWriteTransaction {
36 private static final Logger LOG = LoggerFactory.getLogger(NetconfWriteOnlyTransaction.class);
38 private final RemoteDeviceId id;
39 private final NetconfDOMTransaction delegate;
40 private final ActorSystem actorSystem;
42 public NetconfWriteOnlyTransaction(final RemoteDeviceId id,
43 final ActorSystem actorSystem,
44 final NetconfDOMTransaction delegate) {
46 this.delegate = delegate;
47 this.actorSystem = actorSystem;
49 this.delegate.openTransaction();
53 public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
54 final NormalizedNode<?,?> data) {
55 LOG.trace("{}: Write {} via NETCONF: {} with payload {}", id, store, path, data);
57 delegate.put(store, new NormalizedNodeMessage(path, data));
61 public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
62 final NormalizedNode<?,?> data) {
63 LOG.trace("{}: Merge {} via NETCONF: {} with payload {}", id, store, path, data);
65 delegate.merge(store, new NormalizedNodeMessage(path, data));
69 public boolean cancel() {
70 LOG.trace("{}: Cancel", id);
72 return delegate.cancel();
76 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
77 LOG.trace("{}: Delete {} via NETCONF: {}", id, store, path);
79 delegate.delete(store, path);
83 public CheckedFuture<Void, TransactionCommitFailedException> submit() {
84 LOG.trace("{}: Submit", id);
86 final Future<Void> submit = delegate.submit();
87 final SettableFuture<Void> settFuture = SettableFuture.create();
88 final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture;
89 checkedFuture = Futures.makeChecked(settFuture, new Function<Exception, TransactionCommitFailedException>() {
92 public TransactionCommitFailedException apply(Exception input) {
93 return new TransactionCommitFailedException("Transaction commit failed", input);
96 submit.onComplete(new OnComplete<Void>() {
98 public void onComplete(Throwable throwable, Void object) throws Throwable {
99 if (throwable == null) {
100 settFuture.set(object);
102 settFuture.setException(throwable);
105 }, actorSystem.dispatcher());
106 return checkedFuture;
110 public ListenableFuture<RpcResult<TransactionStatus>> commit() {
111 LOG.trace("{}: Commit", id);
113 final Future<Void> commit = delegate.submit();
114 final SettableFuture<RpcResult<TransactionStatus>> settFuture = SettableFuture.create();
115 commit.onComplete(new OnComplete<Void>() {
117 public void onComplete(final Throwable throwable, final Void result) throws Throwable {
118 if (throwable == null) {
119 TransactionStatus status = TransactionStatus.SUBMITED;
120 RpcResult<TransactionStatus> rpcResult = RpcResultBuilder.success(status).build();
121 settFuture.set(rpcResult);
123 settFuture.setException(throwable);
126 }, actorSystem.dispatcher());
131 public Object getIdentifier() {