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
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.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.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;
23 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
25 import com.google.common.util.concurrent.CheckedFuture;
26 import com.google.common.util.concurrent.Futures;
27 import java.net.InetSocketAddress;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.InOrder;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
37 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
38 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
39 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
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.yang.common.RpcError;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
53 public class NetconfDeviceWriteOnlyTxTest {
55 private final RemoteDeviceId id = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
58 private DOMRpcService rpc;
59 private YangInstanceIdentifier yangIId;
62 public void setUp() throws Exception {
63 MockitoAnnotations.initMocks(this);
65 final CheckedFuture<DefaultDOMRpcResult, Exception> successFuture =
66 Futures.immediateCheckedFuture(new DefaultDOMRpcResult(((NormalizedNode<?, ?>) null)));
68 doReturn(successFuture)
69 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
70 .doReturn(successFuture)
71 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
73 yangIId = YangInstanceIdentifier.builder().node(NetconfState.QNAME).build();
77 public void testIgnoreNonVisibleData() {
78 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
80 final MapNode emptyList = ImmutableNodes.mapNodeBuilder(NETCONF_FILTER_QNAME).build();
81 tx.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
82 tx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
84 verify(rpc, atMost(1)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
88 public void testDiscardChanges() {
89 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
91 final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
93 submitFuture.checkedGet();
94 } catch (final TransactionCommitFailedException e) {
95 // verify discard changes was sent
96 final InOrder inOrder = inOrder(rpc);
97 inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
98 inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME), NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
99 inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME)), any(NormalizedNode.class));
100 inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME), NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_QNAME));
104 fail("Submit should fail");
108 public void testFailedCommit() throws Exception {
109 final CheckedFuture<DefaultDOMRpcResult, Exception> rpcErrorFuture =
110 Futures.immediateCheckedFuture(new DefaultDOMRpcResult(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "a", "m")));
112 doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(((NormalizedNode<?, ?>) null))))
113 .doReturn(rpcErrorFuture).when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
115 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
118 final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
120 submitFuture.checkedGet();
121 } catch (final TransactionCommitFailedException e) {
125 fail("Submit should fail");
129 public void testDiscardChangesNotSentWithoutCandidate() {
130 doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
131 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
132 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
134 final WriteRunningTx tx = new WriteRunningTx(id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext()),
137 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
139 // verify discard changes was sent
140 final InOrder inOrder = inOrder(rpc);
141 inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
142 inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), any(NormalizedNode.class));
143 inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME), NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
147 public void testListenerSuccess() throws Exception {
148 doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
149 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
150 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
151 final TxListener listener = mock(TxListener.class);
152 tx.addListener(listener);
153 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
155 verify(listener).onTransactionSubmitted(tx);
156 verify(listener).onTransactionSuccessful(tx);
157 verify(listener, never()).onTransactionFailed(eq(tx), any());
158 verify(listener, never()).onTransactionCancelled(tx);
162 public void testListenerCancellation() throws Exception {
163 doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
164 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
165 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
166 final TxListener listener = mock(TxListener.class);
167 tx.addListener(listener);
168 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
170 verify(listener).onTransactionCancelled(tx);
171 verify(listener, never()).onTransactionSubmitted(tx);
172 verify(listener, never()).onTransactionSuccessful(tx);
173 verify(listener, never()).onTransactionFailed(eq(tx), any());
177 public void testListenerFailure() throws Exception {
178 final IllegalStateException cause = new IllegalStateException("Failed tx");
179 doReturn(Futures.immediateFailedCheckedFuture(cause))
180 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
181 final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
182 final TxListener listener = mock(TxListener.class);
183 tx.addListener(listener);
184 tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
186 final ArgumentCaptor<Exception> excCaptor = ArgumentCaptor.forClass(Exception.class);
187 verify(listener).onTransactionSubmitted(tx);
188 verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
189 Assert.assertEquals(cause, excCaptor.getValue().getCause().getCause());
190 verify(listener, never()).onTransactionSuccessful(tx);
191 verify(listener, never()).onTransactionCancelled(tx);