Rework BaseScheams
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / spi / 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.client.mdsal.spi;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.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.client.mdsal.impl.NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT;
21 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_CANDIDATE_NODEID;
22 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID;
23
24 import com.google.common.util.concurrent.Futures;
25 import java.net.InetSocketAddress;
26 import java.util.Set;
27 import java.util.concurrent.ExecutionException;
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.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
36 import org.opendaylight.netconf.api.CapabilityURN;
37 import org.opendaylight.netconf.client.mdsal.AbstractBaseSchemasTest;
38 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
39 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
40 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
41 import org.opendaylight.netconf.client.mdsal.impl.NetconfBaseOps;
42 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.Commit;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.DiscardChanges;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.EditConfig;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.Lock;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.Unlock;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.get.input.Filter;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
50 import org.opendaylight.yangtools.yang.common.ErrorTag;
51 import org.opendaylight.yangtools.yang.common.ErrorType;
52 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
55 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
57 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
58
59 @RunWith(MockitoJUnitRunner.StrictStubs.class)
60 public class NetconfDeviceWriteOnlyTxTest extends AbstractBaseSchemasTest {
61     private static final RemoteDeviceId ID = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
62     private static final YangInstanceIdentifier STATE = YangInstanceIdentifier.of(NetconfState.QNAME);
63
64     @Mock
65     private Rpcs.Normalized rpc;
66
67     @Before
68     public void setUp() {
69         final var successFuture = Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null));
70         doReturn(successFuture, Futures.immediateFailedFuture(new IllegalStateException("Failed tx")), successFuture)
71             .when(rpc).invokeNetconf(any(), any());
72     }
73
74     private static MountPointContext baseMountPointContext() {
75         return BASE_SCHEMAS.baseSchemaForCapabilities(NetconfSessionPreferences.fromStrings(Set.of(
76             "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?module=ietf-netconf-monitoring&revision=2010-10-04")))
77             .mountPointContext();
78     }
79
80     @Test
81     public void testIgnoreNonVisibleData() {
82         final var tx = new WriteCandidateTx(ID, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false, true);
83         tx.init();
84
85         final var emptyList = ImmutableNodes.newSystemMapBuilder()
86             .withNodeIdentifier(new NodeIdentifier(Filter.QNAME))
87             .build();
88         tx.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.of(Filter.QNAME), emptyList);
89         tx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.of(Filter.QNAME), emptyList);
90
91         verify(rpc, atMost(1)).invokeNetconf(any(), any());
92     }
93
94     @Test
95     public void testDiscardChanges() {
96         final var tx = new WriteCandidateTx(ID, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false, true);
97         tx.init();
98         final var future = tx.commit();
99         assertThrows(ExecutionException.class, () -> Futures.getDone(future));
100
101         // verify discard changes was sent
102         final var inOrder = inOrder(rpc);
103         inOrder.verify(rpc).invokeNetconf(Lock.QNAME, NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_NODEID));
104         inOrder.verify(rpc).invokeNetconf(Commit.QNAME, NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
105         inOrder.verify(rpc).invokeNetconf(DiscardChanges.QNAME, DISCARD_CHANGES_RPC_CONTENT);
106         inOrder.verify(rpc).invokeNetconf(Unlock.QNAME, NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_NODEID));
107     }
108
109     @Test
110     public void testFailedCommit() {
111         doReturn(
112             Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)),
113             Futures.immediateFuture(new DefaultDOMRpcResult(RpcResultBuilder.newError(ErrorType.APPLICATION,
114                 new ErrorTag("a"), "m"))))
115             .when(rpc).invokeNetconf(any(), any());
116
117         final var tx = new WriteCandidateTx(ID, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false, true);
118         tx.init();
119
120         final var future = tx.commit();
121         assertThrows(ExecutionException.class, () -> Futures.getDone(future));
122     }
123
124     @Test
125     public void testDiscardChangesNotSentWithoutCandidate() {
126         doReturn(
127             Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)),
128             Futures.immediateFailedFuture(new IllegalStateException("Failed tx")))
129             .when(rpc).invokeNetconf(any(), any());
130
131         final var tx = new WriteRunningTx(ID, new NetconfBaseOps(rpc, BASE_SCHEMAS.baseSchemaForCapabilities(
132             NetconfSessionPreferences.fromStrings(Set.of(CapabilityURN.NOTIFICATION,
133                 "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?module=ietf-netconf-monitoring"
134                     + "&revision=2010-10-04"))).mountPointContext()), false, true);
135         tx.init();
136
137         tx.delete(LogicalDatastoreType.CONFIGURATION, STATE);
138         tx.commit();
139         // verify discard changes was sent
140         final var inOrder = inOrder(rpc);
141         inOrder.verify(rpc).invokeNetconf(Lock.QNAME, NetconfBaseOps.getLockContent(NETCONF_RUNNING_NODEID));
142         inOrder.verify(rpc).invokeNetconf(eq(EditConfig.QNAME), any());
143         inOrder.verify(rpc).invokeNetconf(Unlock.QNAME, NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_NODEID));
144     }
145
146     @Test
147     public void testListenerSuccess() throws Exception {
148         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
149             .when(rpc).invokeNetconf(any(), any());
150         final var tx = new WriteCandidateTx(ID, new NetconfBaseOps(rpc, baseMountPointContext()), false, true);
151         tx.init();
152
153         final var listener = mock(TxListener.class);
154         tx.addListener(listener);
155         tx.delete(LogicalDatastoreType.CONFIGURATION, STATE);
156         tx.commit();
157         verify(listener).onTransactionSubmitted(tx);
158         verify(listener).onTransactionSuccessful(tx);
159         verify(listener, never()).onTransactionFailed(eq(tx), any());
160         verify(listener, never()).onTransactionCancelled(tx);
161     }
162
163     @Test
164     public void testListenerCancellation() throws Exception {
165         final var tx = new WriteCandidateTx(ID, new NetconfBaseOps(rpc, baseMountPointContext()), false, true);
166         tx.init();
167
168         final var listener = mock(TxListener.class);
169         tx.addListener(listener);
170         tx.delete(LogicalDatastoreType.CONFIGURATION, STATE);
171         tx.cancel();
172         verify(listener).onTransactionCancelled(tx);
173         verify(listener, never()).onTransactionSubmitted(tx);
174         verify(listener, never()).onTransactionSuccessful(tx);
175         verify(listener, never()).onTransactionFailed(eq(tx), any());
176     }
177
178     @Test
179     public void testListenerFailure() throws Exception {
180         final var cause = new IllegalStateException("Failed tx");
181         doReturn(Futures.immediateFailedFuture(cause)).when(rpc).invokeNetconf(any(), any());
182         final var tx = new WriteCandidateTx(ID, new NetconfBaseOps(rpc, baseMountPointContext()), false, true);
183         tx.init();
184
185         final var listener = mock(TxListener.class);
186         tx.addListener(listener);
187         tx.delete(LogicalDatastoreType.CONFIGURATION, STATE);
188         tx.commit();
189         final var excCaptor = ArgumentCaptor.forClass(Exception.class);
190         verify(listener).onTransactionSubmitted(tx);
191         verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
192         assertEquals(cause, excCaptor.getValue().getCause().getCause());
193         verify(listener, never()).onTransactionSuccessful(tx);
194         verify(listener, never()).onTransactionCancelled(tx);
195     }
196 }