Migrate Collections references
[netconf.git] / apps / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / netconf / ProxyNetconfServiceTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.topology.singleton.impl.netconf;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import akka.actor.ActorSystem;
16 import akka.actor.Status;
17 import akka.dispatch.Futures;
18 import akka.testkit.TestProbe;
19 import akka.testkit.javadsl.TestKit;
20 import akka.util.Timeout;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import java.net.InetSocketAddress;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.mdsal.common.api.ReadFailedException;
33 import org.opendaylight.netconf.api.DocumentedException;
34 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
35 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
36 import org.opendaylight.netconf.topology.singleton.messages.netconf.CommitRequest;
37 import org.opendaylight.netconf.topology.singleton.messages.netconf.CreateEditConfigRequest;
38 import org.opendaylight.netconf.topology.singleton.messages.netconf.DeleteEditConfigRequest;
39 import org.opendaylight.netconf.topology.singleton.messages.netconf.DiscardChangesRequest;
40 import org.opendaylight.netconf.topology.singleton.messages.netconf.GetConfigRequest;
41 import org.opendaylight.netconf.topology.singleton.messages.netconf.GetRequest;
42 import org.opendaylight.netconf.topology.singleton.messages.netconf.LockRequest;
43 import org.opendaylight.netconf.topology.singleton.messages.netconf.MergeEditConfigRequest;
44 import org.opendaylight.netconf.topology.singleton.messages.netconf.RemoveEditConfigRequest;
45 import org.opendaylight.netconf.topology.singleton.messages.netconf.ReplaceEditConfigRequest;
46 import org.opendaylight.netconf.topology.singleton.messages.netconf.UnlockRequest;
47 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessageReply;
48 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
49 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
50 import org.opendaylight.yangtools.yang.common.ErrorTag;
51 import org.opendaylight.yangtools.yang.common.ErrorType;
52 import org.opendaylight.yangtools.yang.common.QName;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
54 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
56 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
57
58 public class ProxyNetconfServiceTest {
59     private static final RemoteDeviceId DEVICE_ID =
60         new RemoteDeviceId("dev1", InetSocketAddress.createUnresolved("localhost", 17830));
61     private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.of();
62     private static final LogicalDatastoreType STORE = LogicalDatastoreType.CONFIGURATION;
63
64     private static ActorSystem system = ActorSystem.apply();
65     private TestProbe masterActor;
66     private ContainerNode node;
67
68     @Before
69     public void setUp() {
70         masterActor = new TestProbe(system);
71         node = Builders.containerBuilder()
72             .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "cont")))
73             .build();
74     }
75
76     @AfterClass
77     public static void staticTearDown() {
78         TestKit.shutdownActorSystem(system, true);
79     }
80
81     private ProxyNetconfService newSuccessfulProxyNetconfService() {
82         return new ProxyNetconfService(DEVICE_ID, Futures.successful(masterActor.ref()),
83             system.dispatcher(), Timeout.apply(5, TimeUnit.SECONDS));
84     }
85
86     private ProxyNetconfService newSuccessfulProxyNetconfService(final Timeout timeout) {
87         return new ProxyNetconfService(DEVICE_ID, Futures.successful(masterActor.ref()),
88             system.dispatcher(), timeout);
89     }
90
91     @Test
92     public void testLock() {
93         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
94         netconf.lock();
95         masterActor.expectMsgClass(LockRequest.class);
96     }
97
98     @Test
99     public void testUnlock() {
100         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
101         netconf.unlock();
102         masterActor.expectMsgClass(UnlockRequest.class);
103     }
104
105     @Test
106     public void testDiscardChanges() {
107         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
108         netconf.discardChanges();
109         masterActor.expectMsgClass(DiscardChangesRequest.class);
110     }
111
112     @Test
113     public void testGet() throws Exception {
114         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
115         final ListenableFuture<Optional<NormalizedNode>> get = netconf.get(PATH);
116         final GetRequest getRequest = masterActor.expectMsgClass(GetRequest.class);
117         assertEquals(PATH, getRequest.getPath());
118
119         masterActor.reply(new NormalizedNodeMessage(PATH, node));
120         assertEquals(Optional.of(node), get.get(5, TimeUnit.SECONDS));
121     }
122
123     @Test
124     public void testGetFailure() throws InterruptedException, TimeoutException {
125         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
126
127         final ListenableFuture<Optional<NormalizedNode>> get = netconf.get(PATH);
128         masterActor.expectMsgClass(GetRequest.class);
129         final RuntimeException mockEx = new RuntimeException("fail");
130         masterActor.reply(new Status.Failure(mockEx));
131
132         try {
133             get.get(5, TimeUnit.SECONDS);
134             fail("Exception should be thrown");
135         } catch (final ExecutionException e) {
136             Throwable cause = e.getCause();
137             assertTrue("Unexpected cause " + cause, cause instanceof ReadFailedException);
138             assertEquals(mockEx, cause.getCause());
139         }
140     }
141
142     @Test
143     public void testGetEmpty() throws Exception {
144         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
145         final ListenableFuture<Optional<NormalizedNode>> get = netconf.get(PATH);
146         masterActor.expectMsgClass(GetRequest.class);
147         masterActor.reply(new EmptyReadResponse());
148         final Optional<NormalizedNode> result = get.get(5, TimeUnit.SECONDS);
149         assertFalse(result.isPresent());
150     }
151
152     @Test
153     public void testGetConfig() throws Exception {
154         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
155         final ListenableFuture<Optional<NormalizedNode>> getConfig = netconf.getConfig(PATH);
156         final GetConfigRequest getRequest = masterActor.expectMsgClass(GetConfigRequest.class);
157         assertEquals(PATH, getRequest.getPath());
158
159         masterActor.reply(new NormalizedNodeMessage(PATH, node));
160         assertEquals(Optional.of(node), getConfig.get(5, TimeUnit.SECONDS));
161     }
162
163     @Test
164     public void testGetConfigFailure() throws InterruptedException, TimeoutException {
165         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
166
167         final ListenableFuture<Optional<NormalizedNode>> getConfig = netconf.getConfig(PATH);
168         masterActor.expectMsgClass(GetConfigRequest.class);
169         final RuntimeException mockEx = new RuntimeException("fail");
170         masterActor.reply(new Status.Failure(mockEx));
171
172         try {
173             getConfig.get(5, TimeUnit.SECONDS);
174             fail("Exception should be thrown");
175         } catch (final ExecutionException e) {
176             Throwable cause = e.getCause();
177             assertTrue("Unexpected cause " + cause, cause instanceof ReadFailedException);
178             assertEquals(mockEx, cause.getCause());
179         }
180     }
181
182     @Test
183     public void testGetConfigEmpty() throws Exception {
184         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
185         final ListenableFuture<Optional<NormalizedNode>> getConfig = netconf.getConfig(PATH);
186         masterActor.expectMsgClass(GetConfigRequest.class);
187         masterActor.reply(new EmptyReadResponse());
188         final Optional<NormalizedNode> result = getConfig.get(5, TimeUnit.SECONDS);
189         assertFalse(result.isPresent());
190     }
191
192     @Test
193     public void testMerge() {
194         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
195         netconf.merge(STORE, PATH, node, Optional.empty());
196         final MergeEditConfigRequest mergeRequest = masterActor.expectMsgClass(MergeEditConfigRequest.class);
197         assertEquals(STORE, mergeRequest.getStore());
198         assertEquals(PATH, mergeRequest.getNormalizedNodeMessage().getIdentifier());
199         assertEquals(node, mergeRequest.getNormalizedNodeMessage().getNode());
200     }
201
202     @Test
203     public void testReplace() {
204         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
205         netconf.replace(STORE, PATH, node, Optional.empty());
206         final ReplaceEditConfigRequest replaceRequest = masterActor.expectMsgClass(ReplaceEditConfigRequest.class);
207         assertEquals(STORE, replaceRequest.getStore());
208         assertEquals(PATH, replaceRequest.getNormalizedNodeMessage().getIdentifier());
209         assertEquals(node, replaceRequest.getNormalizedNodeMessage().getNode());
210     }
211
212     @Test
213     public void testCreate() {
214         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
215         netconf.create(STORE, PATH, node, Optional.empty());
216         final CreateEditConfigRequest createRequest = masterActor.expectMsgClass(CreateEditConfigRequest.class);
217         assertEquals(STORE, createRequest.getStore());
218         assertEquals(PATH, createRequest.getNormalizedNodeMessage().getIdentifier());
219         assertEquals(node, createRequest.getNormalizedNodeMessage().getNode());
220     }
221
222     @Test
223     public void testDelete() {
224         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
225         netconf.delete(STORE, PATH);
226         final DeleteEditConfigRequest deleteRequest = masterActor.expectMsgClass(DeleteEditConfigRequest.class);
227         assertEquals(STORE, deleteRequest.getStore());
228         assertEquals(PATH, deleteRequest.getPath());
229     }
230
231     @Test
232     public void testRemove() {
233         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
234         netconf.remove(STORE, PATH);
235         final RemoveEditConfigRequest removeRequest = masterActor.expectMsgClass(RemoveEditConfigRequest.class);
236         assertEquals(STORE, removeRequest.getStore());
237         assertEquals(PATH, removeRequest.getPath());
238     }
239
240     @Test
241     public void testCommit() throws InterruptedException, ExecutionException, TimeoutException {
242         ProxyNetconfService netconf = newSuccessfulProxyNetconfService();
243         commit(netconf);
244     }
245
246     @Test
247     public void testFutureOperationsWithMasterDown() throws InterruptedException, TimeoutException {
248         ProxyNetconfService netconf = newSuccessfulProxyNetconfService(
249             Timeout.apply(500, TimeUnit.MILLISECONDS));
250
251         ListenableFuture<?> future = netconf.get(PATH);
252         masterActor.expectMsgClass(GetRequest.class);
253
254         // master doesn't reply
255         try {
256             future.get(5, TimeUnit.SECONDS);
257             fail("Exception should be thrown");
258         } catch (final ExecutionException e) {
259             Throwable cause = e.getCause();
260             assertTrue("Unexpected cause " + cause, cause instanceof ReadFailedException);
261             verifyDocumentedException(cause.getCause());
262         }
263
264         future = netconf.getConfig(PATH);
265         masterActor.expectMsgClass(GetConfigRequest.class);
266
267         // master doesn't reply
268         try {
269             future.get(5, TimeUnit.SECONDS);
270             fail("Exception should be thrown");
271         } catch (final ExecutionException e) {
272             Throwable cause = e.getCause();
273             assertTrue("Unexpected cause " + cause, cause instanceof ReadFailedException);
274             verifyDocumentedException(cause.getCause());
275         }
276
277         future = netconf.commit();
278         masterActor.expectMsgClass(CommitRequest.class);
279
280         // master doesn't reply
281         try {
282             future.get(5, TimeUnit.SECONDS);
283             fail("Exception should be thrown");
284         } catch (final ExecutionException e) {
285             Throwable cause = e.getCause();
286             assertTrue("Unexpected cause " + cause, cause instanceof NetconfServiceFailedException);
287             verifyDocumentedException(cause.getCause());
288         }
289     }
290
291     private void commit(final ProxyNetconfService netconf)
292         throws InterruptedException, ExecutionException, TimeoutException {
293         final ListenableFuture<?> submit = netconf.commit();
294         masterActor.expectMsgClass(CommitRequest.class);
295         masterActor.reply(new InvokeRpcMessageReply(null, List.of()));
296         submit.get(5, TimeUnit.SECONDS);
297     }
298
299     private static void verifyDocumentedException(final Throwable cause) {
300         assertTrue("Unexpected cause " + cause, cause instanceof DocumentedException);
301         final DocumentedException de = (DocumentedException) cause;
302         assertEquals(ErrorSeverity.WARNING, de.getErrorSeverity());
303         assertEquals(ErrorTag.OPERATION_FAILED, de.getErrorTag());
304         assertEquals(ErrorType.APPLICATION, de.getErrorType());
305     }
306 }