2 * Copyright (c) 2014 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.controller.sal.connect.netconf;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyCollectionOf;
12 import static org.mockito.Matchers.anyString;
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.Lists;
24 import com.google.common.util.concurrent.Futures;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.List;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
38 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
39 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
40 import org.opendaylight.controller.netconf.api.NetconfMessage;
41 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
42 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
43 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
44 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
45 import org.opendaylight.controller.sal.connect.api.SchemaSourceProviderFactory;
46 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
47 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
48 import org.opendaylight.controller.sal.connect.netconf.sal.NetconfDeviceRpc;
49 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
50 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
51 import org.opendaylight.controller.sal.core.api.RpcImplementation;
52 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
54 import org.opendaylight.yangtools.yang.model.api.Module;
55 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
56 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
57 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
58 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
59 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
60 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
61 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
62 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
63 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
64 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
65 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
66 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
68 public class NetconfDeviceTest {
70 private static final NetconfMessage notification;
72 private static final ContainerNode compositeNode;
76 compositeNode = mockClass(ContainerNode.class);
77 } catch (final Exception e) {
78 throw new RuntimeException(e);
81 notification = new NetconfMessage(XmlUtil.readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
82 } catch (Exception e) {
83 throw new ExceptionInInitializerError(e);
87 private static final DOMRpcResult rpcResultC = new DefaultDOMRpcResult(compositeNode);
89 public static final String TEST_NAMESPACE = "test:namespace";
90 public static final String TEST_MODULE = "test-module";
91 public static final String TEST_REVISION = "2013-07-22";
92 public static final SourceIdentifier TEST_SID = new SourceIdentifier(TEST_MODULE, Optional.of(TEST_REVISION));
93 public static final String TEST_CAPABILITY = TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION;
95 public static final SourceIdentifier TEST_SID2 = new SourceIdentifier(TEST_MODULE + "2", Optional.of(TEST_REVISION));
96 public static final String TEST_CAPABILITY2 = TEST_NAMESPACE + "?module=" + TEST_MODULE + "2" + "&revision=" + TEST_REVISION;
98 private static final NetconfStateSchemas.NetconfStateSchemasResolver stateSchemasResolver = new NetconfStateSchemas.NetconfStateSchemasResolver() {
101 public NetconfStateSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
102 return NetconfStateSchemas.EMPTY;
107 public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
108 final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
110 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
111 final NetconfDeviceCommunicator listener = getListener();
113 final SchemaContextFactory schemaFactory = getSchemaFactory();
115 // Make fallback attempt to fail due to empty resolved sources
116 final SchemaResolutionException schemaResolutionException
117 = new SchemaResolutionException("fail first",
118 Collections.<SourceIdentifier>emptyList(), HashMultimap.<SourceIdentifier, ModuleImport>create());
119 doReturn(Futures.immediateFailedCheckedFuture(
120 schemaResolutionException))
121 .when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
123 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
124 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, stateSchemasResolver);
125 final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(),true);
126 // Monitoring not supported
127 final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
128 device.onRemoteSessionUp(sessionCaps, listener);
130 Mockito.verify(facade, Mockito.timeout(5000)).onDeviceDisconnected();
131 Mockito.verify(listener, Mockito.timeout(5000)).close();
132 Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
136 public void testNetconfDeviceMissingSource() throws Exception {
137 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
138 final NetconfDeviceCommunicator listener = getListener();
140 final SchemaContextFactory schemaFactory = getSchemaFactory();
142 // Make fallback attempt to fail due to empty resolved sources
143 final MissingSchemaSourceException schemaResolutionException = new MissingSchemaSourceException("fail first", TEST_SID);
144 doAnswer(new Answer<Object>() {
146 public Object answer(final InvocationOnMock invocation) throws Throwable {
147 if(((Collection<?>) invocation.getArguments()[0]).size() == 2) {
148 return Futures.immediateFailedCheckedFuture(schemaResolutionException);
150 return Futures.immediateCheckedFuture(getSchema());
153 }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
155 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
156 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, stateSchemasResolver);
157 final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(), true);
158 // Monitoring supported
159 final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
160 device.onRemoteSessionUp(sessionCaps, listener);
162 Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
163 Mockito.verify(schemaFactory, times(2)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
166 private SchemaSourceRegistry getSchemaRegistry() {
167 final SchemaSourceRegistry mock = mock(SchemaSourceRegistry.class);
168 final SchemaSourceRegistration<?> mockReg = mock(SchemaSourceRegistration.class);
169 doNothing().when(mockReg).close();
170 doReturn(mockReg).when(mock).registerSchemaSource(any(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider.class), any(PotentialSchemaSource.class));
175 public void testNotificationBeforeSchema() throws Exception {
176 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
177 final NetconfDeviceCommunicator listener = getListener();
179 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
180 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaFactory(), stateSchemasResolver);
181 final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(), true);
183 device.onNotification(notification);
184 device.onNotification(notification);
186 verify(facade, times(0)).onNotification(any(ContainerNode.class));
188 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
189 Lists.newArrayList(TEST_CAPABILITY));
191 final DOMRpcService deviceRpc = mock(DOMRpcService.class);
193 device.handleSalInitializationSuccess(NetconfToNotificationTest.getNotificationSchemaContext(getClass()), sessionCaps, deviceRpc);
195 verify(facade, timeout(10000).times(2)).onNotification(any(ContainerNode.class));
197 device.onNotification(notification);
198 verify(facade, timeout(10000).times(3)).onNotification(any(ContainerNode.class));
202 public void testNetconfDeviceReconnect() throws Exception {
203 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
204 final NetconfDeviceCommunicator listener = getListener();
206 final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
208 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
209 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaContextProviderFactory, stateSchemasResolver);
210 final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(), true);
211 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
212 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION));
213 device.onRemoteSessionUp(sessionCaps, listener);
215 verify(schemaContextProviderFactory, timeout(5000)).createSchemaContext(any(Collection.class));
216 verify(facade, timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
218 device.onRemoteSessionDown();
219 verify(facade, timeout(5000)).onDeviceDisconnected();
221 device.onRemoteSessionUp(sessionCaps, listener);
223 verify(schemaContextProviderFactory, timeout(5000).times(2)).createSchemaContext(any(Collection.class));
224 verify(facade, timeout(5000).times(2)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
227 private SchemaContextFactory getSchemaFactory() {
228 final SchemaContextFactory schemaFactory = mockClass(SchemaContextFactory.class);
229 doReturn(Futures.immediateCheckedFuture(getSchema())).when(schemaFactory).createSchemaContext(any(Collection.class));
230 return schemaFactory;
233 public static SchemaContext getSchema() {
234 final YangParserImpl parser = new YangParserImpl();
235 final List<InputStream> modelsToParse = Lists.newArrayList(
236 NetconfDeviceTest.class.getResourceAsStream("/schemas/test-module.yang")
238 final Set<Module> models = parser.parseYangModelsFromStreams(modelsToParse);
239 return parser.resolveSchemaContext(models);
242 private RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
243 final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler = mockCloseableClass(RemoteDeviceHandler.class);
244 doNothing().when(remoteDeviceHandler).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
245 doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
246 doNothing().when(remoteDeviceHandler).onNotification(any(ContainerNode.class));
247 return remoteDeviceHandler;
250 private <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass) throws Exception {
251 final T mock = mockClass(remoteDeviceHandlerClass);
252 doNothing().when(mock).close();
256 public SchemaSourceProviderFactory<InputStream> getSourceProviderFactory() {
257 final SchemaSourceProviderFactory<InputStream> mock = mockClass(SchemaSourceProviderFactory.class);
259 final SchemaSourceProvider<InputStream> schemaSourceProvider = mockClass(SchemaSourceProvider.class);
260 doReturn(Optional.<String>absent()).when(schemaSourceProvider).getSchemaSource(anyString(), any(Optional.class));
262 doReturn(schemaSourceProvider).when(mock).createSourceProvider(any(RpcImplementation.class));
266 private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
267 final T mock = mock(remoteDeviceHandlerClass);
268 Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
272 public RemoteDeviceId getId() {
273 return new RemoteDeviceId("test-D");
276 public ExecutorService getExecutor() {
277 return Executors.newSingleThreadExecutor();
280 public MessageTransformer<NetconfMessage> getMessageTransformer() throws Exception {
281 final MessageTransformer<NetconfMessage> messageTransformer = mockClass(MessageTransformer.class);
282 doReturn(notification).when(messageTransformer).toRpcRequest(any(SchemaPath.class), any(NormalizedNode.class));
283 doReturn(rpcResultC).when(messageTransformer).toRpcResult(any(NetconfMessage.class), any(SchemaPath.class));
284 doReturn(compositeNode).when(messageTransformer).toNotification(any(NetconfMessage.class));
285 return messageTransformer;
288 public NetconfSessionPreferences getSessionCaps(final boolean addMonitor, final Collection<String> additionalCapabilities) {
289 final ArrayList<String> capabilities = Lists.newArrayList(
290 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
291 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
294 capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
297 capabilities.addAll(additionalCapabilities);
299 return NetconfSessionPreferences.fromStrings(
303 public NetconfDeviceCommunicator getListener() throws Exception {
304 final NetconfDeviceCommunicator remoteDeviceCommunicator = mockCloseableClass(NetconfDeviceCommunicator.class);
305 // doReturn(Futures.immediateFuture(rpcResult)).when(remoteDeviceCommunicator).sendRequest(any(NetconfMessage.class), any(QName.class));
306 return remoteDeviceCommunicator;