Use NodeIdentifier to identify datastores
[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 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
9
10 import static org.junit.Assert.assertThrows;
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_NODEID;
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_NODEID;
23
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;
54
55 @RunWith(MockitoJUnitRunner.StrictStubs.class)
56 public class NetconfDeviceWriteOnlyTxTest extends AbstractBaseSchemasTest {
57
58     private final RemoteDeviceId id = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
59
60     @Mock
61     private DOMRpcService rpc;
62     private YangInstanceIdentifier yangIId;
63
64     @Before
65     public void setUp() {
66         final ListenableFuture<DefaultDOMRpcResult> successFuture =
67                 Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null));
68
69         doReturn(successFuture)
70                 .doReturn(Futures.immediateFailedFuture(new IllegalStateException("Failed tx")))
71                 .doReturn(successFuture)
72                 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
73
74         yangIId = YangInstanceIdentifier.builder().node(NetconfState.QNAME).build();
75     }
76
77     @Test
78     public void testIgnoreNonVisibleData() {
79         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)),
80                 false);
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);
86
87         verify(rpc, atMost(1)).invokeRpc(any(QName.class), any(ContainerNode.class));
88     }
89
90     @Test
91     public void testDiscardChanges() {
92         doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((ContainerNode) null)))
93                 .when(rpc).invokeRpc(any(QName.class), isNull());
94
95         final var future = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false)
96             .commit();
97         assertThrows(ExecutionException.class, () -> Futures.getDone(future));
98
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_NODEID));
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),
106             isNull());
107         inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
108             NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_NODEID));
109     }
110
111     @Test
112     public void testFailedCommit() {
113         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
114             .doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(RpcResultBuilder.newError(ErrorType.APPLICATION,
115                 new ErrorTag("a"), "m")))).when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
116
117         final var future = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false)
118             .commit();
119
120         assertThrows(ExecutionException.class, () -> Futures.getDone(future));
121     }
122
123     @Test
124     public void testDiscardChangesNotSentWithoutCandidate() {
125         doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((ContainerNode) null)))
126                 .doReturn(FluentFutures.immediateFailedFluentFuture(new IllegalStateException("Failed tx")))
127                 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
128
129         final WriteRunningTx tx = new WriteRunningTx(id,
130             new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchemaWithNotifications().getMountPointContext()), false);
131
132         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
133         tx.commit();
134         // verify discard changes was sent
135         final InOrder inOrder = inOrder(rpc);
136         inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
137                 NetconfBaseOps.getLockContent(NETCONF_RUNNING_NODEID));
138         inOrder.verify(rpc).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME),
139                 any(ContainerNode.class));
140         inOrder.verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
141                 NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_NODEID));
142     }
143
144     @Test
145     public void testListenerSuccess() throws Exception {
146         doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((ContainerNode) null)))
147                 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
148         final WriteCandidateTx tx = new WriteCandidateTx(
149                 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
150         final TxListener listener = mock(TxListener.class);
151         tx.addListener(listener);
152         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
153         tx.commit();
154         verify(listener).onTransactionSubmitted(tx);
155         verify(listener).onTransactionSuccessful(tx);
156         verify(listener, never()).onTransactionFailed(eq(tx), any());
157         verify(listener, never()).onTransactionCancelled(tx);
158     }
159
160     @Test
161     public void testListenerCancellation() throws Exception {
162         doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult((ContainerNode) null)))
163                 .when(rpc).invokeRpc(any(QName.class), isNull());
164         final WriteCandidateTx tx = new WriteCandidateTx(
165                 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
166         final TxListener listener = mock(TxListener.class);
167         tx.addListener(listener);
168         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
169         tx.cancel();
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());
174     }
175
176     @Test
177     public void testListenerFailure() throws Exception {
178         final IllegalStateException cause = new IllegalStateException("Failed tx");
179         doReturn(FluentFutures.immediateFailedFluentFuture(cause))
180                 .when(rpc).invokeRpc(any(QName.class), any(ContainerNode.class));
181         final WriteCandidateTx tx = new WriteCandidateTx(
182                 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
183         final TxListener listener = mock(TxListener.class);
184         tx.addListener(listener);
185         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
186         tx.commit();
187         final ArgumentCaptor<Exception> excCaptor = ArgumentCaptor.forClass(Exception.class);
188         verify(listener).onTransactionSubmitted(tx);
189         verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
190         Assert.assertEquals(cause, excCaptor.getValue().getCause().getCause());
191         verify(listener, never()).onTransactionSuccessful(tx);
192         verify(listener, never()).onTransactionCancelled(tx);
193     }
194 }