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.client.mdsal.spi;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
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.client.mdsal.impl.NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT;
21 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_CANDIDATE_NODEID;
22 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID;
24 import com.google.common.util.concurrent.Futures;
25 import java.net.InetSocketAddress;
26 import java.util.concurrent.ExecutionException;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mock;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
35 import org.opendaylight.netconf.client.mdsal.AbstractBaseSchemasTest;
36 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
37 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
38 import org.opendaylight.netconf.client.mdsal.impl.NetconfBaseOps;
39 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.Commit;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.DiscardChanges;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditConfig;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.Lock;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.Unlock;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.get.input.Filter;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
47 import org.opendaylight.yangtools.yang.common.ErrorTag;
48 import org.opendaylight.yangtools.yang.common.ErrorType;
49 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
54 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
56 @RunWith(MockitoJUnitRunner.StrictStubs.class)
57 public class NetconfDeviceWriteOnlyTxTest extends AbstractBaseSchemasTest {
58 private static final RemoteDeviceId ID = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
59 private static final YangInstanceIdentifier STATE = YangInstanceIdentifier.of(NetconfState.QNAME);
62 private Rpcs.Normalized rpc;
66 final var successFuture = Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null));
67 doReturn(successFuture, Futures.immediateFailedFuture(new IllegalStateException("Failed tx")), successFuture)
68 .when(rpc).invokeNetconf(any(), any());
72 public void testIgnoreNonVisibleData() {
73 final var tx = new WriteCandidateTx(ID, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false, true);
76 final var emptyList = ImmutableNodes.newSystemMapBuilder()
77 .withNodeIdentifier(new NodeIdentifier(Filter.QNAME))
79 tx.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.of(Filter.QNAME), emptyList);
80 tx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.of(Filter.QNAME), emptyList);
82 verify(rpc, atMost(1)).invokeNetconf(any(), any());
86 public void testDiscardChanges() {
87 final var tx = new WriteCandidateTx(ID, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false, true);
89 final var future = tx.commit();
90 assertThrows(ExecutionException.class, () -> Futures.getDone(future));
92 // verify discard changes was sent
93 final var inOrder = inOrder(rpc);
94 inOrder.verify(rpc).invokeNetconf(Lock.QNAME, NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_NODEID));
95 inOrder.verify(rpc).invokeNetconf(Commit.QNAME, NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
96 inOrder.verify(rpc).invokeNetconf(DiscardChanges.QNAME, DISCARD_CHANGES_RPC_CONTENT);
97 inOrder.verify(rpc).invokeNetconf(Unlock.QNAME, NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_NODEID));
101 public void testFailedCommit() {
103 Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)),
104 Futures.immediateFuture(new DefaultDOMRpcResult(RpcResultBuilder.newError(ErrorType.APPLICATION,
105 new ErrorTag("a"), "m"))))
106 .when(rpc).invokeNetconf(any(), any());
108 final var tx = new WriteCandidateTx(ID, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false, true);
111 final var future = tx.commit();
112 assertThrows(ExecutionException.class, () -> Futures.getDone(future));
116 public void testDiscardChangesNotSentWithoutCandidate() {
118 Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)),
119 Futures.immediateFailedFuture(new IllegalStateException("Failed tx")))
120 .when(rpc).invokeNetconf(any(), any());
122 final var tx = new WriteRunningTx(ID,
123 new NetconfBaseOps(rpc, BASE_SCHEMAS.baseSchemaWithNotifications().getMountPointContext()), false, true);
126 tx.delete(LogicalDatastoreType.CONFIGURATION, STATE);
128 // verify discard changes was sent
129 final var inOrder = inOrder(rpc);
130 inOrder.verify(rpc).invokeNetconf(Lock.QNAME, NetconfBaseOps.getLockContent(NETCONF_RUNNING_NODEID));
131 inOrder.verify(rpc).invokeNetconf(eq(EditConfig.QNAME), any());
132 inOrder.verify(rpc).invokeNetconf(Unlock.QNAME, NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_NODEID));
136 public void testListenerSuccess() throws Exception {
137 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
138 .when(rpc).invokeNetconf(any(), any());
139 final var tx = new WriteCandidateTx(ID,
140 new NetconfBaseOps(rpc, BASE_SCHEMAS.baseSchema().getMountPointContext()), false, true);
143 final var listener = mock(TxListener.class);
144 tx.addListener(listener);
145 tx.delete(LogicalDatastoreType.CONFIGURATION, STATE);
147 verify(listener).onTransactionSubmitted(tx);
148 verify(listener).onTransactionSuccessful(tx);
149 verify(listener, never()).onTransactionFailed(eq(tx), any());
150 verify(listener, never()).onTransactionCancelled(tx);
154 public void testListenerCancellation() throws Exception {
155 final var tx = new WriteCandidateTx(ID,
156 new NetconfBaseOps(rpc, BASE_SCHEMAS.baseSchema().getMountPointContext()), false, true);
159 final var listener = mock(TxListener.class);
160 tx.addListener(listener);
161 tx.delete(LogicalDatastoreType.CONFIGURATION, STATE);
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 var cause = new IllegalStateException("Failed tx");
172 doReturn(Futures.immediateFailedFuture(cause)).when(rpc).invokeNetconf(any(), any());
173 final var tx = new WriteCandidateTx(ID,
174 new NetconfBaseOps(rpc, BASE_SCHEMAS.baseSchema().getMountPointContext()), false, true);
177 final var listener = mock(TxListener.class);
178 tx.addListener(listener);
179 tx.delete(LogicalDatastoreType.CONFIGURATION, STATE);
181 final var excCaptor = ArgumentCaptor.forClass(Exception.class);
182 verify(listener).onTransactionSubmitted(tx);
183 verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
184 assertEquals(cause, excCaptor.getValue().getCause().getCause());
185 verify(listener, never()).onTransactionSuccessful(tx);
186 verify(listener, never()).onTransactionCancelled(tx);