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.fail;
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_QNAME;
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_QNAME;
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.api.DOMRpcService;
38 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
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.util.concurrent.FluentFutures;
46 import org.opendaylight.yangtools.yang.common.ErrorTag;
47 import org.opendaylight.yangtools.yang.common.ErrorType;
48 import org.opendaylight.yangtools.yang.common.QName;
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.schema.ContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
53 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
55 @RunWith(MockitoJUnitRunner.StrictStubs.class)
56 public class NetconfDeviceWriteOnlyTxTest extends AbstractBaseSchemasTest {
58 private final RemoteDeviceId id = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
61 private DOMRpcService rpc;
62 private YangInstanceIdentifier yangIId;
66 final ListenableFuture<DefaultDOMRpcResult> successFuture =
67 Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null));
69 doReturn(successFuture)
70 .doReturn(Futures.immediateFailedFuture(new IllegalStateException("Failed tx")))
71 .doReturn(successFuture)
72 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
74 yangIId = YangInstanceIdentifier.builder().node(NetconfState.QNAME).build();
78 public void testIgnoreNonVisibleData() {
79 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)),
81 final MapNode emptyList = ImmutableNodes.mapNodeBuilder(NETCONF_FILTER_QNAME).build();
82 tx.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier
83 .create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
84 tx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier
85 .create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
87 verify(rpc, atMost(1)).invokeRpc(any(QName.class), any(ContainerNode.class));
91 public void testDiscardChanges() throws InterruptedException {
92 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((ContainerNode) null)))
93 .when(rpc).invokeRpc(any(QName.class), isNull());
95 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)),
99 } catch (final ExecutionException e) {
100 // verify discard changes was sent
101 final InOrder inOrder = inOrder(rpc);
102 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
103 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
104 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME,
105 NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
106 inOrder.verify(rpc).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME),
108 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
109 NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_QNAME));
113 fail("Submit should fail");
117 public void testFailedCommit() throws Exception {
118 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
119 .doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(RpcResultBuilder.newError(ErrorType.APPLICATION,
120 new ErrorTag("a"), "m")))).when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
122 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)),
127 fail("Submit should fail");
128 } catch (final ExecutionException e) {
134 public void testDiscardChangesNotSentWithoutCandidate() {
135 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((ContainerNode) null)))
136 .doReturn(FluentFutures.immediateFailedFluentFuture(new IllegalStateException("Failed tx")))
137 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
139 final WriteRunningTx tx = new WriteRunningTx(id,
140 new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchemaWithNotifications().getMountPointContext()), false);
142 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
144 // verify discard changes was sent
145 final InOrder inOrder = inOrder(rpc);
146 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
147 NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
148 inOrder.verify(rpc).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME),
149 any(ContainerNode.class));
150 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
151 NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
155 public void testListenerSuccess() throws Exception {
156 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((ContainerNode) null)))
157 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
158 final WriteCandidateTx tx = new WriteCandidateTx(
159 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
160 final TxListener listener = mock(TxListener.class);
161 tx.addListener(listener);
162 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
164 verify(listener).onTransactionSubmitted(tx);
165 verify(listener).onTransactionSuccessful(tx);
166 verify(listener, never()).onTransactionFailed(eq(tx), any());
167 verify(listener, never()).onTransactionCancelled(tx);
171 public void testListenerCancellation() throws Exception {
172 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((ContainerNode) null)))
173 .when(rpc).invokeRpc(any(QName.class), isNull());
174 final WriteCandidateTx tx = new WriteCandidateTx(
175 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
176 final TxListener listener = mock(TxListener.class);
177 tx.addListener(listener);
178 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
180 verify(listener).onTransactionCancelled(tx);
181 verify(listener, never()).onTransactionSubmitted(tx);
182 verify(listener, never()).onTransactionSuccessful(tx);
183 verify(listener, never()).onTransactionFailed(eq(tx), any());
187 public void testListenerFailure() throws Exception {
188 final IllegalStateException cause = new IllegalStateException("Failed tx");
189 doReturn(FluentFutures.immediateFailedFluentFuture(cause))
190 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
191 final WriteCandidateTx tx = new WriteCandidateTx(
192 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
193 final TxListener listener = mock(TxListener.class);
194 tx.addListener(listener);
195 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
197 final ArgumentCaptor<Exception> excCaptor = ArgumentCaptor.forClass(Exception.class);
198 verify(listener).onTransactionSubmitted(tx);
199 verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
200 Assert.assertEquals(cause, excCaptor.getValue().getCause().getCause());
201 verify(listener, never()).onTransactionSuccessful(tx);
202 verify(listener, never()).onTransactionCancelled(tx);