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.FluentFuture;
25 import java.net.InetSocketAddress;
26 import java.util.concurrent.ExecutionException;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.InOrder;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.mdsal.dom.api.DOMRpcService;
37 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
38 import org.opendaylight.netconf.sal.connect.netconf.AbstractBaseSchemasTest;
39 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
40 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
41 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
43 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
44 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.common.RpcError;
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.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
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 DOMRpcService rpc;
61 private YangInstanceIdentifier yangIId;
65 final FluentFuture<DefaultDOMRpcResult> successFuture =
66 FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((NormalizedNode) null));
68 doReturn(successFuture)
69 .doReturn(FluentFutures.immediateFailedFluentFuture(new IllegalStateException("Failed tx")))
70 .doReturn(successFuture)
71 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
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 YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
83 tx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier
84 .create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
86 verify(rpc, atMost(1)).invokeRpc(any(QName.class), any(ContainerNode.class));
90 public void testDiscardChanges() throws InterruptedException {
91 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((NormalizedNode) null)))
92 .when(rpc).invokeRpc(any(QName.class), isNull());
94 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)),
98 } catch (final ExecutionException e) {
99 // verify discard changes was sent
100 final InOrder inOrder = inOrder(rpc);
101 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
102 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
103 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME,
104 NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
105 inOrder.verify(rpc).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME),
107 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
108 NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_QNAME));
112 fail("Submit should fail");
116 public void testFailedCommit() throws Exception {
117 final FluentFuture<DefaultDOMRpcResult> rpcErrorFuture = FluentFutures.immediateFluentFuture(
118 new DefaultDOMRpcResult(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "a", "m")));
120 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((NormalizedNode) null)))
121 .doReturn(rpcErrorFuture).when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
123 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)),
128 fail("Submit should fail");
129 } catch (final ExecutionException e) {
135 public void testDiscardChangesNotSentWithoutCandidate() {
136 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((NormalizedNode) null)))
137 .doReturn(FluentFutures.immediateFailedFluentFuture(new IllegalStateException("Failed tx")))
138 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
140 final WriteRunningTx tx = new WriteRunningTx(id,
141 new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchemaWithNotifications().getMountPointContext()), false);
143 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
145 // verify discard changes was sent
146 final InOrder inOrder = inOrder(rpc);
147 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
148 NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
149 inOrder.verify(rpc).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME),
150 any(ContainerNode.class));
151 inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
152 NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
156 public void testListenerSuccess() throws Exception {
157 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((NormalizedNode) null)))
158 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
159 final WriteCandidateTx tx = new WriteCandidateTx(
160 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
161 final TxListener listener = mock(TxListener.class);
162 tx.addListener(listener);
163 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
165 verify(listener).onTransactionSubmitted(tx);
166 verify(listener).onTransactionSuccessful(tx);
167 verify(listener, never()).onTransactionFailed(eq(tx), any());
168 verify(listener, never()).onTransactionCancelled(tx);
172 public void testListenerCancellation() throws Exception {
173 doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((NormalizedNode) null)))
174 .when(rpc).invokeRpc(any(QName.class), isNull());
175 final WriteCandidateTx tx = new WriteCandidateTx(
176 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
177 final TxListener listener = mock(TxListener.class);
178 tx.addListener(listener);
179 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
181 verify(listener).onTransactionCancelled(tx);
182 verify(listener, never()).onTransactionSubmitted(tx);
183 verify(listener, never()).onTransactionSuccessful(tx);
184 verify(listener, never()).onTransactionFailed(eq(tx), any());
188 public void testListenerFailure() throws Exception {
189 final IllegalStateException cause = new IllegalStateException("Failed tx");
190 doReturn(FluentFutures.immediateFailedFluentFuture(cause))
191 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
192 final WriteCandidateTx tx = new WriteCandidateTx(
193 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
194 final TxListener listener = mock(TxListener.class);
195 tx.addListener(listener);
196 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
198 final ArgumentCaptor<Exception> excCaptor = ArgumentCaptor.forClass(Exception.class);
199 verify(listener).onTransactionSubmitted(tx);
200 verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
201 Assert.assertEquals(cause, excCaptor.getValue().getCause().getCause());
202 verify(listener, never()).onTransactionSuccessful(tx);
203 verify(listener, never()).onTransactionCancelled(tx);