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