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.Mockito.doAnswer;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.timeout;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
20 import com.google.common.base.Optional;
21 import com.google.common.collect.HashMultimap;
22 import com.google.common.collect.Lists;
23 import com.google.common.util.concurrent.Futures;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import org.mockito.invocation.InvocationOnMock;
35 import org.mockito.stubbing.Answer;
36 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
37 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
38 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
39 import org.opendaylight.controller.netconf.api.NetconfMessage;
40 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
41 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
42 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
43 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
44 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
45 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
46 import org.opendaylight.controller.sal.connect.netconf.sal.NetconfDeviceRpc;
47 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
48 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
51 import org.opendaylight.yangtools.yang.model.api.Module;
52 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
53 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
54 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
55 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
56 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
57 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
58 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
59 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
60 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
61 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
62 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
64 public class NetconfDeviceTest {
66 private static final NetconfMessage notification;
68 private static final ContainerNode compositeNode;
72 compositeNode = mockClass(ContainerNode.class);
73 } catch (final Exception e) {
74 throw new RuntimeException(e);
77 notification = new NetconfMessage(XmlUtil.readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
78 } catch (Exception e) {
79 throw new ExceptionInInitializerError(e);
83 private static final DOMRpcResult rpcResultC = new DefaultDOMRpcResult(compositeNode);
85 public static final String TEST_NAMESPACE = "test:namespace";
86 public static final String TEST_MODULE = "test-module";
87 public static final String TEST_REVISION = "2013-07-22";
88 public static final SourceIdentifier TEST_SID = new SourceIdentifier(TEST_MODULE, Optional.of(TEST_REVISION));
89 public static final String TEST_CAPABILITY = TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION;
91 public static final SourceIdentifier TEST_SID2 = new SourceIdentifier(TEST_MODULE + "2", Optional.of(TEST_REVISION));
92 public static final String TEST_CAPABILITY2 = TEST_NAMESPACE + "?module=" + TEST_MODULE + "2" + "&revision=" + TEST_REVISION;
94 private static final NetconfStateSchemas.NetconfStateSchemasResolver stateSchemasResolver = new NetconfStateSchemas.NetconfStateSchemasResolver() {
97 public NetconfStateSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
98 return NetconfStateSchemas.EMPTY;
103 public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
104 final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
106 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
107 final NetconfDeviceCommunicator listener = getListener();
109 final SchemaContextFactory schemaFactory = getSchemaFactory();
111 // Make fallback attempt to fail due to empty resolved sources
112 final SchemaResolutionException schemaResolutionException
113 = new SchemaResolutionException("fail first",
114 Collections.<SourceIdentifier>emptyList(), HashMultimap.<SourceIdentifier, ModuleImport>create());
115 doReturn(Futures.immediateFailedCheckedFuture(
116 schemaResolutionException))
117 .when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
119 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
120 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, stateSchemasResolver);
121 final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(),true);
122 // Monitoring not supported
123 final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
124 device.onRemoteSessionUp(sessionCaps, listener);
126 Mockito.verify(facade, Mockito.timeout(5000)).onDeviceDisconnected();
127 Mockito.verify(listener, Mockito.timeout(5000)).close();
128 Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
132 public void testNetconfDeviceMissingSource() throws Exception {
133 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
134 final NetconfDeviceCommunicator listener = getListener();
136 final SchemaContextFactory schemaFactory = getSchemaFactory();
138 // Make fallback attempt to fail due to empty resolved sources
139 final MissingSchemaSourceException schemaResolutionException = new MissingSchemaSourceException("fail first", TEST_SID);
140 doAnswer(new Answer<Object>() {
142 public Object answer(final InvocationOnMock invocation) throws Throwable {
143 if(((Collection<?>) invocation.getArguments()[0]).size() == 2) {
144 return Futures.immediateFailedCheckedFuture(schemaResolutionException);
146 return Futures.immediateCheckedFuture(getSchema());
149 }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
151 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
152 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, stateSchemasResolver);
153 final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(), true);
154 // Monitoring supported
155 final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
156 device.onRemoteSessionUp(sessionCaps, listener);
158 Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
159 Mockito.verify(schemaFactory, times(2)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
162 private SchemaSourceRegistry getSchemaRegistry() {
163 final SchemaSourceRegistry mock = mock(SchemaSourceRegistry.class);
164 final SchemaSourceRegistration<?> mockReg = mock(SchemaSourceRegistration.class);
165 doNothing().when(mockReg).close();
166 doReturn(mockReg).when(mock).registerSchemaSource(any(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider.class), any(PotentialSchemaSource.class));
171 public void testNotificationBeforeSchema() throws Exception {
172 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
173 final NetconfDeviceCommunicator listener = getListener();
175 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
176 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaFactory(), stateSchemasResolver);
177 final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(), true);
179 device.onNotification(notification);
180 device.onNotification(notification);
182 verify(facade, times(0)).onNotification(any(ContainerNode.class));
184 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
185 Lists.newArrayList(TEST_CAPABILITY));
187 final DOMRpcService deviceRpc = mock(DOMRpcService.class);
189 device.handleSalInitializationSuccess(NetconfToNotificationTest.getNotificationSchemaContext(getClass()), sessionCaps, deviceRpc);
191 verify(facade, timeout(10000).times(2)).onNotification(any(ContainerNode.class));
193 device.onNotification(notification);
194 verify(facade, timeout(10000).times(3)).onNotification(any(ContainerNode.class));
198 public void testNetconfDeviceReconnect() throws Exception {
199 final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
200 final NetconfDeviceCommunicator listener = getListener();
202 final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
204 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
205 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaContextProviderFactory, stateSchemasResolver);
206 final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(), true);
207 final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
208 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION));
209 device.onRemoteSessionUp(sessionCaps, listener);
211 verify(schemaContextProviderFactory, timeout(5000)).createSchemaContext(any(Collection.class));
212 verify(facade, timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
214 device.onRemoteSessionDown();
215 verify(facade, timeout(5000)).onDeviceDisconnected();
217 device.onRemoteSessionUp(sessionCaps, listener);
219 verify(schemaContextProviderFactory, timeout(5000).times(2)).createSchemaContext(any(Collection.class));
220 verify(facade, timeout(5000).times(2)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
223 private SchemaContextFactory getSchemaFactory() {
224 final SchemaContextFactory schemaFactory = mockClass(SchemaContextFactory.class);
225 doReturn(Futures.immediateCheckedFuture(getSchema())).when(schemaFactory).createSchemaContext(any(Collection.class));
226 return schemaFactory;
229 public static SchemaContext getSchema() {
230 final YangParserImpl parser = new YangParserImpl();
231 final List<InputStream> modelsToParse = Lists.newArrayList(
232 NetconfDeviceTest.class.getResourceAsStream("/schemas/test-module.yang")
234 final Set<Module> models = parser.parseYangModelsFromStreams(modelsToParse);
235 return parser.resolveSchemaContext(models);
238 private RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
239 final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler = mockCloseableClass(RemoteDeviceHandler.class);
240 doNothing().when(remoteDeviceHandler).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
241 doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
242 doNothing().when(remoteDeviceHandler).onNotification(any(ContainerNode.class));
243 return remoteDeviceHandler;
246 private <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass) throws Exception {
247 final T mock = mockClass(remoteDeviceHandlerClass);
248 doNothing().when(mock).close();
252 private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
253 final T mock = mock(remoteDeviceHandlerClass);
254 Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
258 public RemoteDeviceId getId() {
259 return new RemoteDeviceId("test-D");
262 public ExecutorService getExecutor() {
263 return Executors.newSingleThreadExecutor();
266 public MessageTransformer<NetconfMessage> getMessageTransformer() throws Exception {
267 final MessageTransformer<NetconfMessage> messageTransformer = mockClass(MessageTransformer.class);
268 doReturn(notification).when(messageTransformer).toRpcRequest(any(SchemaPath.class), any(NormalizedNode.class));
269 doReturn(rpcResultC).when(messageTransformer).toRpcResult(any(NetconfMessage.class), any(SchemaPath.class));
270 doReturn(compositeNode).when(messageTransformer).toNotification(any(NetconfMessage.class));
271 return messageTransformer;
274 public NetconfSessionPreferences getSessionCaps(final boolean addMonitor, final Collection<String> additionalCapabilities) {
275 final ArrayList<String> capabilities = Lists.newArrayList(
276 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
277 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
280 capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
283 capabilities.addAll(additionalCapabilities);
285 return NetconfSessionPreferences.fromStrings(
289 public NetconfDeviceCommunicator getListener() throws Exception {
290 final NetconfDeviceCommunicator remoteDeviceCommunicator = mockCloseableClass(NetconfDeviceCommunicator.class);
291 // doReturn(Futures.immediateFuture(rpcResult)).when(remoteDeviceCommunicator).sendRequest(any(NetconfMessage.class), any(QName.class));
292 return remoteDeviceCommunicator;