Merge "Bug 2820 - LLDP TLV support and testing"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connect / netconf / NetconfDeviceTest.java
1 /*
2 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3 *
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
7 */
8 package org.opendaylight.controller.sal.connect.netconf;
9
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;
19
20 import com.google.common.base.Optional;
21 import com.google.common.collect.HashMultimap;
22 import com.google.common.collect.Iterables;
23 import com.google.common.collect.Lists;
24 import com.google.common.collect.Sets;
25 import com.google.common.util.concurrent.Futures;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Set;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.mockito.invocation.InvocationOnMock;
37 import org.mockito.stubbing.Answer;
38 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
39 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
40 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
41 import org.opendaylight.controller.netconf.api.NetconfMessage;
42 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
43 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
44 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
45 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
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.yangtools.yang.common.QName;
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.parser.impl.YangParserImpl;
66
67 public class NetconfDeviceTest {
68
69     private static final NetconfMessage notification;
70
71     private static final ContainerNode compositeNode;
72
73     static {
74         try {
75             compositeNode = mockClass(ContainerNode.class);
76         } catch (final Exception e) {
77             throw new RuntimeException(e);
78         }
79         try {
80             notification = new NetconfMessage(XmlUtil.readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
81         } catch (Exception e) {
82             throw new ExceptionInInitializerError(e);
83         }
84     }
85
86     private static final DOMRpcResult rpcResultC = new DefaultDOMRpcResult(compositeNode);
87
88     public static final String TEST_NAMESPACE = "test:namespace";
89     public static final String TEST_MODULE = "test-module";
90     public static final String TEST_REVISION = "2013-07-22";
91     public static final SourceIdentifier TEST_SID = new SourceIdentifier(TEST_MODULE, Optional.of(TEST_REVISION));
92     public static final String TEST_CAPABILITY = TEST_NAMESPACE + "?module=" + TEST_MODULE + "&revision=" + TEST_REVISION;
93
94     public static final SourceIdentifier TEST_SID2 = new SourceIdentifier(TEST_MODULE + "2", Optional.of(TEST_REVISION));
95     public static final String TEST_CAPABILITY2 = TEST_NAMESPACE + "?module=" + TEST_MODULE + "2" + "&revision=" + TEST_REVISION;
96
97     private static final NetconfStateSchemas.NetconfStateSchemasResolver stateSchemasResolver = new NetconfStateSchemas.NetconfStateSchemasResolver() {
98
99         @Override
100         public NetconfStateSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
101             return NetconfStateSchemas.EMPTY;
102         }
103     };
104
105     @Test
106     public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
107         final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
108
109         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
110         final NetconfDeviceCommunicator listener = getListener();
111
112         final SchemaContextFactory schemaFactory = getSchemaFactory();
113
114         // Make fallback attempt to fail due to empty resolved sources
115         final SchemaResolutionException schemaResolutionException
116                 = new SchemaResolutionException("fail first",
117                 Collections.<SourceIdentifier>emptyList(), HashMultimap.<SourceIdentifier, ModuleImport>create());
118         doReturn(Futures.immediateFailedCheckedFuture(
119                 schemaResolutionException))
120                 .when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
121
122         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
123                 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, stateSchemasResolver);
124         final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(),true);
125         // Monitoring not supported
126         final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
127         device.onRemoteSessionUp(sessionCaps, listener);
128
129         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceDisconnected();
130         Mockito.verify(listener, Mockito.timeout(5000)).close();
131         Mockito.verify(schemaFactory, times(1)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
132     }
133
134     @Test
135     public void testNetconfDeviceMissingSource() throws Exception {
136         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
137         final NetconfDeviceCommunicator listener = getListener();
138         final SchemaContext schema = getSchema();
139
140         final SchemaContextFactory schemaFactory = getSchemaFactory();
141
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>() {
145             @Override
146             public Object answer(final InvocationOnMock invocation) throws Throwable {
147                 if(((Collection<?>) invocation.getArguments()[0]).size() == 2) {
148                     return Futures.immediateFailedCheckedFuture(schemaResolutionException);
149                 } else {
150                     return Futures.immediateCheckedFuture(schema);
151                 }
152             }
153         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
154
155         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
156                 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, new NetconfStateSchemas.NetconfStateSchemasResolver() {
157             @Override
158             public NetconfStateSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
159                 final Module first = Iterables.getFirst(schema.getModules(), null);
160                 final QName qName = QName.create(first.getQNameModule(), first.getName());
161                 final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
162                 final NetconfStateSchemas.RemoteYangSchema source2 = new NetconfStateSchemas.RemoteYangSchema(QName.create(first.getQNameModule(), "test-module2"));
163                 return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
164             }
165         });
166
167         final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(), true);
168         // Monitoring supported
169         final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
170         device.onRemoteSessionUp(sessionCaps, listener);
171
172         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
173         Mockito.verify(schemaFactory, times(2)).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
174     }
175
176     private SchemaSourceRegistry getSchemaRegistry() {
177         final SchemaSourceRegistry mock = mock(SchemaSourceRegistry.class);
178         final SchemaSourceRegistration<?> mockReg = mock(SchemaSourceRegistration.class);
179         doNothing().when(mockReg).close();
180         doReturn(mockReg).when(mock).registerSchemaSource(any(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider.class), any(PotentialSchemaSource.class));
181         return mock;
182     }
183
184     @Test
185     public void testNotificationBeforeSchema() throws Exception {
186         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
187         final NetconfDeviceCommunicator listener = getListener();
188
189         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
190                 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaFactory(), stateSchemasResolver);
191         final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(), true);
192
193         device.onNotification(notification);
194         device.onNotification(notification);
195
196         verify(facade, times(0)).onNotification(any(ContainerNode.class));
197
198         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
199                 Lists.newArrayList(TEST_CAPABILITY));
200
201         final DOMRpcService deviceRpc = mock(DOMRpcService.class);
202
203         device.handleSalInitializationSuccess(NetconfToNotificationTest.getNotificationSchemaContext(getClass()), sessionCaps, deviceRpc);
204
205         verify(facade, timeout(10000).times(2)).onNotification(any(ContainerNode.class));
206
207         device.onNotification(notification);
208         verify(facade, timeout(10000).times(3)).onNotification(any(ContainerNode.class));
209     }
210
211     @Test
212     public void testNetconfDeviceReconnect() throws Exception {
213         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
214         final NetconfDeviceCommunicator listener = getListener();
215
216         final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
217
218         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
219                 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaContextProviderFactory, stateSchemasResolver);
220         final NetconfDevice device = new NetconfDevice(schemaResourcesDTO, getId(), facade, getExecutor(), true);
221         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
222                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
223         device.onRemoteSessionUp(sessionCaps, listener);
224
225         verify(schemaContextProviderFactory, timeout(5000)).createSchemaContext(any(Collection.class));
226         verify(facade, timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
227
228         device.onRemoteSessionDown();
229         verify(facade, timeout(5000)).onDeviceDisconnected();
230
231         device.onRemoteSessionUp(sessionCaps, listener);
232
233         verify(schemaContextProviderFactory, timeout(5000).times(2)).createSchemaContext(any(Collection.class));
234         verify(facade, timeout(5000).times(2)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
235     }
236
237     private SchemaContextFactory getSchemaFactory() {
238         final SchemaContextFactory schemaFactory = mockClass(SchemaContextFactory.class);
239         doReturn(Futures.immediateCheckedFuture(getSchema())).when(schemaFactory).createSchemaContext(any(Collection.class));
240         return schemaFactory;
241     }
242
243     public static SchemaContext getSchema() {
244         final YangParserImpl parser = new YangParserImpl();
245         final List<InputStream> modelsToParse = Lists.newArrayList(
246                 NetconfDeviceTest.class.getResourceAsStream("/schemas/test-module.yang")
247         );
248         final Set<Module> models = parser.parseYangModelsFromStreams(modelsToParse);
249         return parser.resolveSchemaContext(models);
250     }
251
252     private RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
253         final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler = mockCloseableClass(RemoteDeviceHandler.class);
254         doNothing().when(remoteDeviceHandler).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(NetconfDeviceRpc.class));
255         doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
256         doNothing().when(remoteDeviceHandler).onNotification(any(ContainerNode.class));
257         return remoteDeviceHandler;
258     }
259
260     private <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass) throws Exception {
261         final T mock = mockClass(remoteDeviceHandlerClass);
262         doNothing().when(mock).close();
263         return mock;
264     }
265
266     private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
267         final T mock = mock(remoteDeviceHandlerClass);
268         Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
269         return mock;
270     }
271
272     public RemoteDeviceId getId() {
273         return new RemoteDeviceId("test-D");
274     }
275
276     public ExecutorService getExecutor() {
277         return Executors.newSingleThreadExecutor();
278     }
279
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;
286     }
287
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);
292
293         if(addMonitor) {
294             capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
295         }
296
297         capabilities.addAll(additionalCapabilities);
298
299         return NetconfSessionPreferences.fromStrings(
300                 capabilities);
301     }
302
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;
307     }
308 }