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