2 * Copyright (c) 2014, 2015 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
8 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
10 import static org.junit.Assert.assertThrows;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.atMost;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.inOrder;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.never;
18 import static org.mockito.Mockito.verify;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_NODEID;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_FILTER_QNAME;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID;
24 import com.google.common.util.concurrent.Futures;
25 import com.google.common.util.concurrent.ListenableFuture;
26 import java.net.InetSocketAddress;
27 import java.util.concurrent.ExecutionException;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.InOrder;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
37 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
38 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
39 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
40 import org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest;
41 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
42 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
44 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
45 import org.opendaylight.yangtools.yang.common.ErrorTag;
46 import org.opendaylight.yangtools.yang.common.ErrorType;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
52 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
54 @RunWith(MockitoJUnitRunner.StrictStubs.class)
55 public class NetconfDeviceWriteOnlyTxTest extends AbstractBaseSchemasTest {
57 private final RemoteDeviceId id = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
60 private Rpcs.Normalized rpc;
61 private YangInstanceIdentifier yangIId;
65 final ListenableFuture<DefaultDOMRpcResult> successFuture =
66 Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null));
68 doReturn(successFuture)
69 .doReturn(Futures.immediateFailedFuture(new IllegalStateException("Failed tx")))
70 .doReturn(successFuture)
71 .when(rpc).invokeNetconf(any(), any());
73 yangIId = YangInstanceIdentifier.builder().node(NetconfState.QNAME).build();
77 public void testIgnoreNonVisibleData() {
78 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)),
80 final MapNode emptyList = ImmutableNodes.mapNodeBuilder(NETCONF_FILTER_QNAME).build();
81 tx.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier
82 .create(new NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
83 tx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier
84 .create(new NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
86 verify(rpc, atMost(1)).invokeNetconf(any(), any());
90 public void testDiscardChanges() {
91 final var future = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false)
93 assertThrows(ExecutionException.class, () -> Futures.getDone(future));
95 // verify discard changes was sent
96 final InOrder inOrder = inOrder(rpc);
97 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
98 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_NODEID));
99 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME,
100 NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
101 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME,
102 DISCARD_CHANGES_RPC_CONTENT);
103 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
104 NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_NODEID));
108 public void testFailedCommit() {
109 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
110 .doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(RpcResultBuilder.newError(ErrorType.APPLICATION,
111 new ErrorTag("a"), "m")))).when(rpc).invokeNetconf(any(), any());
113 final var future = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false)
116 assertThrows(ExecutionException.class, () -> Futures.getDone(future));
120 public void testDiscardChangesNotSentWithoutCandidate() {
121 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
122 .doReturn(Futures.immediateFailedFuture(new IllegalStateException("Failed tx")))
123 .when(rpc).invokeNetconf(any(), any());
125 final WriteRunningTx tx = new WriteRunningTx(id,
126 new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchemaWithNotifications().getMountPointContext()), false);
128 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
130 // verify discard changes was sent
131 final InOrder inOrder = inOrder(rpc);
132 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
133 NetconfBaseOps.getLockContent(NETCONF_RUNNING_NODEID));
134 inOrder.verify(rpc).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), any());
135 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
136 NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_NODEID));
140 public void testListenerSuccess() throws Exception {
141 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
142 .when(rpc).invokeNetconf(any(), any());
143 final WriteCandidateTx tx = new WriteCandidateTx(
144 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
145 final TxListener listener = mock(TxListener.class);
146 tx.addListener(listener);
147 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
149 verify(listener).onTransactionSubmitted(tx);
150 verify(listener).onTransactionSuccessful(tx);
151 verify(listener, never()).onTransactionFailed(eq(tx), any());
152 verify(listener, never()).onTransactionCancelled(tx);
156 public void testListenerCancellation() throws Exception {
157 final WriteCandidateTx tx = new WriteCandidateTx(
158 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
159 final TxListener listener = mock(TxListener.class);
160 tx.addListener(listener);
161 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
163 verify(listener).onTransactionCancelled(tx);
164 verify(listener, never()).onTransactionSubmitted(tx);
165 verify(listener, never()).onTransactionSuccessful(tx);
166 verify(listener, never()).onTransactionFailed(eq(tx), any());
170 public void testListenerFailure() throws Exception {
171 final IllegalStateException cause = new IllegalStateException("Failed tx");
172 doReturn(Futures.immediateFailedFuture(cause)).when(rpc).invokeNetconf(any(), any());
173 final WriteCandidateTx tx = new WriteCandidateTx(
174 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
175 final TxListener listener = mock(TxListener.class);
176 tx.addListener(listener);
177 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
179 final ArgumentCaptor<Exception> excCaptor = ArgumentCaptor.forClass(Exception.class);
180 verify(listener).onTransactionSubmitted(tx);
181 verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
182 Assert.assertEquals(cause, excCaptor.getValue().getCause().getCause());
183 verify(listener, never()).onTransactionSuccessful(tx);
184 verify(listener, never()).onTransactionCancelled(tx);