Migrate netconf users of submit() to commit()
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / NetconfDeviceWriteOnlyTxTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
10
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;
24
25 import com.google.common.util.concurrent.CheckedFuture;
26 import com.google.common.util.concurrent.Futures;
27 import java.net.InetSocketAddress;
28 import java.util.concurrent.ExecutionException;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.InOrder;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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;
52
53 public class NetconfDeviceWriteOnlyTxTest {
54
55     private final RemoteDeviceId id = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
56
57     @Mock
58     private DOMRpcService rpc;
59     private YangInstanceIdentifier yangIId;
60
61     @Before
62     public void setUp() throws Exception {
63         MockitoAnnotations.initMocks(this);
64
65         final CheckedFuture<DefaultDOMRpcResult, Exception> successFuture =
66                 Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null));
67
68         doReturn(successFuture)
69                 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
70                 .doReturn(successFuture)
71                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
72
73         yangIId = YangInstanceIdentifier.builder().node(NetconfState.QNAME).build();
74     }
75
76     @Test
77     public void testIgnoreNonVisibleData() {
78         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
79                 false);
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);
85
86         verify(rpc, atMost(1)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
87     }
88
89     @Test
90     public void testDiscardChanges() throws InterruptedException {
91         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
92                 false);
93         try {
94             tx.commit().get();
95         } catch (final ExecutionException e) {
96             // verify discard changes was sent
97             final InOrder inOrder = inOrder(rpc);
98             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME),
99                     NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
100             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME),
101                     NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
102             inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME)),
103                     any(NormalizedNode.class));
104             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME),
105                     NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_QNAME));
106             return;
107         }
108
109         fail("Submit should fail");
110     }
111
112     @Test
113     public void testFailedCommit() throws Exception {
114         final CheckedFuture<DefaultDOMRpcResult, Exception> rpcErrorFuture = Futures.immediateCheckedFuture(
115                 new DefaultDOMRpcResult(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "a", "m")));
116
117         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
118         .doReturn(rpcErrorFuture).when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
119
120         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
121                 false);
122
123         try {
124             tx.commit().get();
125             fail("Submit should fail");
126         } catch (final ExecutionException e) {
127             // Intended
128         }
129     }
130
131     @Test
132     public void testDiscardChangesNotSentWithoutCandidate() {
133         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
134                 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
135                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
136
137         final WriteRunningTx tx = new WriteRunningTx(
138                 id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext()), false);
139
140         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
141         tx.commit();
142         // verify discard changes was sent
143         final InOrder inOrder = inOrder(rpc);
144         inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME),
145                 NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
146         inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)),
147                 any(NormalizedNode.class));
148         inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME),
149                 NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
150     }
151
152     @Test
153     public void testListenerSuccess() throws Exception {
154         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
155                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
156         final WriteCandidateTx tx = new WriteCandidateTx(
157                 id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
158         final TxListener listener = mock(TxListener.class);
159         tx.addListener(listener);
160         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
161         tx.commit();
162         verify(listener).onTransactionSubmitted(tx);
163         verify(listener).onTransactionSuccessful(tx);
164         verify(listener, never()).onTransactionFailed(eq(tx), any());
165         verify(listener, never()).onTransactionCancelled(tx);
166     }
167
168     @Test
169     public void testListenerCancellation() throws Exception {
170         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
171                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
172         final WriteCandidateTx tx = new WriteCandidateTx(
173                 id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
174         final TxListener listener = mock(TxListener.class);
175         tx.addListener(listener);
176         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
177         tx.cancel();
178         verify(listener).onTransactionCancelled(tx);
179         verify(listener, never()).onTransactionSubmitted(tx);
180         verify(listener, never()).onTransactionSuccessful(tx);
181         verify(listener, never()).onTransactionFailed(eq(tx), any());
182     }
183
184     @Test
185     public void testListenerFailure() throws Exception {
186         final IllegalStateException cause = new IllegalStateException("Failed tx");
187         doReturn(Futures.immediateFailedCheckedFuture(cause))
188                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
189         final WriteCandidateTx tx = new WriteCandidateTx(
190                 id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
191         final TxListener listener = mock(TxListener.class);
192         tx.addListener(listener);
193         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
194         tx.commit();
195         final ArgumentCaptor<Exception> excCaptor = ArgumentCaptor.forClass(Exception.class);
196         verify(listener).onTransactionSubmitted(tx);
197         verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
198         Assert.assertEquals(cause, excCaptor.getValue().getCause().getCause());
199         verify(listener, never()).onTransactionSuccessful(tx);
200         verify(listener, never()).onTransactionCancelled(tx);
201     }
202 }