e5f2830d120dc2f928b87bae67c5fc43bc8cbdf3
[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.yang.common.QName;
62 import org.opendaylight.yangtools.yang.common.Revision;
63 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
65 import org.opendaylight.yangtools.yang.model.api.Module;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
68 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
69 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
70 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
71 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
72 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
73 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
74 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
75 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
76 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
77 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
78 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
79 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
80 import org.xml.sax.SAXException;
81
82 @SuppressWarnings("checkstyle:IllegalCatch")
83 public class NetconfDeviceTest {
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) -> NetconfStateSchemas.EMPTY;
120
121     @Test
122     public void testNetconfDeviceFlawedModelFailedResolution() throws Exception {
123         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
124         final NetconfDeviceCommunicator listener = getListener();
125
126         final SchemaContextFactory schemaFactory = getSchemaFactory();
127         final SchemaContext schema = getSchema();
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);
137             }
138         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
139
140         final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id) -> {
141             final Module first = Iterables.getFirst(schema.getModules(), null);
142             final QName qName = QName.create(first.getQNameModule(), first.getName());
143             final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
144             final NetconfStateSchemas.RemoteYangSchema source2 =
145                     new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
146             return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
147         };
148
149         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
150                 .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
151
152         final NetconfDevice device = new NetconfDeviceBuilder()
153                 .setReconnectOnSchemasChange(true)
154                 .setSchemaResourcesDTO(schemaResourcesDTO)
155                 .setGlobalProcessingExecutor(getExecutor())
156                 .setId(getId())
157                 .setSalFacade(facade)
158                 .build();
159         // Monitoring supported
160         final NetconfSessionPreferences sessionCaps =
161                 getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
162         device.onRemoteSessionUp(sessionCaps, listener);
163
164         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(
165                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class), isNull());
166         Mockito.verify(schemaFactory, times(2)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
167     }
168
169     @Test
170     public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
171         final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
172
173         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
174         final NetconfDeviceCommunicator listener = getListener();
175
176         final SchemaContextFactory schemaFactory = getSchemaFactory();
177         final SchemaRepository schemaRepository = getSchemaRepository();
178
179         // Make fallback attempt to fail due to empty resolved sources
180         final SchemaResolutionException schemaResolutionException
181                 = new SchemaResolutionException("fail first",
182                 Collections.emptyList(), HashMultimap.create());
183         doReturn(Futures.immediateFailedFuture(schemaResolutionException))
184                 .when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
185
186         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
187                 .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, STATE_SCHEMAS_RESOLVER);
188         final NetconfDevice device = new NetconfDeviceBuilder()
189                 .setReconnectOnSchemasChange(true)
190                 .setSchemaResourcesDTO(schemaResourcesDTO)
191                 .setGlobalProcessingExecutor(getExecutor())
192                 .setId(getId())
193                 .setSalFacade(facade)
194                 .build();
195
196         // Monitoring not supported
197         final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
198         device.onRemoteSessionUp(sessionCaps, listener);
199
200         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceDisconnected();
201         Mockito.verify(listener, Mockito.timeout(5000)).close();
202         Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
203     }
204
205     @Test
206     public void testNetconfDeviceMissingSource() throws Exception {
207         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
208         final NetconfDeviceCommunicator listener = getListener();
209         final SchemaContext schema = getSchema();
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);
224             }
225         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
226
227         final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id) -> {
228             final Module first = Iterables.getFirst(schema.getModules(), null);
229             final QName qName = QName.create(first.getQNameModule(), first.getName());
230             final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
231             final NetconfStateSchemas.RemoteYangSchema source2 =
232                     new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
233             return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
234         };
235
236         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
237                 .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
238
239         final NetconfDevice device = new NetconfDeviceBuilder()
240                 .setReconnectOnSchemasChange(true)
241                 .setSchemaResourcesDTO(schemaResourcesDTO)
242                 .setGlobalProcessingExecutor(getExecutor())
243                 .setId(getId())
244                 .setSalFacade(facade)
245                 .build();
246         // Monitoring supported
247         final NetconfSessionPreferences sessionCaps =
248                 getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
249         device.onRemoteSessionUp(sessionCaps, listener);
250
251         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(
252                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class), isNull());
253         Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
254     }
255
256     private static SchemaSourceRegistry getSchemaRegistry() {
257         final SchemaSourceRegistry mock = mock(SchemaSourceRegistry.class);
258         final SchemaSourceRegistration<?> mockReg = mock(SchemaSourceRegistration.class);
259         doNothing().when(mockReg).close();
260         doReturn(mockReg).when(mock).registerSchemaSource(
261                 any(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider.class),
262                 any(PotentialSchemaSource.class));
263         return mock;
264     }
265
266     private static SchemaRepository getSchemaRepository() {
267         final SchemaRepository mock = mock(SchemaRepository.class);
268         final SchemaSourceRepresentation mockRep = mock(SchemaSourceRepresentation.class);
269         doReturn(Futures.immediateFuture(mockRep))
270                 .when(mock).getSchemaSource(any(SourceIdentifier.class), eq(YangTextSchemaSource.class));
271         return mock;
272     }
273
274     @Test
275     public void testNotificationBeforeSchema() throws Exception {
276         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
277         final NetconfDeviceCommunicator listener = getListener();
278         final SchemaContextFactory schemaContextProviderFactory = mock(SchemaContextFactory.class);
279         final SettableFuture<SchemaContext> schemaFuture = SettableFuture.create();
280         doReturn(schemaFuture).when(schemaContextProviderFactory).createSchemaContext(any(Collection.class));
281         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO =
282                 new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(),
283                         schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
284         final NetconfDevice device = new NetconfDeviceBuilder()
285                 .setReconnectOnSchemasChange(true)
286                 .setSchemaResourcesDTO(schemaResourcesDTO)
287                 .setGlobalProcessingExecutor(getExecutor())
288                 .setId(getId())
289                 .setSalFacade(facade)
290                 .build();
291
292         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
293                 Lists.newArrayList(TEST_CAPABILITY));
294         device.onRemoteSessionUp(sessionCaps, listener);
295
296         device.onNotification(NOTIFICATION);
297         device.onNotification(NOTIFICATION);
298         verify(facade, times(0)).onNotification(any(DOMNotification.class));
299
300         verify(facade, times(0)).onNotification(any(DOMNotification.class));
301         schemaFuture.set(NetconfToNotificationTest.getNotificationSchemaContext(getClass(), false));
302         verify(facade, timeout(10000).times(2)).onNotification(any(DOMNotification.class));
303
304         device.onNotification(NOTIFICATION);
305         verify(facade, timeout(10000).times(3)).onNotification(any(DOMNotification.class));
306     }
307
308     @Test
309     public void testNetconfDeviceReconnect() throws Exception {
310         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
311         final NetconfDeviceCommunicator listener = getListener();
312
313         final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
314
315         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
316                 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
317         final NetconfDevice device = new NetconfDeviceBuilder()
318                 .setReconnectOnSchemasChange(true)
319                 .setSchemaResourcesDTO(schemaResourcesDTO)
320                 .setGlobalProcessingExecutor(getExecutor())
321                 .setId(getId())
322                 .setSalFacade(facade)
323                 .build();
324         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
325                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
326         device.onRemoteSessionUp(sessionCaps, listener);
327
328         verify(schemaContextProviderFactory, timeout(5000)).createSchemaContext(any(Collection.class));
329         verify(facade, timeout(5000)).onDeviceConnected(
330                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class),
331                 isNull());
332
333         device.onRemoteSessionDown();
334         verify(facade, timeout(5000)).onDeviceDisconnected();
335
336         device.onRemoteSessionUp(sessionCaps, listener);
337
338         verify(schemaContextProviderFactory, timeout(5000).times(2)).createSchemaContext(any(Collection.class));
339         verify(facade, timeout(5000).times(2)).onDeviceConnected(
340                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class),
341                 isNull());
342     }
343
344     @Test
345     public void testNetconfDeviceDisconnectListenerCallCancellation() throws Exception {
346         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
347         final NetconfDeviceCommunicator listener = getListener();
348         final SchemaContextFactory schemaContextProviderFactory = mock(SchemaContextFactory.class);
349         final SettableFuture<SchemaContext> schemaFuture = SettableFuture.create();
350         doReturn(schemaFuture).when(schemaContextProviderFactory).createSchemaContext(any(Collection.class));
351         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
352                 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(),
353                 schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
354         final NetconfDevice device = new NetconfDeviceBuilder()
355                 .setReconnectOnSchemasChange(true)
356                 .setSchemaResourcesDTO(schemaResourcesDTO)
357                 .setGlobalProcessingExecutor(getExecutor())
358                 .setId(getId())
359                 .setSalFacade(facade)
360                 .build();
361         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
362                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
363         //session up, start schema resolution
364         device.onRemoteSessionUp(sessionCaps, listener);
365         //session closed
366         device.onRemoteSessionDown();
367         verify(facade, timeout(5000)).onDeviceDisconnected();
368         //complete schema setup
369         schemaFuture.set(getSchema());
370         //facade.onDeviceDisconnected() was called, so facade.onDeviceConnected() shouldn't be called anymore
371         verify(facade, after(1000).never()).onDeviceConnected(any(), any(), any(), any(DOMActionService.class));
372     }
373
374     @Test
375     public void testNetconfDeviceAvailableCapabilitiesBuilding() throws Exception {
376         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
377         final NetconfDeviceCommunicator listener = getListener();
378
379         final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
380
381         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
382                 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
383         final NetconfDevice device = new NetconfDeviceBuilder()
384                 .setReconnectOnSchemasChange(true)
385                 .setSchemaResourcesDTO(schemaResourcesDTO)
386                 .setGlobalProcessingExecutor(getExecutor())
387                 .setId(getId())
388                 .setSalFacade(facade)
389                 .build();
390         final NetconfDevice netconfSpy = Mockito.spy(device);
391
392         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
393                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
394         final Map<QName, AvailableCapability.CapabilityOrigin> moduleBasedCaps = new HashMap<>();
395         moduleBasedCaps.putAll(sessionCaps.getModuleBasedCapsOrigin());
396         moduleBasedCaps
397                 .put(QName.create("(test:qname:side:loading)test"), AvailableCapability.CapabilityOrigin.UserDefined);
398
399         netconfSpy.onRemoteSessionUp(sessionCaps.replaceModuleCaps(moduleBasedCaps), listener);
400
401         final ArgumentCaptor<NetconfSessionPreferences> argument =
402                 ArgumentCaptor.forClass(NetconfSessionPreferences.class);
403         verify(facade, timeout(5000))
404                 .onDeviceConnected(any(SchemaContext.class), argument.capture(), any(DOMRpcService.class), isNull());
405         final NetconfDeviceCapabilities netconfDeviceCaps = argument.getValue().getNetconfDeviceCapabilities();
406
407         netconfDeviceCaps.getResolvedCapabilities()
408                 .forEach(entry -> assertEquals("Builded 'AvailableCapability' schemas should match input capabilities.",
409                         moduleBasedCaps.get(
410                                 QName.create(entry.getCapability())).getName(), entry.getCapabilityOrigin().getName()));
411     }
412
413     private static SchemaContextFactory getSchemaFactory() throws Exception {
414         final SchemaContextFactory schemaFactory = mockClass(SchemaContextFactory.class);
415         doReturn(Futures.immediateFuture(getSchema()))
416                 .when(schemaFactory).createSchemaContext(any(Collection.class));
417         return schemaFactory;
418     }
419
420     public static SchemaContext getSchema() {
421         return YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
422     }
423
424     private static RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
425         final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler =
426                 mockCloseableClass(RemoteDeviceHandler.class);
427         doNothing().when(remoteDeviceHandler).onDeviceConnected(
428                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class),
429                 any(DOMActionService.class));
430         doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
431         doNothing().when(remoteDeviceHandler).onNotification(any(DOMNotification.class));
432         return remoteDeviceHandler;
433     }
434
435     private static <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass)
436             throws Exception {
437         final T mock = mockClass(remoteDeviceHandlerClass);
438         doNothing().when(mock).close();
439         return mock;
440     }
441
442     private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
443         final T mock = mock(remoteDeviceHandlerClass);
444         Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
445         return mock;
446     }
447
448     public RemoteDeviceId getId() {
449         return new RemoteDeviceId("test-D", InetSocketAddress.createUnresolved("localhost", 22));
450     }
451
452     public ListeningExecutorService getExecutor() {
453         return MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
454     }
455
456     public MessageTransformer<NetconfMessage> getMessageTransformer() throws Exception {
457         final MessageTransformer<NetconfMessage> messageTransformer = mockClass(MessageTransformer.class);
458         doReturn(NOTIFICATION).when(messageTransformer).toRpcRequest(any(SchemaPath.class), any(NormalizedNode.class));
459         doReturn(RPC_RESULT).when(messageTransformer).toRpcResult(any(NetconfMessage.class), any(SchemaPath.class));
460         doReturn(COMPOSITE_NODE).when(messageTransformer).toNotification(any(NetconfMessage.class));
461         return messageTransformer;
462     }
463
464     public NetconfSessionPreferences getSessionCaps(final boolean addMonitor,
465                                                     final Collection<String> additionalCapabilities) {
466         final ArrayList<String> capabilities = Lists.newArrayList(
467                 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
468                 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
469
470         if (addMonitor) {
471             capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
472         }
473
474         capabilities.addAll(additionalCapabilities);
475
476         return NetconfSessionPreferences.fromStrings(
477                 capabilities);
478     }
479
480     public NetconfDeviceCommunicator getListener() throws Exception {
481         final NetconfDeviceCommunicator remoteDeviceCommunicator = mockCloseableClass(NetconfDeviceCommunicator.class);
482 //        doReturn(Futures.immediateFuture(rpcResult))
483 //                .when(remoteDeviceCommunicator).sendRequest(any(NetconfMessage.class), any(QName.class));
484         return remoteDeviceCommunicator;
485     }
486 }