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.Mockito.doAnswer;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.timeout;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
21 import com.google.common.base.Optional;
22 import com.google.common.collect.HashMultimap;
23 import com.google.common.collect.Iterables;
24 import com.google.common.collect.Lists;
25 import com.google.common.collect.Sets;
26 import com.google.common.util.concurrent.Futures;
27 import java.io.InputStream;
28 import java.net.InetSocketAddress;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.concurrent.ExecutorService;
34 import java.util.concurrent.Executors;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.mockito.invocation.InvocationOnMock;
38 import org.mockito.stubbing.Answer;
39 import org.opendaylight.controller.config.util.xml.XmlUtil;
40 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
41 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
42 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
43 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
44 import org.opendaylight.netconf.api.NetconfMessage;
45 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
46 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
47 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemas;
48 import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemasResolver;
49 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
50 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
51 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
52 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
53 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
54 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
55 import org.opendaylight.yangtools.yang.common.QName;
56 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
58 import org.opendaylight.yangtools.yang.model.api.Module;
59 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
60 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
61 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
62 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
63 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
64 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
65 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
66 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
67 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
68 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
69 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
70 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
71 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
72 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
74 public class NetconfDeviceTest {
76 private static final NetconfMessage notification;
78 private static final ContainerNode compositeNode;
82 compositeNode = mockClass(ContainerNode.class);
83 } catch (final Exception e) {
84 throw new RuntimeException(e);
87 notification = new NetconfMessage(XmlUtil.readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
88 } catch (Exception e) {
89 throw new ExceptionInInitializerError(e);
93 private static final DOMRpcResult rpcResultC = new DefaultDOMRpcResult(compositeNode);
95 public static final String TEST_NAMESPACE = "test:namespace";
96 public static final String TEST_MODULE = "test-module";
97 public static final String TEST_REVISION = "2013-07-22";
98 public static final SourceIdentifier TEST_SID =
99 RevisionSourceIdentifier.create(TEST_MODULE, Optional.of(TEST_REVISION));
100 public static final String TEST_CAPABILITY = TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION;
102 public static final SourceIdentifier TEST_SID2 =
103 RevisionSourceIdentifier.create(TEST_MODULE + "2", Optional.of(TEST_REVISION));
104 public static final String TEST_CAPABILITY2 = TEST_NAMESPACE + "?module=" + TEST_MODULE + "2" + "&revision=" + TEST_REVISION;
106 private static final NetconfDeviceSchemasResolver stateSchemasResolver = new NetconfDeviceSchemasResolver() {
109 public NetconfDeviceSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
110 return NetconfStateSchemas.EMPTY;
115 public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
116 final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
118 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
119 final NetconfDeviceCommunicator listener = getListener();
121 final SchemaContextFactory schemaFactory = getSchemaFactory();
123 // Make fallback attempt to fail due to empty resolved sources
124 final SchemaResolutionException schemaResolutionException
125 = new SchemaResolutionException("fail first",
126 Collections.<SourceIdentifier>emptyList(), HashMultimap.<SourceIdentifier, ModuleImport>create());
127 doReturn(Futures.immediateFailedCheckedFuture(
128 schemaResolutionException))
129 .when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
131 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
132 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, stateSchemasResolver);
133 final NetconfDevice device = new NetconfDeviceBuilder()
134 .setReconnectOnSchemasChange(true)
135 .setSchemaResourcesDTO(schemaResourcesDTO)
136 .setGlobalProcessingExecutor(getExecutor())
138 .setSalFacade(facade)
141 // Monitoring not supported
142 final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
143 device.onRemoteSessionUp(sessionCaps, listener);
145 Mockito.verify(facade, Mockito.timeout(5000)).onDeviceDisconnected();
146 Mockito.verify(listener, Mockito.timeout(5000)).close();
147 Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
151 public void testNetconfDeviceMissingSource() throws Exception {
152 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
153 final NetconfDeviceCommunicator listener = getListener();
154 final SchemaContext schema = getSchema();
156 final SchemaContextFactory schemaFactory = getSchemaFactory();
158 // Make fallback attempt to fail due to empty resolved sources
159 final MissingSchemaSourceException schemaResolutionException = new MissingSchemaSourceException("fail first", TEST_SID);
160 doAnswer(new Answer<Object>() {
162 public Object answer(final InvocationOnMock invocation) throws Throwable {
163 if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
164 return Futures.immediateFailedCheckedFuture(schemaResolutionException);
166 return Futures.immediateCheckedFuture(schema);
169 }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
171 final NetconfDeviceSchemasResolver stateSchemasResolver = new NetconfDeviceSchemasResolver() {
173 public NetconfDeviceSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
174 final Module first = Iterables.getFirst(schema.getModules(), null);
175 final QName qName = QName.create(first.getQNameModule(), first.getName());
176 final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
177 final NetconfStateSchemas.RemoteYangSchema source2 = new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
178 return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
182 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
183 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, stateSchemasResolver);
185 final NetconfDevice device = new NetconfDeviceBuilder()
186 .setReconnectOnSchemasChange(true)
187 .setSchemaResourcesDTO(schemaResourcesDTO)
188 .setGlobalProcessingExecutor(getExecutor())
190 .setSalFacade(facade)
192 // Monitoring supported
193 final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
194 device.onRemoteSessionUp(sessionCaps, listener);
196 Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
197 Mockito.verify(schemaFactory, times(2)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
200 private SchemaSourceRegistry getSchemaRegistry() {
201 final SchemaSourceRegistry mock = mock(SchemaSourceRegistry.class);
202 final SchemaSourceRegistration<?> mockReg = mock(SchemaSourceRegistration.class);
203 doNothing().when(mockReg).close();
204 doReturn(mockReg).when(mock).registerSchemaSource(any(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider.class), any(PotentialSchemaSource.class));
209 public void testNotificationBeforeSchema() throws Exception {
210 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
211 final NetconfDeviceCommunicator listener = getListener();
213 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
214 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaFactory(), stateSchemasResolver);
215 final NetconfDevice device = new NetconfDeviceBuilder()
216 .setReconnectOnSchemasChange(true)
217 .setSchemaResourcesDTO(schemaResourcesDTO)
218 .setGlobalProcessingExecutor(getExecutor())
220 .setSalFacade(facade)
223 device.onNotification(notification);
224 device.onNotification(notification);
226 verify(facade, times(0)).onNotification(any(DOMNotification.class));
228 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
229 Lists.newArrayList(TEST_CAPABILITY));
231 final DOMRpcService deviceRpc = mock(DOMRpcService.class);
233 device.handleSalInitializationSuccess(NetconfToNotificationTest.getNotificationSchemaContext(getClass(), false), sessionCaps, deviceRpc);
235 verify(facade, timeout(10000).times(2)).onNotification(any(DOMNotification.class));
237 device.onNotification(notification);
238 verify(facade, timeout(10000).times(3)).onNotification(any(DOMNotification.class));
242 public void testNetconfDeviceReconnect() throws Exception {
243 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
244 final NetconfDeviceCommunicator listener = getListener();
246 final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
248 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
249 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaContextProviderFactory, stateSchemasResolver);
250 final NetconfDevice device = new NetconfDeviceBuilder()
251 .setReconnectOnSchemasChange(true)
252 .setSchemaResourcesDTO(schemaResourcesDTO)
253 .setGlobalProcessingExecutor(getExecutor())
255 .setSalFacade(facade)
257 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
258 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION));
259 device.onRemoteSessionUp(sessionCaps, listener);
261 verify(schemaContextProviderFactory, timeout(5000)).createSchemaContext(any(Collection.class));
262 verify(facade, timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
264 device.onRemoteSessionDown();
265 verify(facade, timeout(5000)).onDeviceDisconnected();
267 device.onRemoteSessionUp(sessionCaps, listener);
269 verify(schemaContextProviderFactory, timeout(5000).times(2)).createSchemaContext(any(Collection.class));
270 verify(facade, timeout(5000).times(2)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
273 private SchemaContextFactory getSchemaFactory() {
274 final SchemaContextFactory schemaFactory = mockClass(SchemaContextFactory.class);
275 doReturn(Futures.immediateCheckedFuture(getSchema())).when(schemaFactory).createSchemaContext(any(Collection.class));
276 return schemaFactory;
279 private static SchemaContext parseYangStreams(final List<InputStream> streams) {
280 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
282 final SchemaContext schemaContext;
284 schemaContext = reactor.buildEffective(streams);
285 } catch (ReactorException e) {
286 throw new RuntimeException("Unable to build schema context from " + streams, e);
288 return schemaContext;
291 public static SchemaContext getSchema() {
292 final List<InputStream> modelsToParse = Lists.newArrayList(
293 NetconfDeviceTest.class.getResourceAsStream("/schemas/test-module.yang")
295 return parseYangStreams(modelsToParse);
298 private RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
299 final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler = mockCloseableClass(RemoteDeviceHandler.class);
300 doNothing().when(remoteDeviceHandler).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
301 doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
302 doNothing().when(remoteDeviceHandler).onNotification(any(DOMNotification.class));
303 return remoteDeviceHandler;
306 private <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass) throws Exception {
307 final T mock = mockClass(remoteDeviceHandlerClass);
308 doNothing().when(mock).close();
312 private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
313 final T mock = mock(remoteDeviceHandlerClass);
314 Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
318 public RemoteDeviceId getId() {
319 return new RemoteDeviceId("test-D", InetSocketAddress.createUnresolved("localhost", 22));
322 public ExecutorService getExecutor() {
323 return Executors.newSingleThreadExecutor();
326 public MessageTransformer<NetconfMessage> getMessageTransformer() throws Exception {
327 final MessageTransformer<NetconfMessage> messageTransformer = mockClass(MessageTransformer.class);
328 doReturn(notification).when(messageTransformer).toRpcRequest(any(SchemaPath.class), any(NormalizedNode.class));
329 doReturn(rpcResultC).when(messageTransformer).toRpcResult(any(NetconfMessage.class), any(SchemaPath.class));
330 doReturn(compositeNode).when(messageTransformer).toNotification(any(NetconfMessage.class));
331 return messageTransformer;
334 public NetconfSessionPreferences getSessionCaps(final boolean addMonitor, final Collection<String> additionalCapabilities) {
335 final ArrayList<String> capabilities = Lists.newArrayList(
336 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
337 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
340 capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
343 capabilities.addAll(additionalCapabilities);
345 return NetconfSessionPreferences.fromStrings(
349 public NetconfDeviceCommunicator getListener() throws Exception {
350 final NetconfDeviceCommunicator remoteDeviceCommunicator = mockCloseableClass(NetconfDeviceCommunicator.class);
351 // doReturn(Futures.immediateFuture(rpcResult)).when(remoteDeviceCommunicator).sendRequest(any(NetconfMessage.class), any(QName.class));
352 return remoteDeviceCommunicator;