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