Add 'features/protocol-framework/' from commit 'cb42405784db97d0ce2c5991d12a89b46d185949'
[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.base.Optional;
25 import com.google.common.collect.HashMultimap;
26 import com.google.common.collect.Iterables;
27 import com.google.common.collect.Lists;
28 import com.google.common.collect.Sets;
29 import com.google.common.util.concurrent.Futures;
30 import com.google.common.util.concurrent.SettableFuture;
31 import java.io.IOException;
32 import java.io.InputStream;
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.List;
39 import java.util.Map;
40 import java.util.concurrent.ExecutorService;
41 import java.util.concurrent.Executors;
42 import org.junit.Test;
43 import org.mockito.ArgumentCaptor;
44 import org.mockito.Mockito;
45 import org.opendaylight.controller.config.util.xml.XmlUtil;
46 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
47 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
48 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
49 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
50 import org.opendaylight.netconf.api.NetconfMessage;
51 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
52 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
53 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemasResolver;
54 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
55 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
56 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
57 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
58 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
59 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
60 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
62 import org.opendaylight.yangtools.yang.common.QName;
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.ModuleImport;
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.MissingSchemaSourceException;
70 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
71 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
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.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.parser.util.ASTSchemaSource;
80 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
81 import org.xml.sax.SAXException;
82
83 @SuppressWarnings("checkstyle:IllegalCatch")
84 public class NetconfDeviceTest {
85
86     private static final NetconfMessage NOTIFICATION;
87
88     private static final ContainerNode COMPOSITE_NODE;
89
90     static {
91         try {
92             COMPOSITE_NODE = mockClass(ContainerNode.class);
93         } catch (final Exception e) {
94             throw new RuntimeException(e);
95         }
96         try {
97             NOTIFICATION = new NetconfMessage(XmlUtil
98                     .readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
99         } catch (SAXException | IOException e) {
100             throw new ExceptionInInitializerError(e);
101         }
102     }
103
104     private static final DOMRpcResult RPC_RESULT = new DefaultDOMRpcResult(COMPOSITE_NODE);
105
106     public static final String TEST_NAMESPACE = "test:namespace";
107     public static final String TEST_MODULE = "test-module";
108     public static final String TEST_REVISION = "2013-07-22";
109     public static final SourceIdentifier TEST_SID =
110             RevisionSourceIdentifier.create(TEST_MODULE, Optional.of(TEST_REVISION));
111     public static final String TEST_CAPABILITY =
112             TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION;
113
114     public static final SourceIdentifier TEST_SID2 =
115             RevisionSourceIdentifier.create(TEST_MODULE + "2", Optional.of(TEST_REVISION));
116     public static final String TEST_CAPABILITY2 =
117             TEST_NAMESPACE + "?module=" + TEST_MODULE + "2" + "&revision=" + TEST_REVISION;
118
119     private static final NetconfDeviceSchemasResolver STATE_SCHEMAS_RESOLVER =
120         (deviceRpc, remoteSessionCapabilities, id) -> NetconfStateSchemas.EMPTY;
121
122     @Test
123     public void testNetconfDeviceFlawedModelFailedResolution() throws Exception {
124         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
125         final NetconfDeviceCommunicator listener = getListener();
126
127         final SchemaContextFactory schemaFactory = getSchemaFactory();
128         final SchemaContext schema = getSchema();
129         final SchemaRepository schemaRepository = getSchemaRepository();
130
131         final SchemaResolutionException schemaResolutionException =
132                 new SchemaResolutionException("fail first", TEST_SID, new Throwable("YangTools parser fail"));
133         doAnswer(invocation -> {
134             if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
135                 return Futures.immediateFailedCheckedFuture(schemaResolutionException);
136             } else {
137                 return Futures.immediateCheckedFuture(schema);
138             }
139         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
140
141         final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id) -> {
142             final Module first = Iterables.getFirst(schema.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                 .build();
160         // Monitoring supported
161         final NetconfSessionPreferences sessionCaps =
162                 getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
163         device.onRemoteSessionUp(sessionCaps, listener);
164
165         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(
166                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
167         Mockito.verify(schemaFactory, times(2)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
168     }
169
170     @Test
171     public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
172         final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
173
174         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
175         final NetconfDeviceCommunicator listener = getListener();
176
177         final SchemaContextFactory schemaFactory = getSchemaFactory();
178         final SchemaRepository schemaRepository = getSchemaRepository();
179
180         // Make fallback attempt to fail due to empty resolved sources
181         final SchemaResolutionException schemaResolutionException
182                 = new SchemaResolutionException("fail first",
183                 Collections.<SourceIdentifier>emptyList(), HashMultimap.<SourceIdentifier, ModuleImport>create());
184         doReturn(Futures.immediateFailedCheckedFuture(
185                 schemaResolutionException))
186                 .when(schemaFactory).createSchemaContext(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                 .build();
197
198         // Monitoring not supported
199         final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
200         device.onRemoteSessionUp(sessionCaps, listener);
201
202         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceDisconnected();
203         Mockito.verify(listener, Mockito.timeout(5000)).close();
204         Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
205     }
206
207     @Test
208     public void testNetconfDeviceMissingSource() throws Exception {
209         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
210         final NetconfDeviceCommunicator listener = getListener();
211         final SchemaContext schema = getSchema();
212
213         final SchemaContextFactory 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.immediateFailedCheckedFuture(schemaResolutionException))
220                 .when(schemaRepository).getSchemaSource(eq(TEST_SID), eq(ASTSchemaSource.class));
221         doAnswer(invocation -> {
222             if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
223                 return Futures.immediateFailedCheckedFuture(schemaResolutionException);
224             } else {
225                 return Futures.immediateCheckedFuture(schema);
226             }
227         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
228
229         final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id) -> {
230             final Module first = Iterables.getFirst(schema.getModules(), null);
231             final QName qName = QName.create(first.getQNameModule(), first.getName());
232             final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
233             final NetconfStateSchemas.RemoteYangSchema source2 =
234                     new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
235             return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
236         };
237
238         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
239                 .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
240
241         final NetconfDevice device = new NetconfDeviceBuilder()
242                 .setReconnectOnSchemasChange(true)
243                 .setSchemaResourcesDTO(schemaResourcesDTO)
244                 .setGlobalProcessingExecutor(getExecutor())
245                 .setId(getId())
246                 .setSalFacade(facade)
247                 .build();
248         // Monitoring supported
249         final NetconfSessionPreferences sessionCaps =
250                 getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
251         device.onRemoteSessionUp(sessionCaps, listener);
252
253         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(
254                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
255         Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
256     }
257
258     private static SchemaSourceRegistry getSchemaRegistry() {
259         final SchemaSourceRegistry mock = mock(SchemaSourceRegistry.class);
260         final SchemaSourceRegistration<?> mockReg = mock(SchemaSourceRegistration.class);
261         doNothing().when(mockReg).close();
262         doReturn(mockReg).when(mock).registerSchemaSource(
263                 any(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider.class),
264                 any(PotentialSchemaSource.class));
265         return mock;
266     }
267
268     private static SchemaRepository getSchemaRepository() {
269         final SchemaRepository mock = mock(SchemaRepository.class);
270         final SchemaSourceRepresentation mockRep = mock(SchemaSourceRepresentation.class);
271         doReturn(Futures.immediateCheckedFuture(mockRep))
272                 .when(mock).getSchemaSource(any(SourceIdentifier.class), eq(ASTSchemaSource.class));
273         return mock;
274     }
275
276     @Test
277     public void testNotificationBeforeSchema() throws Exception {
278         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
279         final NetconfDeviceCommunicator listener = getListener();
280         final SchemaContextFactory schemaContextProviderFactory = mock(SchemaContextFactory.class);
281         final SettableFuture<SchemaContext> schemaFuture = SettableFuture.create();
282         doReturn(Futures.makeChecked(schemaFuture, e -> new SchemaResolutionException("fail")))
283                 .when(schemaContextProviderFactory).createSchemaContext(any(Collection.class));
284         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO =
285                 new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(),
286                         schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
287         final NetconfDevice device = new NetconfDeviceBuilder()
288                 .setReconnectOnSchemasChange(true)
289                 .setSchemaResourcesDTO(schemaResourcesDTO)
290                 .setGlobalProcessingExecutor(getExecutor())
291                 .setId(getId())
292                 .setSalFacade(facade)
293                 .build();
294
295         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
296                 Lists.newArrayList(TEST_CAPABILITY));
297         device.onRemoteSessionUp(sessionCaps, listener);
298
299         device.onNotification(NOTIFICATION);
300         device.onNotification(NOTIFICATION);
301         verify(facade, times(0)).onNotification(any(DOMNotification.class));
302
303         verify(facade, times(0)).onNotification(any(DOMNotification.class));
304         schemaFuture.set(NetconfToNotificationTest.getNotificationSchemaContext(getClass(), false));
305         verify(facade, timeout(10000).times(2)).onNotification(any(DOMNotification.class));
306
307         device.onNotification(NOTIFICATION);
308         verify(facade, timeout(10000).times(3)).onNotification(any(DOMNotification.class));
309     }
310
311     @Test
312     public void testNetconfDeviceReconnect() throws Exception {
313         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
314         final NetconfDeviceCommunicator listener = getListener();
315
316         final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
317
318         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
319                 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
320         final NetconfDevice device = new NetconfDeviceBuilder()
321                 .setReconnectOnSchemasChange(true)
322                 .setSchemaResourcesDTO(schemaResourcesDTO)
323                 .setGlobalProcessingExecutor(getExecutor())
324                 .setId(getId())
325                 .setSalFacade(facade)
326                 .build();
327         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
328                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
329         device.onRemoteSessionUp(sessionCaps, listener);
330
331         verify(schemaContextProviderFactory, timeout(5000)).createSchemaContext(any(Collection.class));
332         verify(facade, timeout(5000)).onDeviceConnected(
333                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
334
335         device.onRemoteSessionDown();
336         verify(facade, timeout(5000)).onDeviceDisconnected();
337
338         device.onRemoteSessionUp(sessionCaps, listener);
339
340         verify(schemaContextProviderFactory, timeout(5000).times(2)).createSchemaContext(any(Collection.class));
341         verify(facade, timeout(5000).times(2)).onDeviceConnected(
342                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.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(Futures.makeChecked(schemaFuture, e -> new SchemaResolutionException("fail")))
352                 .when(schemaContextProviderFactory).createSchemaContext(any(Collection.class));
353         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
354                 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(),
355                 schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
356         final NetconfDevice device = new NetconfDeviceBuilder()
357                 .setReconnectOnSchemasChange(true)
358                 .setSchemaResourcesDTO(schemaResourcesDTO)
359                 .setGlobalProcessingExecutor(getExecutor())
360                 .setId(getId())
361                 .setSalFacade(facade)
362                 .build();
363         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
364                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
365         //session up, start schema resolution
366         device.onRemoteSessionUp(sessionCaps, listener);
367         //session closed
368         device.onRemoteSessionDown();
369         verify(facade, timeout(5000)).onDeviceDisconnected();
370         //complete schema setup
371         schemaFuture.set(getSchema());
372         //facade.onDeviceDisconnected() was called, so facade.onDeviceConnected() shouldn't be called anymore
373         verify(facade, after(1000).never()).onDeviceConnected(any(), any(), any());
374     }
375
376     @Test
377     public void testNetconfDeviceAvailableCapabilitiesBuilding() throws Exception {
378         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
379         final NetconfDeviceCommunicator listener = getListener();
380
381         final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
382
383         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
384                 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
385         final NetconfDevice device = new NetconfDeviceBuilder()
386                 .setReconnectOnSchemasChange(true)
387                 .setSchemaResourcesDTO(schemaResourcesDTO)
388                 .setGlobalProcessingExecutor(getExecutor())
389                 .setId(getId())
390                 .setSalFacade(facade)
391                 .build();
392         final NetconfDevice netconfSpy = Mockito.spy(device);
393
394         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
395                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
396         final Map<QName, AvailableCapability.CapabilityOrigin> moduleBasedCaps = new HashMap<>();
397         moduleBasedCaps.putAll(sessionCaps.getModuleBasedCapsOrigin());
398         moduleBasedCaps
399                 .put(QName.create("(test:qname:side:loading)test"), AvailableCapability.CapabilityOrigin.UserDefined);
400
401         netconfSpy.onRemoteSessionUp(sessionCaps.replaceModuleCaps(moduleBasedCaps), listener);
402
403         final ArgumentCaptor<NetconfSessionPreferences> argument =
404                 ArgumentCaptor.forClass(NetconfSessionPreferences.class);
405         verify(facade, timeout(5000))
406                 .onDeviceConnected(any(SchemaContext.class), argument.capture(), any(DOMRpcService.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.immediateCheckedFuture(getSchema()))
418                 .when(schemaFactory).createSchemaContext(any(Collection.class));
419         return schemaFactory;
420     }
421
422     public static SchemaContext getSchema() throws Exception {
423         final List<InputStream> modelsToParse = Lists.newArrayList(
424                 NetconfDeviceTest.class.getResourceAsStream("/schemas/test-module.yang")
425         );
426         return YangParserTestUtils.parseYangStreams(modelsToParse);
427     }
428
429     private static RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
430         final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler =
431                 mockCloseableClass(RemoteDeviceHandler.class);
432         doNothing().when(remoteDeviceHandler).onDeviceConnected(
433                 any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.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 ExecutorService getExecutor() {
457         return 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 }