Merge "Remove use of ThreadPools in sal-netconf-connector"
[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.Matchers.any;
12 import static org.mockito.Matchers.anyCollectionOf;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.after;
15 import static org.mockito.Mockito.doAnswer;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.timeout;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22
23 import com.google.common.collect.HashMultimap;
24 import com.google.common.collect.Iterables;
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Sets;
27 import com.google.common.util.concurrent.Futures;
28 import com.google.common.util.concurrent.ListeningExecutorService;
29 import com.google.common.util.concurrent.MoreExecutors;
30 import com.google.common.util.concurrent.SettableFuture;
31 import java.io.IOException;
32 import java.net.InetSocketAddress;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.Map;
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.DOMActionService;
43 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
44 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
45 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
46 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
47 import org.opendaylight.netconf.api.NetconfMessage;
48 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
49 import org.opendaylight.netconf.api.xml.XmlUtil;
50 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
51 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemasResolver;
52 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
53 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
54 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
55 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
56 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
57 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
58 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
60 import org.opendaylight.yangtools.yang.common.QName;
61 import org.opendaylight.yangtools.yang.common.Revision;
62 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
64 import org.opendaylight.yangtools.yang.model.api.Module;
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                 any(DOMActionService.class));
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),
253                 any(DOMActionService.class));
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(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class),
332                 any(DOMActionService.class));
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(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class),
342                 any(DOMActionService.class));
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(getSchema());
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))
405                 .onDeviceConnected(any(SchemaContext.class), argument.capture(), any(DOMRpcService.class),
406                         any(DOMActionService.class));
407         final NetconfDeviceCapabilities netconfDeviceCaps = argument.getValue().getNetconfDeviceCapabilities();
408
409         netconfDeviceCaps.getResolvedCapabilities()
410                 .forEach(entry -> assertEquals("Builded 'AvailableCapability' schemas should match input capabilities.",
411                         moduleBasedCaps.get(
412                                 QName.create(entry.getCapability())).getName(), entry.getCapabilityOrigin().getName()));
413     }
414
415     private static SchemaContextFactory getSchemaFactory() throws Exception {
416         final SchemaContextFactory schemaFactory = mockClass(SchemaContextFactory.class);
417         doReturn(Futures.immediateFuture(getSchema()))
418                 .when(schemaFactory).createSchemaContext(any(Collection.class));
419         return schemaFactory;
420     }
421
422     public static SchemaContext getSchema() {
423         return YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
424     }
425
426     private static RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
427         final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler =
428                 mockCloseableClass(RemoteDeviceHandler.class);
429         doNothing().when(remoteDeviceHandler).onDeviceConnected(
430                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class),
431                 any(DOMActionService.class));
432         doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
433         doNothing().when(remoteDeviceHandler).onNotification(any(DOMNotification.class));
434         return remoteDeviceHandler;
435     }
436
437     private static <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass)
438             throws Exception {
439         final T mock = mockClass(remoteDeviceHandlerClass);
440         doNothing().when(mock).close();
441         return mock;
442     }
443
444     private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
445         final T mock = mock(remoteDeviceHandlerClass);
446         Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
447         return mock;
448     }
449
450     public RemoteDeviceId getId() {
451         return new RemoteDeviceId("test-D", InetSocketAddress.createUnresolved("localhost", 22));
452     }
453
454     public ListeningExecutorService getExecutor() {
455         return MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
456     }
457
458     public MessageTransformer<NetconfMessage> getMessageTransformer() throws Exception {
459         final MessageTransformer<NetconfMessage> messageTransformer = mockClass(MessageTransformer.class);
460         doReturn(NOTIFICATION).when(messageTransformer).toRpcRequest(any(SchemaPath.class), any(NormalizedNode.class));
461         doReturn(RPC_RESULT).when(messageTransformer).toRpcResult(any(NetconfMessage.class), any(SchemaPath.class));
462         doReturn(COMPOSITE_NODE).when(messageTransformer).toNotification(any(NetconfMessage.class));
463         return messageTransformer;
464     }
465
466     public NetconfSessionPreferences getSessionCaps(final boolean addMonitor,
467                                                     final Collection<String> additionalCapabilities) {
468         final ArrayList<String> capabilities = Lists.newArrayList(
469                 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
470                 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
471
472         if (addMonitor) {
473             capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
474         }
475
476         capabilities.addAll(additionalCapabilities);
477
478         return NetconfSessionPreferences.fromStrings(
479                 capabilities);
480     }
481
482     public NetconfDeviceCommunicator getListener() throws Exception {
483         final NetconfDeviceCommunicator remoteDeviceCommunicator = mockCloseableClass(NetconfDeviceCommunicator.class);
484 //        doReturn(Futures.immediateFuture(rpcResult))
485 //                .when(remoteDeviceCommunicator).sendRequest(any(NetconfMessage.class), any(QName.class));
486         return remoteDeviceCommunicator;
487     }
488 }