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
9 package org.opendaylight.netconf.sal.connect.netconf;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyCollectionOf;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.timeout;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
22 import com.google.common.base.Optional;
23 import com.google.common.collect.HashMultimap;
24 import com.google.common.collect.Iterables;
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Sets;
27 import com.google.common.util.concurrent.Futures;
28 import java.io.InputStream;
29 import java.net.InetSocketAddress;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.concurrent.ExecutorService;
35 import java.util.concurrent.Executors;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.mockito.invocation.InvocationOnMock;
39 import org.mockito.stubbing.Answer;
40 import org.opendaylight.controller.config.util.xml.XmlUtil;
41 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
42 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
43 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
44 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
45 import org.opendaylight.netconf.api.NetconfMessage;
46 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
47 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
48 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemas;
49 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemasResolver;
50 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
51 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
52 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
53 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
54 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
55 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
56 import org.opendaylight.yangtools.yang.common.QName;
57 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
59 import org.opendaylight.yangtools.yang.model.api.Module;
60 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
61 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
62 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
63 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
64 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
65 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
66 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
67 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
68 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
69 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
70 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
71 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
72 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
73 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
74 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
75 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
76 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
78 public class NetconfDeviceTest {
80 private static final NetconfMessage notification;
82 private static final ContainerNode compositeNode;
86 compositeNode = mockClass(ContainerNode.class);
87 } catch (final Exception e) {
88 throw new RuntimeException(e);
91 notification = new NetconfMessage(XmlUtil.readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
92 } catch (Exception e) {
93 throw new ExceptionInInitializerError(e);
97 private static final DOMRpcResult rpcResultC = new DefaultDOMRpcResult(compositeNode);
99 public static final String TEST_NAMESPACE = "test:namespace";
100 public static final String TEST_MODULE = "test-module";
101 public static final String TEST_REVISION = "2013-07-22";
102 public static final SourceIdentifier TEST_SID =
103 RevisionSourceIdentifier.create(TEST_MODULE, Optional.of(TEST_REVISION));
104 public static final String TEST_CAPABILITY = TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION;
106 public static final SourceIdentifier TEST_SID2 =
107 RevisionSourceIdentifier.create(TEST_MODULE + "2", Optional.of(TEST_REVISION));
108 public static final String TEST_CAPABILITY2 = TEST_NAMESPACE + "?module=" + TEST_MODULE + "2" + "&revision=" + TEST_REVISION;
110 private static final NetconfDeviceSchemasResolver stateSchemasResolver = new NetconfDeviceSchemasResolver() {
113 public NetconfDeviceSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
114 return NetconfStateSchemas.EMPTY;
119 public void testNetconfDeviceFlawedModelFailedResolution() throws Exception {
120 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
121 final NetconfDeviceCommunicator listener = getListener();
123 final SchemaContextFactory schemaFactory = getSchemaFactory();
124 final SchemaContext schema = getSchema();
125 final SchemaRepository schemaRepository = getSchemaRepository();
127 final SchemaResolutionException schemaResolutionException =
128 new SchemaResolutionException("fail first", TEST_SID, new Throwable("YangTools parser fail"));
129 doAnswer(invocation -> {
130 if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
131 return Futures.immediateFailedCheckedFuture(schemaResolutionException);
133 return Futures.immediateCheckedFuture(schema);
135 }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
137 final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id) -> {
138 final Module first = Iterables.getFirst(schema.getModules(), null);
139 final QName qName = QName.create(first.getQNameModule(), first.getName());
140 final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
141 final NetconfStateSchemas.RemoteYangSchema source2 = new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
142 return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
145 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
146 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
148 final NetconfDevice device = new NetconfDeviceBuilder()
149 .setReconnectOnSchemasChange(true)
150 .setSchemaResourcesDTO(schemaResourcesDTO)
151 .setGlobalProcessingExecutor(getExecutor())
153 .setSalFacade(facade)
155 // Monitoring supported
156 final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
157 device.onRemoteSessionUp(sessionCaps, listener);
159 Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
160 Mockito.verify(schemaFactory, times(2)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
164 public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
165 final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
167 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
168 final NetconfDeviceCommunicator listener = getListener();
170 final SchemaContextFactory schemaFactory = getSchemaFactory();
171 final SchemaRepository schemaRepository = getSchemaRepository();
173 // Make fallback attempt to fail due to empty resolved sources
174 final SchemaResolutionException schemaResolutionException
175 = new SchemaResolutionException("fail first",
176 Collections.<SourceIdentifier>emptyList(), HashMultimap.<SourceIdentifier, ModuleImport>create());
177 doReturn(Futures.immediateFailedCheckedFuture(
178 schemaResolutionException))
179 .when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
181 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
182 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
183 final NetconfDevice device = new NetconfDeviceBuilder()
184 .setReconnectOnSchemasChange(true)
185 .setSchemaResourcesDTO(schemaResourcesDTO)
186 .setGlobalProcessingExecutor(getExecutor())
188 .setSalFacade(facade)
191 // Monitoring not supported
192 final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
193 device.onRemoteSessionUp(sessionCaps, listener);
195 Mockito.verify(facade, Mockito.timeout(5000)).onDeviceDisconnected();
196 Mockito.verify(listener, Mockito.timeout(5000)).close();
197 Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
201 public void testNetconfDeviceMissingSource() throws Exception {
202 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
203 final NetconfDeviceCommunicator listener = getListener();
204 final SchemaContext schema = getSchema();
206 final SchemaContextFactory schemaFactory = getSchemaFactory();
207 final SchemaRepository schemaRepository = getSchemaRepository();
209 // Make fallback attempt to fail due to empty resolved sources
210 final MissingSchemaSourceException schemaResolutionException = new MissingSchemaSourceException("fail first", TEST_SID);
211 doReturn(Futures.immediateFailedCheckedFuture(schemaResolutionException)).when(schemaRepository).getSchemaSource(eq(TEST_SID), eq(ASTSchemaSource.class));
212 doAnswer(new Answer<Object>() {
214 public Object answer(final InvocationOnMock invocation) throws Throwable {
215 if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
216 return Futures.immediateFailedCheckedFuture(schemaResolutionException);
218 return Futures.immediateCheckedFuture(schema);
221 }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
223 final NetconfDeviceSchemasResolver stateSchemasResolver = new NetconfDeviceSchemasResolver() {
225 public NetconfDeviceSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId 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 = new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
230 return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
234 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
235 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
237 final NetconfDevice device = new NetconfDeviceBuilder()
238 .setReconnectOnSchemasChange(true)
239 .setSchemaResourcesDTO(schemaResourcesDTO)
240 .setGlobalProcessingExecutor(getExecutor())
242 .setSalFacade(facade)
244 // Monitoring supported
245 final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
246 device.onRemoteSessionUp(sessionCaps, listener);
248 Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
249 Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
252 private SchemaSourceRegistry getSchemaRegistry() {
253 final SchemaSourceRegistry mock = mock(SchemaSourceRegistry.class);
254 final SchemaSourceRegistration<?> mockReg = mock(SchemaSourceRegistration.class);
255 doNothing().when(mockReg).close();
256 doReturn(mockReg).when(mock).registerSchemaSource(any(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider.class), any(PotentialSchemaSource.class));
260 private SchemaRepository getSchemaRepository() {
261 final SchemaRepository mock = mock(SchemaRepository.class);
262 final SchemaSourceRepresentation mockRep = mock(SchemaSourceRepresentation.class);
263 doReturn(Futures.immediateCheckedFuture(mockRep)).when(mock).getSchemaSource(any(SourceIdentifier.class), eq(ASTSchemaSource.class));
268 public void testNotificationBeforeSchema() throws Exception {
269 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
270 final NetconfDeviceCommunicator listener = getListener();
272 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
273 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(), getSchemaFactory(), stateSchemasResolver);
274 final NetconfDevice device = new NetconfDeviceBuilder()
275 .setReconnectOnSchemasChange(true)
276 .setSchemaResourcesDTO(schemaResourcesDTO)
277 .setGlobalProcessingExecutor(getExecutor())
279 .setSalFacade(facade)
282 device.onNotification(notification);
283 device.onNotification(notification);
285 verify(facade, times(0)).onNotification(any(DOMNotification.class));
287 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
288 Lists.newArrayList(TEST_CAPABILITY));
290 final DOMRpcService deviceRpc = mock(DOMRpcService.class);
292 device.handleSalInitializationSuccess(NetconfToNotificationTest.getNotificationSchemaContext(getClass(), false), sessionCaps, deviceRpc);
294 verify(facade, timeout(10000).times(2)).onNotification(any(DOMNotification.class));
296 device.onNotification(notification);
297 verify(facade, timeout(10000).times(3)).onNotification(any(DOMNotification.class));
301 public void testNetconfDeviceReconnect() throws Exception {
302 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
303 final NetconfDeviceCommunicator listener = getListener();
305 final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
307 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
308 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, stateSchemasResolver);
309 final NetconfDevice device = new NetconfDeviceBuilder()
310 .setReconnectOnSchemasChange(true)
311 .setSchemaResourcesDTO(schemaResourcesDTO)
312 .setGlobalProcessingExecutor(getExecutor())
314 .setSalFacade(facade)
316 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
317 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION));
318 device.onRemoteSessionUp(sessionCaps, listener);
320 verify(schemaContextProviderFactory, timeout(5000)).createSchemaContext(any(Collection.class));
321 verify(facade, timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
323 device.onRemoteSessionDown();
324 verify(facade, timeout(5000)).onDeviceDisconnected();
326 device.onRemoteSessionUp(sessionCaps, listener);
328 verify(schemaContextProviderFactory, timeout(5000).times(2)).createSchemaContext(any(Collection.class));
329 verify(facade, timeout(5000).times(2)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
332 private SchemaContextFactory getSchemaFactory() {
333 final SchemaContextFactory schemaFactory = mockClass(SchemaContextFactory.class);
334 doReturn(Futures.immediateCheckedFuture(getSchema())).when(schemaFactory).createSchemaContext(any(Collection.class));
335 return schemaFactory;
338 private static SchemaContext parseYangStreams(final List<InputStream> streams) {
339 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
341 final SchemaContext schemaContext;
343 schemaContext = reactor.buildEffective(streams);
344 } catch (ReactorException e) {
345 throw new RuntimeException("Unable to build schema context from " + streams, e);
347 return schemaContext;
350 public static SchemaContext getSchema() {
351 final List<InputStream> modelsToParse = Lists.newArrayList(
352 NetconfDeviceTest.class.getResourceAsStream("/schemas/test-module.yang")
354 return parseYangStreams(modelsToParse);
357 private RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
358 final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler = mockCloseableClass(RemoteDeviceHandler.class);
359 doNothing().when(remoteDeviceHandler).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
360 doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
361 doNothing().when(remoteDeviceHandler).onNotification(any(DOMNotification.class));
362 return remoteDeviceHandler;
365 private <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass) throws Exception {
366 final T mock = mockClass(remoteDeviceHandlerClass);
367 doNothing().when(mock).close();
371 private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
372 final T mock = mock(remoteDeviceHandlerClass);
373 Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
377 public RemoteDeviceId getId() {
378 return new RemoteDeviceId("test-D", InetSocketAddress.createUnresolved("localhost", 22));
381 public ExecutorService getExecutor() {
382 return Executors.newSingleThreadExecutor();
385 public MessageTransformer<NetconfMessage> getMessageTransformer() throws Exception {
386 final MessageTransformer<NetconfMessage> messageTransformer = mockClass(MessageTransformer.class);
387 doReturn(notification).when(messageTransformer).toRpcRequest(any(SchemaPath.class), any(NormalizedNode.class));
388 doReturn(rpcResultC).when(messageTransformer).toRpcResult(any(NetconfMessage.class), any(SchemaPath.class));
389 doReturn(compositeNode).when(messageTransformer).toNotification(any(NetconfMessage.class));
390 return messageTransformer;
393 public NetconfSessionPreferences getSessionCaps(final boolean addMonitor, final Collection<String> additionalCapabilities) {
394 final ArrayList<String> capabilities = Lists.newArrayList(
395 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
396 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
399 capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
402 capabilities.addAll(additionalCapabilities);
404 return NetconfSessionPreferences.fromStrings(
408 public NetconfDeviceCommunicator getListener() throws Exception {
409 final NetconfDeviceCommunicator remoteDeviceCommunicator = mockCloseableClass(NetconfDeviceCommunicator.class);
410 // doReturn(Futures.immediateFuture(rpcResult)).when(remoteDeviceCommunicator).sendRequest(any(NetconfMessage.class), any(QName.class));
411 return remoteDeviceCommunicator;