1 package org.opendaylight.controller.sal.connect.netconf.sal.tx;
3 import static org.junit.Assert.fail;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Matchers.same;
6 import static org.mockito.Mockito.doReturn;
7 import static org.mockito.Mockito.inOrder;
8 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT;
9 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
10 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Collections;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.InOrder;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
24 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionCapabilities;
25 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
26 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
27 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
28 import org.opendaylight.controller.sal.core.api.RpcImplementation;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
32 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 public class NetconfDeviceWriteOnlyTxTest {
37 private final RemoteDeviceId id = new RemoteDeviceId("test-mount");
40 private RpcImplementation rpc;
42 private DataNormalizer normalizer;
43 private YangInstanceIdentifier yangIId;
46 public void setUp() throws Exception {
47 MockitoAnnotations.initMocks(this);
49 ListenableFuture<RpcResult<CompositeNode>> successFuture = Futures.immediateFuture(RpcResultBuilder.<CompositeNode>success().build());
51 doReturn(successFuture)
52 .doReturn(Futures.<RpcResult<CompositeNode>>immediateFailedFuture(new IllegalStateException("Failed tx")))
53 .doReturn(successFuture)
54 .when(rpc).invokeRpc(any(QName.class), any(CompositeNode.class));
56 yangIId = YangInstanceIdentifier.builder().node(QName.create("namespace", "2012-12-12", "name")).build();
57 doReturn(yangIId).when(normalizer).toLegacy(yangIId);
61 public void testDiscardChanges() {
62 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc), normalizer,
63 NetconfSessionCapabilities.fromStrings(Collections.<String>emptySet()));
64 final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
66 submitFuture.checkedGet();
67 } catch (final TransactionCommitFailedException e) {
68 // verify discard changes was sent
69 final InOrder inOrder = inOrder(rpc);
70 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME, NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
71 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME, NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
72 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME, DISCARD_CHANGES_RPC_CONTENT);
73 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME, NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_QNAME));
77 fail("Submit should fail");
81 public void testDiscardChangesNotSentWithoutCandidate() {
82 doReturn(Futures.immediateFuture(RpcResultBuilder.<CompositeNode>success().build()))
83 .doReturn(Futures.<RpcResult<CompositeNode>>immediateFailedFuture(new IllegalStateException("Failed tx")))
84 .when(rpc).invokeRpc(any(QName.class), any(CompositeNode.class));
86 final WriteRunningTx tx = new WriteRunningTx(id, new NetconfBaseOps(rpc), normalizer,
87 NetconfSessionCapabilities.fromStrings(Collections.<String>emptySet()));
89 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
90 } catch (final Exception e) {
91 // verify discard changes was sent
92 final InOrder inOrder = inOrder(rpc);
93 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME, NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
94 inOrder.verify(rpc).invokeRpc(same(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), any(CompositeNode.class));
95 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME, NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
99 fail("Delete should fail");