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.ArgumentMatchers.isNull;
14 import static org.mockito.Mockito.atMost;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.inOrder;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.never;
19 import static org.mockito.Mockito.verify;
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.RemoteDeviceServices.Rpcs;
39 import org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest;
40 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
41 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
42 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
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 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
92 .when(rpc).invokeNetconf(any(), isNull());
94 final var future = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false)
96 assertThrows(ExecutionException.class, () -> Futures.getDone(future));
98 // verify discard changes was sent
99 final InOrder inOrder = inOrder(rpc);
100 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
101 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_NODEID));
102 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME,
103 NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
104 inOrder.verify(rpc).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME),
106 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
107 NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_NODEID));
111 public void testFailedCommit() {
112 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
113 .doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(RpcResultBuilder.newError(ErrorType.APPLICATION,
114 new ErrorTag("a"), "m")))).when(rpc).invokeNetconf(any(), any());
116 final var future = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false)
119 assertThrows(ExecutionException.class, () -> Futures.getDone(future));
123 public void testDiscardChangesNotSentWithoutCandidate() {
124 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
125 .doReturn(Futures.immediateFailedFuture(new IllegalStateException("Failed tx")))
126 .when(rpc).invokeNetconf(any(), any());
128 final WriteRunningTx tx = new WriteRunningTx(id,
129 new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchemaWithNotifications().getMountPointContext()), false);
131 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
133 // verify discard changes was sent
134 final InOrder inOrder = inOrder(rpc);
135 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
136 NetconfBaseOps.getLockContent(NETCONF_RUNNING_NODEID));
137 inOrder.verify(rpc).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), any());
138 inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
139 NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_NODEID));
143 public void testListenerSuccess() throws Exception {
144 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
145 .when(rpc).invokeNetconf(any(), any());
146 final WriteCandidateTx tx = new WriteCandidateTx(
147 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
148 final TxListener listener = mock(TxListener.class);
149 tx.addListener(listener);
150 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
152 verify(listener).onTransactionSubmitted(tx);
153 verify(listener).onTransactionSuccessful(tx);
154 verify(listener, never()).onTransactionFailed(eq(tx), any());
155 verify(listener, never()).onTransactionCancelled(tx);
159 public void testListenerCancellation() throws Exception {
160 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
161 .when(rpc).invokeNetconf(any(), isNull());
162 final WriteCandidateTx tx = new WriteCandidateTx(
163 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
164 final TxListener listener = mock(TxListener.class);
165 tx.addListener(listener);
166 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
168 verify(listener).onTransactionCancelled(tx);
169 verify(listener, never()).onTransactionSubmitted(tx);
170 verify(listener, never()).onTransactionSuccessful(tx);
171 verify(listener, never()).onTransactionFailed(eq(tx), any());
175 public void testListenerFailure() throws Exception {
176 final IllegalStateException cause = new IllegalStateException("Failed tx");
177 doReturn(Futures.immediateFailedFuture(cause)).when(rpc).invokeNetconf(any(), any());
178 final WriteCandidateTx tx = new WriteCandidateTx(
179 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
180 final TxListener listener = mock(TxListener.class);
181 tx.addListener(listener);
182 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
184 final ArgumentCaptor<Exception> excCaptor = ArgumentCaptor.forClass(Exception.class);
185 verify(listener).onTransactionSubmitted(tx);
186 verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
187 Assert.assertEquals(cause, excCaptor.getValue().getCause().getCause());
188 verify(listener, never()).onTransactionSuccessful(tx);
189 verify(listener, never()).onTransactionCancelled(tx);