Merge "Bug-8187: call home throwing exception on startup" into stable/carbon
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / tx / NetconfWriteOnlyTransaction.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.netconf.topology.singleton.impl.tx;
10
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;
33
34 public class NetconfWriteOnlyTransaction implements DOMDataWriteTransaction {
35
36     private static final Logger LOG = LoggerFactory.getLogger(NetconfWriteOnlyTransaction.class);
37
38     private final RemoteDeviceId id;
39     private final NetconfDOMTransaction delegate;
40     private final ActorSystem actorSystem;
41
42     public NetconfWriteOnlyTransaction(final RemoteDeviceId id,
43                                        final ActorSystem actorSystem,
44                                        final NetconfDOMTransaction delegate) {
45         this.id = id;
46         this.delegate = delegate;
47         this.actorSystem = actorSystem;
48
49         this.delegate.openTransaction();
50     }
51
52     @Override
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);
56
57         delegate.put(store, new NormalizedNodeMessage(path, data));
58     }
59
60     @Override
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);
64
65         delegate.merge(store, new NormalizedNodeMessage(path, data));
66     }
67
68     @Override
69     public boolean cancel() {
70         LOG.trace("{}: Cancel", id);
71
72         return delegate.cancel();
73     }
74
75     @Override
76     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
77         LOG.trace("{}: Delete {} via NETCONF: {}", id, store, path);
78
79         delegate.delete(store, path);
80     }
81
82     @Override
83     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
84         LOG.trace("{}: Submit", id);
85
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>() {
90             @Nullable
91             @Override
92             public TransactionCommitFailedException apply(Exception input) {
93                 return new TransactionCommitFailedException("Transaction commit failed", input);
94             }
95         });
96         submit.onComplete(new OnComplete<Void>() {
97             @Override
98             public void onComplete(Throwable throwable, Void object) throws Throwable {
99                 if (throwable == null) {
100                     settFuture.set(object);
101                 } else {
102                     settFuture.setException(throwable);
103                 }
104             }
105         }, actorSystem.dispatcher());
106         return checkedFuture;
107     }
108
109     @Override
110     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
111         LOG.trace("{}: Commit", id);
112
113         final Future<Void> commit = delegate.submit();
114         final SettableFuture<RpcResult<TransactionStatus>> settFuture = SettableFuture.create();
115         commit.onComplete(new OnComplete<Void>() {
116             @Override
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);
122                 } else {
123                     settFuture.setException(throwable);
124                 }
125             }
126         }, actorSystem.dispatcher());
127         return settFuture;
128     }
129
130     @Override
131     public Object getIdentifier() {
132         return this;
133     }
134 }