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