2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.anyCollection;
15 import static org.mockito.ArgumentMatchers.eq;
16 import static org.mockito.ArgumentMatchers.isNull;
17 import static org.mockito.Mockito.after;
18 import static org.mockito.Mockito.doAnswer;
19 import static org.mockito.Mockito.doNothing;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.timeout;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
27 import com.google.common.collect.HashMultimap;
28 import com.google.common.collect.Iterables;
29 import com.google.common.collect.Lists;
30 import com.google.common.collect.Sets;
31 import com.google.common.util.concurrent.Futures;
32 import com.google.common.util.concurrent.ListeningExecutorService;
33 import com.google.common.util.concurrent.MoreExecutors;
34 import com.google.common.util.concurrent.SettableFuture;
35 import java.io.IOException;
36 import java.net.InetSocketAddress;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.List;
45 import java.util.concurrent.Executors;
46 import org.junit.Test;
47 import org.mockito.ArgumentCaptor;
48 import org.opendaylight.mdsal.dom.api.DOMActionService;
49 import org.opendaylight.mdsal.dom.api.DOMNotification;
50 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
51 import org.opendaylight.mdsal.dom.api.DOMRpcService;
52 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
53 import org.opendaylight.netconf.api.NetconfMessage;
54 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
55 import org.opendaylight.netconf.api.xml.XmlUtil;
56 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
57 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemasResolver;
58 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
59 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
60 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
61 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
62 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
63 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
64 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
66 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
67 import org.opendaylight.yangtools.yang.common.QName;
68 import org.opendaylight.yangtools.yang.common.Revision;
69 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
70 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
71 import org.opendaylight.yangtools.yang.model.api.Module;
72 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
73 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
74 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
75 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
76 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
77 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
78 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
79 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
80 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
81 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
82 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
83 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
84 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
85 import org.xml.sax.SAXException;
87 public class NetconfDeviceTest extends AbstractTestModelTest {
89 private static final NetconfMessage NOTIFICATION;
91 private static final ContainerNode COMPOSITE_NODE = mockClass(ContainerNode.class);
95 NOTIFICATION = new NetconfMessage(XmlUtil
96 .readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
97 } catch (SAXException | IOException e) {
98 throw new ExceptionInInitializerError(e);
102 private static final DOMRpcResult RPC_RESULT = new DefaultDOMRpcResult(COMPOSITE_NODE);
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;
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;
117 private static final NetconfDeviceSchemasResolver STATE_SCHEMAS_RESOLVER =
118 (deviceRpc, remoteSessionCapabilities, id, schemaContext) -> NetconfStateSchemas.EMPTY;
122 public void testNetconfDeviceFlawedModelFailedResolution() throws Exception {
123 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
124 final NetconfDeviceCommunicator listener = getListener();
126 final EffectiveModelContextFactory schemaFactory = getSchemaFactory();
127 final SchemaRepository schemaRepository = getSchemaRepository();
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);
135 return Futures.immediateFuture(SCHEMA_CONTEXT);
137 }).when(schemaFactory).createEffectiveModelContext(anyCollection());
139 final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id,
141 final Module first = Iterables.getFirst(SCHEMA_CONTEXT.getModules(), null);
142 final QName qName = QName.create(first.getQNameModule(), first.getName());
143 final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
144 final NetconfStateSchemas.RemoteYangSchema source2 =
145 new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
146 return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
149 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
150 .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
152 final NetconfDevice device = new NetconfDeviceBuilder()
153 .setReconnectOnSchemasChange(true)
154 .setSchemaResourcesDTO(schemaResourcesDTO)
155 .setGlobalProcessingExecutor(getExecutor())
157 .setSalFacade(facade)
158 .setBaseSchemas(BASE_SCHEMAS)
160 // Monitoring supported
161 final NetconfSessionPreferences sessionCaps =
162 getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
163 device.onRemoteSessionUp(sessionCaps, listener);
165 verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class),
166 any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class), isNull());
167 verify(schemaFactory, times(2)).createEffectiveModelContext(anyCollection());
171 public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
172 final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
174 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
175 final NetconfDeviceCommunicator listener = getListener();
177 final EffectiveModelContextFactory schemaFactory = getSchemaFactory();
178 final SchemaRepository schemaRepository = getSchemaRepository();
180 // Make fallback attempt to fail due to empty resolved sources
181 final SchemaResolutionException schemaResolutionException
182 = new SchemaResolutionException("fail first",
183 Collections.emptyList(), HashMultimap.create());
184 doReturn(Futures.immediateFailedFuture(schemaResolutionException))
185 .when(schemaFactory).createEffectiveModelContext(anyCollection());
187 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
188 .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, STATE_SCHEMAS_RESOLVER);
189 final NetconfDevice device = new NetconfDeviceBuilder()
190 .setReconnectOnSchemasChange(true)
191 .setSchemaResourcesDTO(schemaResourcesDTO)
192 .setGlobalProcessingExecutor(getExecutor())
194 .setSalFacade(facade)
195 .setBaseSchemas(BASE_SCHEMAS)
198 // Monitoring not supported
199 final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
200 device.onRemoteSessionUp(sessionCaps, listener);
202 verify(facade, timeout(5000)).onDeviceDisconnected();
203 verify(listener, timeout(5000)).close();
204 verify(schemaFactory, times(1)).createEffectiveModelContext(anyCollection());
208 public void testNetconfDeviceMissingSource() throws Exception {
209 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
210 final NetconfDeviceCommunicator listener = getListener();
212 final EffectiveModelContextFactory schemaFactory = getSchemaFactory();
213 final SchemaRepository schemaRepository = getSchemaRepository();
215 // Make fallback attempt to fail due to empty resolved sources
216 final MissingSchemaSourceException schemaResolutionException =
217 new MissingSchemaSourceException("fail first", TEST_SID);
218 doReturn(Futures.immediateFailedFuture(schemaResolutionException))
219 .when(schemaRepository).getSchemaSource(eq(TEST_SID), eq(YangTextSchemaSource.class));
220 doAnswer(invocation -> {
221 if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
222 return Futures.immediateFailedFuture(schemaResolutionException);
224 return Futures.immediateFuture(SCHEMA_CONTEXT);
226 }).when(schemaFactory).createEffectiveModelContext(anyCollection());
228 final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id,
230 final Module first = Iterables.getFirst(SCHEMA_CONTEXT.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));
238 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
239 .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
241 final NetconfDevice device = new NetconfDeviceBuilder()
242 .setReconnectOnSchemasChange(true)
243 .setSchemaResourcesDTO(schemaResourcesDTO)
244 .setGlobalProcessingExecutor(getExecutor())
245 .setBaseSchemas(BASE_SCHEMAS)
247 .setSalFacade(facade)
249 // Monitoring supported
250 final NetconfSessionPreferences sessionCaps =
251 getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
252 device.onRemoteSessionUp(sessionCaps, listener);
254 verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class),
255 any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class), isNull());
256 verify(schemaFactory, times(1)).createEffectiveModelContext(anyCollection());
259 private static SchemaSourceRegistry getSchemaRegistry() {
260 final SchemaSourceRegistry mock = mock(SchemaSourceRegistry.class);
261 final SchemaSourceRegistration<?> mockReg = mock(SchemaSourceRegistration.class);
262 doNothing().when(mockReg).close();
263 doReturn(mockReg).when(mock).registerSchemaSource(
264 any(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider.class),
265 any(PotentialSchemaSource.class));
269 private static SchemaRepository getSchemaRepository() {
270 final SchemaRepository mock = mock(SchemaRepository.class);
271 final SchemaSourceRepresentation mockRep = mock(SchemaSourceRepresentation.class);
272 doReturn(Futures.immediateFuture(mockRep))
273 .when(mock).getSchemaSource(any(SourceIdentifier.class), eq(YangTextSchemaSource.class));
278 public void testNotificationBeforeSchema() throws Exception {
279 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
280 final NetconfDeviceCommunicator listener = getListener();
281 final EffectiveModelContextFactory schemaContextProviderFactory = mock(EffectiveModelContextFactory.class);
282 final SettableFuture<SchemaContext> schemaFuture = SettableFuture.create();
283 doReturn(schemaFuture).when(schemaContextProviderFactory).createEffectiveModelContext(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())
292 .setSalFacade(facade)
293 .setBaseSchemas(BASE_SCHEMAS)
296 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
297 Lists.newArrayList(TEST_CAPABILITY));
298 device.onRemoteSessionUp(sessionCaps, listener);
300 device.onNotification(NOTIFICATION);
301 device.onNotification(NOTIFICATION);
302 verify(facade, times(0)).onNotification(any(DOMNotification.class));
304 verify(facade, times(0)).onNotification(any(DOMNotification.class));
305 schemaFuture.set(NetconfToNotificationTest.getNotificationSchemaContext(getClass(), false));
306 verify(facade, timeout(10000).times(2)).onNotification(any(DOMNotification.class));
308 device.onNotification(NOTIFICATION);
309 verify(facade, timeout(10000).times(3)).onNotification(any(DOMNotification.class));
313 public void testNetconfDeviceReconnect() throws Exception {
314 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
315 final NetconfDeviceCommunicator listener = getListener();
317 final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
319 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
320 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
321 final NetconfDevice device = new NetconfDeviceBuilder()
322 .setReconnectOnSchemasChange(true)
323 .setSchemaResourcesDTO(schemaResourcesDTO)
324 .setGlobalProcessingExecutor(getExecutor())
326 .setSalFacade(facade)
327 .setBaseSchemas(BASE_SCHEMAS)
329 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
330 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION));
331 device.onRemoteSessionUp(sessionCaps, listener);
333 verify(schemaContextProviderFactory, timeout(5000)).createEffectiveModelContext(any(Collection.class));
334 verify(facade, timeout(5000)).onDeviceConnected(
335 any(MountPointContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class),
338 device.onRemoteSessionDown();
339 verify(facade, timeout(5000)).onDeviceDisconnected();
341 device.onRemoteSessionUp(sessionCaps, listener);
343 verify(schemaContextProviderFactory, timeout(5000).times(2)).createEffectiveModelContext(any(Collection.class));
344 verify(facade, timeout(5000).times(2)).onDeviceConnected(
345 any(MountPointContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class),
350 public void testNetconfDeviceDisconnectListenerCallCancellation() throws Exception {
351 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
352 final NetconfDeviceCommunicator listener = getListener();
353 final EffectiveModelContextFactory schemaContextProviderFactory = mock(EffectiveModelContextFactory.class);
354 final SettableFuture<SchemaContext> schemaFuture = SettableFuture.create();
355 doReturn(schemaFuture).when(schemaContextProviderFactory).createEffectiveModelContext(any(Collection.class));
356 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
357 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(),
358 schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
359 final NetconfDevice device = new NetconfDeviceBuilder()
360 .setReconnectOnSchemasChange(true)
361 .setSchemaResourcesDTO(schemaResourcesDTO)
362 .setGlobalProcessingExecutor(getExecutor())
364 .setSalFacade(facade)
365 .setBaseSchemas(BASE_SCHEMAS)
367 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
368 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION));
369 //session up, start schema resolution
370 device.onRemoteSessionUp(sessionCaps, listener);
372 device.onRemoteSessionDown();
373 verify(facade, timeout(5000)).onDeviceDisconnected();
374 //complete schema setup
375 schemaFuture.set(SCHEMA_CONTEXT);
376 //facade.onDeviceDisconnected() was called, so facade.onDeviceConnected() shouldn't be called anymore
377 verify(facade, after(1000).never()).onDeviceConnected(any(), any(), any(), any(DOMActionService.class));
381 public void testNetconfDeviceAvailableCapabilitiesBuilding() throws Exception {
382 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
383 final NetconfDeviceCommunicator listener = getListener();
385 final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
387 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
388 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
389 final NetconfDevice device = new NetconfDeviceBuilder()
390 .setReconnectOnSchemasChange(true)
391 .setSchemaResourcesDTO(schemaResourcesDTO)
392 .setGlobalProcessingExecutor(getExecutor())
394 .setSalFacade(facade)
395 .setBaseSchemas(BASE_SCHEMAS)
397 final NetconfDevice netconfSpy = spy(device);
399 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
400 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION));
401 final Map<QName, AvailableCapability.CapabilityOrigin> moduleBasedCaps = new HashMap<>();
402 moduleBasedCaps.putAll(sessionCaps.getModuleBasedCapsOrigin());
404 .put(QName.create("(test:qname:side:loading)test"), AvailableCapability.CapabilityOrigin.UserDefined);
406 netconfSpy.onRemoteSessionUp(sessionCaps.replaceModuleCaps(moduleBasedCaps), listener);
408 final ArgumentCaptor<NetconfSessionPreferences> argument =
409 ArgumentCaptor.forClass(NetconfSessionPreferences.class);
410 verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class), argument.capture(),
411 any(DOMRpcService.class), isNull());
412 final NetconfDeviceCapabilities netconfDeviceCaps = argument.getValue().getNetconfDeviceCapabilities();
414 netconfDeviceCaps.getResolvedCapabilities()
415 .forEach(entry -> assertEquals("Builded 'AvailableCapability' schemas should match input capabilities.",
417 QName.create(entry.getCapability())).getName(), entry.getCapabilityOrigin().getName()));
421 public void testNetconfDeviceNotificationsModelNotPresentWithCapability() throws Exception {
422 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
423 final NetconfDeviceCommunicator listener = getListener();
424 final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
426 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
427 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
428 final NetconfDevice device = new NetconfDeviceBuilder()
429 .setSchemaResourcesDTO(schemaResourcesDTO)
430 .setGlobalProcessingExecutor(getExecutor())
432 .setSalFacade(facade)
433 .setBaseSchemas(BASE_SCHEMAS)
435 final NetconfDevice netconfSpy = spy(device);
437 final NetconfSessionPreferences sessionCaps = getSessionCaps(false,
438 Lists.newArrayList(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_NOTIFICATION_1_0));
440 netconfSpy.onRemoteSessionUp(sessionCaps, listener);
442 final ArgumentCaptor<NetconfSessionPreferences> argument =
443 ArgumentCaptor.forClass(NetconfSessionPreferences.class);
444 verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class), argument.capture(),
445 any(DOMRpcService.class), isNull());
447 List<String> notificationModulesName = Arrays.asList(
448 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714
449 .$YangModuleInfoImpl.getInstance().getName().toString(),
450 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715
451 .$YangModuleInfoImpl.getInstance().getName().toString());
453 final Set<AvailableCapability> resolvedCapabilities = argument.getValue().getNetconfDeviceCapabilities()
454 .getResolvedCapabilities();
456 assertEquals(2, resolvedCapabilities.size());
457 assertTrue(resolvedCapabilities.stream().anyMatch(entry -> notificationModulesName
458 .contains(entry.getCapability())));
462 public void testNetconfDeviceNotificationsCapabilityIsNotPresent() throws Exception {
463 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
464 final NetconfDeviceCommunicator listener = getListener();
465 final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
467 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
468 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
469 final NetconfDevice device = new NetconfDeviceBuilder()
470 .setSchemaResourcesDTO(schemaResourcesDTO)
471 .setGlobalProcessingExecutor(getExecutor())
473 .setSalFacade(facade)
474 .setBaseSchemas(BASE_SCHEMAS)
476 final NetconfDevice netconfSpy = spy(device);
478 final NetconfSessionPreferences sessionCaps = getSessionCaps(false,
479 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION));
481 netconfSpy.onRemoteSessionUp(sessionCaps, listener);
483 final ArgumentCaptor<NetconfSessionPreferences> argument =
484 ArgumentCaptor.forClass(NetconfSessionPreferences.class);
485 verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class), argument.capture(),
486 any(DOMRpcService.class), isNull());
487 final NetconfDeviceCapabilities netconfDeviceCaps = argument.getValue().getNetconfDeviceCapabilities();
489 List<String> notificationModulesName = Arrays.asList(
490 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714
491 .$YangModuleInfoImpl.getInstance().getName().toString(),
492 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715
493 .$YangModuleInfoImpl.getInstance().getName().toString());
495 assertFalse(netconfDeviceCaps.getResolvedCapabilities().stream().anyMatch(entry -> notificationModulesName
496 .contains(entry.getCapability())));
500 public void testNetconfDeviceNotificationsModelIsPresent() throws Exception {
501 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
502 final NetconfDeviceCommunicator listener = getListener();
503 final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
505 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
506 getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
507 final NetconfDevice device = new NetconfDeviceBuilder()
508 .setSchemaResourcesDTO(schemaResourcesDTO)
509 .setGlobalProcessingExecutor(getExecutor())
511 .setSalFacade(facade)
512 .setBaseSchemas(BASE_SCHEMAS)
514 final NetconfDevice netconfSpy = spy(device);
516 final NetconfSessionPreferences sessionCaps = getSessionCaps(false, Collections.emptyList());
518 final Map<QName, AvailableCapability.CapabilityOrigin> moduleBasedCaps = new HashMap<>();
519 moduleBasedCaps.put(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714
520 .$YangModuleInfoImpl.getInstance().getName(),
521 AvailableCapability.CapabilityOrigin.DeviceAdvertised);
522 moduleBasedCaps.put(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715
523 .$YangModuleInfoImpl.getInstance().getName(),
524 AvailableCapability.CapabilityOrigin.DeviceAdvertised);
527 netconfSpy.onRemoteSessionUp(sessionCaps.replaceModuleCaps(moduleBasedCaps), listener);
529 final ArgumentCaptor<NetconfSessionPreferences> argument =
530 ArgumentCaptor.forClass(NetconfSessionPreferences.class);
531 verify(facade, timeout(5000)).onDeviceConnected(any(MountPointContext.class), argument.capture(),
532 any(DOMRpcService.class), isNull());
533 final Set<AvailableCapability> resolvedCapabilities = argument.getValue().getNetconfDeviceCapabilities()
534 .getResolvedCapabilities();
536 List<String> notificationModulesName = Arrays.asList(
537 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714
538 .$YangModuleInfoImpl.getInstance().getName().toString(),
539 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715
540 .$YangModuleInfoImpl.getInstance().getName().toString());
542 assertEquals(2, resolvedCapabilities.size());
543 assertTrue(resolvedCapabilities.stream().anyMatch(entry -> notificationModulesName
544 .contains(entry.getCapability())));
547 private static EffectiveModelContextFactory getSchemaFactory() throws Exception {
548 final EffectiveModelContextFactory schemaFactory = mockClass(EffectiveModelContextFactory.class);
549 doReturn(Futures.immediateFuture(SCHEMA_CONTEXT))
550 .when(schemaFactory).createEffectiveModelContext(any(Collection.class));
551 return schemaFactory;
554 private static RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
555 final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler =
556 mockCloseableClass(RemoteDeviceHandler.class);
557 doNothing().when(remoteDeviceHandler).onDeviceConnected(
558 any(MountPointContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class),
559 any(DOMActionService.class));
560 doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
561 doNothing().when(remoteDeviceHandler).onNotification(any(DOMNotification.class));
562 return remoteDeviceHandler;
565 private static <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass)
567 final T mock = mockClass(remoteDeviceHandlerClass);
568 doNothing().when(mock).close();
572 private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
573 final T mock = mock(remoteDeviceHandlerClass);
574 doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
578 public RemoteDeviceId getId() {
579 return new RemoteDeviceId("test-D", InetSocketAddress.createUnresolved("localhost", 22));
582 public ListeningExecutorService getExecutor() {
583 return MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
586 public MessageTransformer<NetconfMessage> getMessageTransformer() throws Exception {
587 final MessageTransformer<NetconfMessage> messageTransformer = mockClass(MessageTransformer.class);
588 doReturn(NOTIFICATION).when(messageTransformer).toRpcRequest(any(SchemaPath.class), any(NormalizedNode.class));
589 doReturn(RPC_RESULT).when(messageTransformer).toRpcResult(any(NetconfMessage.class), any(SchemaPath.class));
590 doReturn(COMPOSITE_NODE).when(messageTransformer).toNotification(any(NetconfMessage.class));
591 return messageTransformer;
594 public NetconfSessionPreferences getSessionCaps(final boolean addMonitor,
595 final Collection<String> additionalCapabilities) {
596 final ArrayList<String> capabilities = Lists.newArrayList(
597 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
598 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
601 capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
604 capabilities.addAll(additionalCapabilities);
606 return NetconfSessionPreferences.fromStrings(
610 public NetconfDeviceCommunicator getListener() throws Exception {
611 final NetconfDeviceCommunicator remoteDeviceCommunicator = mockCloseableClass(NetconfDeviceCommunicator.class);
612 // doReturn(Futures.immediateFuture(rpcResult))
613 // .when(remoteDeviceCommunicator).sendRequest(any(NetconfMessage.class), any(QName.class));
614 return remoteDeviceCommunicator;