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.eq;
6 import static org.mockito.Mockito.atMost;
7 import static org.mockito.Mockito.doReturn;
8 import static org.mockito.Mockito.inOrder;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.verify;
11 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
12 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_FILTER_QNAME;
13 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
14 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.Futures;
18 import java.net.InetSocketAddress;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.InOrder;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
27 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
28 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
29 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
30 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
31 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 public class NetconfDeviceWriteOnlyTxTest {
44 private final RemoteDeviceId id = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
47 private DOMRpcService rpc;
48 private YangInstanceIdentifier yangIId;
51 public void setUp() throws Exception {
52 MockitoAnnotations.initMocks(this);
54 final CheckedFuture<DefaultDOMRpcResult, Exception> successFuture =
55 Futures.immediateCheckedFuture(new DefaultDOMRpcResult(((NormalizedNode<?, ?>) null)));
57 doReturn(successFuture)
58 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
59 .doReturn(successFuture)
60 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
62 yangIId = YangInstanceIdentifier.builder().node(NetconfState.QNAME).build();
66 public void testIgnoreNonVisibleData() {
67 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
69 final MapNode emptyList = ImmutableNodes.mapNodeBuilder(NETCONF_FILTER_QNAME).build();
70 tx.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
71 tx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
73 verify(rpc, atMost(1)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
77 public void testDiscardChanges() {
78 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
80 final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
82 submitFuture.checkedGet();
83 } catch (final TransactionCommitFailedException e) {
84 // verify discard changes was sent
85 final InOrder inOrder = inOrder(rpc);
86 inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
87 inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME), NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
88 inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME)), any(NormalizedNode.class));
89 inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME), NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_QNAME));
93 fail("Submit should fail");
97 public void testFailedCommit() throws Exception {
98 final CheckedFuture<DefaultDOMRpcResult, Exception> rpcErrorFuture =
99 Futures.immediateCheckedFuture(new DefaultDOMRpcResult(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "a", "m")));
101 doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(((NormalizedNode<?, ?>) null))))
102 .doReturn(rpcErrorFuture).when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
104 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
107 final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
109 submitFuture.checkedGet();
110 } catch (final TransactionCommitFailedException e) {
114 fail("Submit should fail");
118 public void testDiscardChangesNotSentWithoutCandidate() {
119 doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(((NormalizedNode<?, ?>) null))))
120 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
121 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
123 final WriteRunningTx tx = new WriteRunningTx(id, new NetconfBaseOps(rpc, NetconfDevice.INIT_SCHEMA_CTX),
126 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
127 } catch (final Exception e) {
128 // verify discard changes was sent
129 final InOrder inOrder = inOrder(rpc);
130 inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
131 inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), any(NormalizedNode.class));
132 inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME), NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
136 fail("Delete should fail");