394797673d3cf176ad7d347522c7bac9f9ac1a0d
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / NetconfDeviceTest.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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.anyCollectionOf;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.ArgumentMatchers.isNull;
15 import static org.mockito.Mockito.after;
16 import static org.mockito.Mockito.doAnswer;
17 import static org.mockito.Mockito.doNothing;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.timeout;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23
24 import com.google.common.collect.HashMultimap;
25 import com.google.common.collect.Iterables;
26 import com.google.common.collect.Lists;
27 import com.google.common.collect.Sets;
28 import com.google.common.util.concurrent.Futures;
29 import com.google.common.util.concurrent.ListeningExecutorService;
30 import com.google.common.util.concurrent.MoreExecutors;
31 import com.google.common.util.concurrent.SettableFuture;
32 import java.io.IOException;
33 import java.net.InetSocketAddress;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Map;
39 import java.util.concurrent.Executors;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Mockito;
43 import org.opendaylight.mdsal.dom.api.DOMActionService;
44 import org.opendaylight.mdsal.dom.api.DOMNotification;
45 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
46 import org.opendaylight.mdsal.dom.api.DOMRpcService;
47 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
48 import org.opendaylight.netconf.api.NetconfMessage;
49 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
50 import org.opendaylight.netconf.api.xml.XmlUtil;
51 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
52 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemasResolver;
53 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
54 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
55 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
56 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
57 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
58 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
59 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
61 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
62 import org.opendaylight.yangtools.yang.common.QName;
63 import org.opendaylight.yangtools.yang.common.Revision;
64 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
66 import org.opendaylight.yangtools.yang.model.api.Module;
67 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
68 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
69 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
70 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
71 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
72 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
73 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
74 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
75 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
76 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
77 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
78 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
79 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
80 import org.xml.sax.SAXException;
81
82 @SuppressWarnings("checkstyle:IllegalCatch")
83 public class NetconfDeviceTest extends AbstractTestModelTest {
84
85     private static final NetconfMessage NOTIFICATION;
86
87     private static final ContainerNode COMPOSITE_NODE;
88
89     static {
90         try {
91             COMPOSITE_NODE = mockClass(ContainerNode.class);
92         } catch (final Exception e) {
93             throw new RuntimeException(e);
94         }
95         try {
96             NOTIFICATION = new NetconfMessage(XmlUtil
97                     .readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
98         } catch (SAXException | IOException e) {
99             throw new ExceptionInInitializerError(e);
100         }
101     }
102
103     private static final DOMRpcResult RPC_RESULT = new DefaultDOMRpcResult(COMPOSITE_NODE);
104
105     public static final String TEST_NAMESPACE = "test:namespace";
106     public static final String TEST_MODULE = "test-module";
107     public static final String TEST_REVISION = "2013-07-22";
108     public static final SourceIdentifier TEST_SID =
109             RevisionSourceIdentifier.create(TEST_MODULE, Revision.of(TEST_REVISION));
110     public static final String TEST_CAPABILITY =
111             TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION;
112
113     public static final SourceIdentifier TEST_SID2 =
114             RevisionSourceIdentifier.create(TEST_MODULE + "2", Revision.of(TEST_REVISION));
115     public static final String TEST_CAPABILITY2 =
116             TEST_NAMESPACE + "?module=" + TEST_MODULE + "2" + "&revision=" + TEST_REVISION;
117
118     private static final NetconfDeviceSchemasResolver STATE_SCHEMAS_RESOLVER =
119         (deviceRpc, remoteSessionCapabilities, id, schemaContext) -> NetconfStateSchemas.EMPTY;
120
121
122     @Test
123     public void testNetconfDeviceFlawedModelFailedResolution() throws Exception {
124         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
125         final NetconfDeviceCommunicator listener = getListener();
126
127         final SchemaContextFactory schemaFactory = getSchemaFactory();
128         final SchemaRepository schemaRepository = getSchemaRepository();
129
130         final SchemaResolutionException schemaResolutionException =
131                 new SchemaResolutionException("fail first", TEST_SID, new Throwable("YangTools parser fail"));
132         doAnswer(invocation -> {
133             if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
134                 return Futures.immediateFailedFuture(schemaResolutionException);
135             } else {
136                 return Futures.immediateFuture(SCHEMA_CONTEXT);
137             }
138         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
139
140         final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id,
141                 schemaContext) -> {
142             final Module first = Iterables.getFirst(SCHEMA_CONTEXT.getModules(), null);
143             final QName qName = QName.create(first.getQNameModule(), first.getName());
144             final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
145             final NetconfStateSchemas.RemoteYangSchema source2 =
146                     new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
147             return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
148         };
149
150         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
151                 .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
152
153         final NetconfDevice device = new NetconfDeviceBuilder()
154                 .setReconnectOnSchemasChange(true)
155                 .setSchemaResourcesDTO(schemaResourcesDTO)
156                 .setGlobalProcessingExecutor(getExecutor())
157                 .setId(getId())
158                 .setSalFacade(facade)
159                 .build();
160         // Monitoring supported
161         final NetconfSessionPreferences sessionCaps =
162                 getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
163         device.onRemoteSessionUp(sessionCaps, listener);
164
165         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(any(MountPointContext.class),
166             any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class), isNull());
167         Mockito.verify(schemaFactory, times(2)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
168     }
169
170     @Test
171     public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
172         final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
173
174         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
175         final NetconfDeviceCommunicator listener = getListener();
176
177         final SchemaContextFactory schemaFactory = getSchemaFactory();
178         final SchemaRepository schemaRepository = getSchemaRepository();
179
180         // Make fallback attempt to fail due to empty resolved sources
181         final SchemaResolutionException schemaResolutionException
182                 = new SchemaResolutionException("fail first",
183                 Collections.emptyList(), HashMultimap.create());
184         doReturn(Futures.immediateFailedFuture(schemaResolutionException))
185                 .when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
186
187         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
188                 .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, STATE_SCHEMAS_RESOLVER);
189         final NetconfDevice device = new NetconfDeviceBuilder()
190                 .setReconnectOnSchemasChange(true)
191                 .setSchemaResourcesDTO(schemaResourcesDTO)
192                 .setGlobalProcessingExecutor(getExecutor())
193                 .setId(getId())
194                 .setSalFacade(facade)
195                 .build();
196
197         // Monitoring not supported
198         final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
199         device.onRemoteSessionUp(sessionCaps, listener);
200
201         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceDisconnected();
202         Mockito.verify(listener, Mockito.timeout(5000)).close();
203         Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
204     }
205
206     @Test
207     public void testNetconfDeviceMissingSource() throws Exception {
208         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
209         final NetconfDeviceCommunicator listener = getListener();
210
211         final SchemaContextFactory schemaFactory = getSchemaFactory();
212         final SchemaRepository schemaRepository = getSchemaRepository();
213
214         // Make fallback attempt to fail due to empty resolved sources
215         final MissingSchemaSourceException schemaResolutionException =
216                 new MissingSchemaSourceException("fail first", TEST_SID);
217         doReturn(Futures.immediateFailedFuture(schemaResolutionException))
218                 .when(schemaRepository).getSchemaSource(eq(TEST_SID), eq(YangTextSchemaSource.class));
219         doAnswer(invocation -> {
220             if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
221                 return Futures.immediateFailedFuture(schemaResolutionException);
222             } else {
223                 return Futures.immediateFuture(SCHEMA_CONTEXT);
224             }
225         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
226
227         final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id,
228             schemaContext) -> {
229             final Module first = Iterables.getFirst(SCHEMA_CONTEXT.getModules(), null);
230             final QName qName = QName.create(first.getQNameModule(), first.getName());
231             final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
232             final NetconfStateSchemas.RemoteYangSchema source2 =
233                     new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
234             return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
235         };
236
237         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
238                 .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
239
240         final NetconfDevice device = new NetconfDeviceBuilder()
241                 .setReconnectOnSchemasChange(true)
242                 .setSchemaResourcesDTO(schemaResourcesDTO)
243                 .setGlobalProcessingExecutor(getExecutor())
244                 .setId(getId())
245                 .setSalFacade(facade)
246                 .build();
247         // Monitoring supported
248         final NetconfSessionPreferences sessionCaps =
249                 getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
250         device.onRemoteSessionUp(sessionCaps, listener);
251
252         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(any(MountPointContext.class),
253             any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class), isNull());
254         Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
255     }
256
257     private static SchemaSourceRegistry getSchemaRegistry() {
258         final SchemaSourceRegistry mock = mock(SchemaSourceRegistry.class);
259         final SchemaSourceRegistration<?> mockReg = mock(SchemaSourceRegistration.class);
260         doNothing().when(mockReg).close();
261         doReturn(mockReg).when(mock).registerSchemaSource(
262                 any(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider.class),
263                 any(PotentialSchemaSource.class));
264         return mock;
265     }
266
267     private static SchemaRepository getSchemaRepository() {
268         final SchemaRepository mock = mock(SchemaRepository.class);
269         final SchemaSourceRepresentation mockRep = mock(SchemaSourceRepresentation.class);
270         doReturn(Futures.immediateFuture(mockRep))
271                 .when(mock).getSchemaSource(any(SourceIdentifier.class), eq(YangTextSchemaSource.class));
272         return mock;
273     }
274
275     @Test
276     public void testNotificationBeforeSchema() throws Exception {
277         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
278         final NetconfDeviceCommunicator listener = getListener();
279         final SchemaContextFactory schemaContextProviderFactory = mock(SchemaContextFactory.class);
280         final SettableFuture<SchemaContext> schemaFuture = SettableFuture.create();
281         doReturn(schemaFuture).when(schemaContextProviderFactory).createSchemaContext(any(Collection.class));
282         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO =
283                 new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(),
284                         schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
285         final NetconfDevice device = new NetconfDeviceBuilder()
286                 .setReconnectOnSchemasChange(true)
287                 .setSchemaResourcesDTO(schemaResourcesDTO)
288                 .setGlobalProcessingExecutor(getExecutor())
289                 .setId(getId())
290                 .setSalFacade(facade)
291                 .build();
292
293         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
294                 Lists.newArrayList(TEST_CAPABILITY));
295         device.onRemoteSessionUp(sessionCaps, listener);
296
297         device.onNotification(NOTIFICATION);
298         device.onNotification(NOTIFICATION);
299         verify(facade, times(0)).onNotification(any(DOMNotification.class));
300
301         verify(facade, times(0)).onNotification(any(DOMNotification.class));
302         schemaFuture.set(NetconfToNotificationTest.getNotificationSchemaContext(getClass(), false));
303         verify(facade, timeout(10000).times(2)).onNotification(any(DOMNotification.class));
304
305         device.onNotification(NOTIFICATION);
306         verify(facade, timeout(10000).times(3)).onNotification(any(DOMNotification.class));
307     }
308
309     @Test
310     public void testNetconfDeviceReconnect() throws Exception {
311         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
312         final NetconfDeviceCommunicator listener = getListener();
313
314         final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
315
316         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
317                 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
318         final NetconfDevice device = new NetconfDeviceBuilder()
319                 .setReconnectOnSchemasChange(true)
320                 .setSchemaResourcesDTO(schemaResourcesDTO)
321                 .setGlobalProcessingExecutor(getExecutor())
322                 .setId(getId())
323                 .setSalFacade(facade)
324                 .build();
325         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
326                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
327         device.onRemoteSessionUp(sessionCaps, listener);
328
329         verify(schemaContextProviderFactory, timeout(5000)).createSchemaContext(any(Collection.class));
330         verify(facade, timeout(5000)).onDeviceConnected(
331                 any(MountPointContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class),
332                 isNull());
333
334         device.onRemoteSessionDown();
335         verify(facade, timeout(5000)).onDeviceDisconnected();
336
337         device.onRemoteSessionUp(sessionCaps, listener);
338
339         verify(schemaContextProviderFactory, timeout(5000).times(2)).createSchemaContext(any(Collection.class));
340         verify(facade, timeout(5000).times(2)).onDeviceConnected(
341                 any(MountPointContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class),
342                 isNull());
343     }
344
345     @Test
346     public void testNetconfDeviceDisconnectListenerCallCancellation() throws Exception {
347         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
348         final NetconfDeviceCommunicator listener = getListener();
349         final SchemaContextFactory schemaContextProviderFactory = mock(SchemaContextFactory.class);
350         final SettableFuture<SchemaContext> schemaFuture = SettableFuture.create();
351         doReturn(schemaFuture).when(schemaContextProviderFactory).createSchemaContext(any(Collection.class));
352         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
353                 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(),
354                 schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
355         final NetconfDevice device = new NetconfDeviceBuilder()
356                 .setReconnectOnSchemasChange(true)
357                 .setSchemaResourcesDTO(schemaResourcesDTO)
358                 .setGlobalProcessingExecutor(getExecutor())
359                 .setId(getId())
360                 .setSalFacade(facade)
361                 .build();
362         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
363                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
364         //session up, start schema resolution
365         device.onRemoteSessionUp(sessionCaps, listener);
366         //session closed
367         device.onRemoteSessionDown();
368         verify(facade, timeout(5000)).onDeviceDisconnected();
369         //complete schema setup
370         schemaFuture.set(SCHEMA_CONTEXT);
371         //facade.onDeviceDisconnected() was called, so facade.onDeviceConnected() shouldn't be called anymore
372         verify(facade, after(1000).never()).onDeviceConnected(any(), any(), any(), any(DOMActionService.class));
373     }
374
375     @Test
376     public void testNetconfDeviceAvailableCapabilitiesBuilding() throws Exception {
377         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
378         final NetconfDeviceCommunicator listener = getListener();
379
380         final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
381
382         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
383                 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
384         final NetconfDevice device = new NetconfDeviceBuilder()
385                 .setReconnectOnSchemasChange(true)
386                 .setSchemaResourcesDTO(schemaResourcesDTO)
387                 .setGlobalProcessingExecutor(getExecutor())
388                 .setId(getId())
389                 .setSalFacade(facade)
390                 .build();
391         final NetconfDevice netconfSpy = Mockito.spy(device);
392
393         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
394                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
395         final Map<QName, AvailableCapability.CapabilityOrigin> moduleBasedCaps = new HashMap<>();
396         moduleBasedCaps.putAll(sessionCaps.getModuleBasedCapsOrigin());
397         moduleBasedCaps
398                 .put(QName.create("(test:qname:side:loading)test"), AvailableCapability.CapabilityOrigin.UserDefined);
399
400         netconfSpy.onRemoteSessionUp(sessionCaps.replaceModuleCaps(moduleBasedCaps), listener);
401
402         final ArgumentCaptor<NetconfSessionPreferences> argument =
403                 ArgumentCaptor.forClass(NetconfSessionPreferences.class);
404         verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class), argument.capture(),
405             any(DOMRpcService.class), isNull());
406         final NetconfDeviceCapabilities netconfDeviceCaps = argument.getValue().getNetconfDeviceCapabilities();
407
408         netconfDeviceCaps.getResolvedCapabilities()
409                 .forEach(entry -> assertEquals("Builded 'AvailableCapability' schemas should match input capabilities.",
410                         moduleBasedCaps.get(
411                                 QName.create(entry.getCapability())).getName(), entry.getCapabilityOrigin().getName()));
412     }
413
414     private static SchemaContextFactory getSchemaFactory() throws Exception {
415         final SchemaContextFactory schemaFactory = mockClass(SchemaContextFactory.class);
416         doReturn(Futures.immediateFuture(SCHEMA_CONTEXT))
417                 .when(schemaFactory).createSchemaContext(any(Collection.class));
418         return schemaFactory;
419     }
420
421     private static RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
422         final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler =
423                 mockCloseableClass(RemoteDeviceHandler.class);
424         doNothing().when(remoteDeviceHandler).onDeviceConnected(
425                 any(MountPointContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class),
426                 any(DOMActionService.class));
427         doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
428         doNothing().when(remoteDeviceHandler).onNotification(any(DOMNotification.class));
429         return remoteDeviceHandler;
430     }
431
432     private static <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass)
433             throws Exception {
434         final T mock = mockClass(remoteDeviceHandlerClass);
435         doNothing().when(mock).close();
436         return mock;
437     }
438
439     private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
440         final T mock = mock(remoteDeviceHandlerClass);
441         Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
442         return mock;
443     }
444
445     public RemoteDeviceId getId() {
446         return new RemoteDeviceId("test-D", InetSocketAddress.createUnresolved("localhost", 22));
447     }
448
449     public ListeningExecutorService getExecutor() {
450         return MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
451     }
452
453     public MessageTransformer<NetconfMessage> getMessageTransformer() throws Exception {
454         final MessageTransformer<NetconfMessage> messageTransformer = mockClass(MessageTransformer.class);
455         doReturn(NOTIFICATION).when(messageTransformer).toRpcRequest(any(SchemaPath.class), any(NormalizedNode.class));
456         doReturn(RPC_RESULT).when(messageTransformer).toRpcResult(any(NetconfMessage.class), any(SchemaPath.class));
457         doReturn(COMPOSITE_NODE).when(messageTransformer).toNotification(any(NetconfMessage.class));
458         return messageTransformer;
459     }
460
461     public NetconfSessionPreferences getSessionCaps(final boolean addMonitor,
462                                                     final Collection<String> additionalCapabilities) {
463         final ArrayList<String> capabilities = Lists.newArrayList(
464                 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
465                 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
466
467         if (addMonitor) {
468             capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
469         }
470
471         capabilities.addAll(additionalCapabilities);
472
473         return NetconfSessionPreferences.fromStrings(
474                 capabilities);
475     }
476
477     public NetconfDeviceCommunicator getListener() throws Exception {
478         final NetconfDeviceCommunicator remoteDeviceCommunicator = mockCloseableClass(NetconfDeviceCommunicator.class);
479 //        doReturn(Futures.immediateFuture(rpcResult))
480 //                .when(remoteDeviceCommunicator).sendRequest(any(NetconfMessage.class), any(QName.class));
481         return remoteDeviceCommunicator;
482     }
483 }