ae6726b7802f64efcdde29e272711e85a315af1c
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / NetconfDeviceTest.java
1 /*
2  * Copyright (c) 2014, 2015 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
9 package org.opendaylight.netconf.sal.connect.netconf;
10
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;
21
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;
77
78 public class NetconfDeviceTest {
79
80     private static final NetconfMessage notification;
81
82     private static final ContainerNode compositeNode;
83
84     static {
85         try {
86             compositeNode = mockClass(ContainerNode.class);
87         } catch (final Exception e) {
88             throw new RuntimeException(e);
89         }
90         try {
91             notification = new NetconfMessage(XmlUtil.readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
92         } catch (Exception e) {
93             throw new ExceptionInInitializerError(e);
94         }
95     }
96
97     private static final DOMRpcResult rpcResultC = new DefaultDOMRpcResult(compositeNode);
98
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;
105
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;
109
110     private static final NetconfDeviceSchemasResolver stateSchemasResolver = new NetconfDeviceSchemasResolver() {
111
112         @Override
113         public NetconfDeviceSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
114             return NetconfStateSchemas.EMPTY;
115         }
116     };
117
118     @Test
119     public void testNetconfDeviceFlawedModelFailedResolution() throws Exception {
120         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
121         final NetconfDeviceCommunicator listener = getListener();
122
123         final SchemaContextFactory schemaFactory = getSchemaFactory();
124         final SchemaContext schema = getSchema();
125         final SchemaRepository schemaRepository = getSchemaRepository();
126
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);
132             } else {
133                 return Futures.immediateCheckedFuture(schema);
134             }
135         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
136
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));
143         };
144
145         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
146                 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
147
148         final NetconfDevice device = new NetconfDeviceBuilder()
149                 .setReconnectOnSchemasChange(true)
150                 .setSchemaResourcesDTO(schemaResourcesDTO)
151                 .setGlobalProcessingExecutor(getExecutor())
152                 .setId(getId())
153                 .setSalFacade(facade)
154                 .build();
155         // Monitoring supported
156         final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
157         device.onRemoteSessionUp(sessionCaps, listener);
158
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));
161     }
162
163     @Test
164     public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
165         final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
166
167         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
168         final NetconfDeviceCommunicator listener = getListener();
169
170         final SchemaContextFactory schemaFactory = getSchemaFactory();
171         final SchemaRepository schemaRepository = getSchemaRepository();
172
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));
180
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())
187                 .setId(getId())
188                 .setSalFacade(facade)
189                 .build();
190
191         // Monitoring not supported
192         final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
193         device.onRemoteSessionUp(sessionCaps, listener);
194
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));
198     }
199
200     @Test
201     public void testNetconfDeviceMissingSource() throws Exception {
202         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
203         final NetconfDeviceCommunicator listener = getListener();
204         final SchemaContext schema = getSchema();
205
206         final SchemaContextFactory schemaFactory = getSchemaFactory();
207         final SchemaRepository schemaRepository = getSchemaRepository();
208
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>() {
213             @Override
214             public Object answer(final InvocationOnMock invocation) throws Throwable {
215                 if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
216                     return Futures.immediateFailedCheckedFuture(schemaResolutionException);
217                 } else {
218                     return Futures.immediateCheckedFuture(schema);
219                 }
220             }
221         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
222
223         final NetconfDeviceSchemasResolver stateSchemasResolver = new NetconfDeviceSchemasResolver() {
224             @Override
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));
231             }
232         };
233
234         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
235                 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
236
237         final NetconfDevice device = new NetconfDeviceBuilder()
238                 .setReconnectOnSchemasChange(true)
239                 .setSchemaResourcesDTO(schemaResourcesDTO)
240                 .setGlobalProcessingExecutor(getExecutor())
241                 .setId(getId())
242                 .setSalFacade(facade)
243                 .build();
244         // Monitoring supported
245         final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
246         device.onRemoteSessionUp(sessionCaps, listener);
247
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));
250     }
251
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));
257         return mock;
258     }
259
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));
264         return mock;
265     }
266
267     @Test
268     public void testNotificationBeforeSchema() throws Exception {
269         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
270         final NetconfDeviceCommunicator listener = getListener();
271
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())
278                 .setId(getId())
279                 .setSalFacade(facade)
280                 .build();
281
282         device.onNotification(notification);
283         device.onNotification(notification);
284
285         verify(facade, times(0)).onNotification(any(DOMNotification.class));
286
287         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
288                 Lists.newArrayList(TEST_CAPABILITY));
289
290         final DOMRpcService deviceRpc = mock(DOMRpcService.class);
291
292         device.handleSalInitializationSuccess(NetconfToNotificationTest.getNotificationSchemaContext(getClass(), false), sessionCaps, deviceRpc);
293
294         verify(facade, timeout(10000).times(2)).onNotification(any(DOMNotification.class));
295
296         device.onNotification(notification);
297         verify(facade, timeout(10000).times(3)).onNotification(any(DOMNotification.class));
298     }
299
300     @Test
301     public void testNetconfDeviceReconnect() throws Exception {
302         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
303         final NetconfDeviceCommunicator listener = getListener();
304
305         final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
306
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())
313                 .setId(getId())
314                 .setSalFacade(facade)
315                 .build();
316         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
317                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
318         device.onRemoteSessionUp(sessionCaps, listener);
319
320         verify(schemaContextProviderFactory, timeout(5000)).createSchemaContext(any(Collection.class));
321         verify(facade, timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
322
323         device.onRemoteSessionDown();
324         verify(facade, timeout(5000)).onDeviceDisconnected();
325
326         device.onRemoteSessionUp(sessionCaps, listener);
327
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));
330     }
331
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;
336     }
337
338     private static SchemaContext parseYangStreams(final List<InputStream> streams) {
339         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
340                 .newBuild();
341         final SchemaContext schemaContext;
342         try {
343             schemaContext = reactor.buildEffective(streams);
344         } catch (ReactorException e) {
345             throw new RuntimeException("Unable to build schema context from " + streams, e);
346         }
347         return schemaContext;
348     }
349
350     public static SchemaContext getSchema() {
351         final List<InputStream> modelsToParse = Lists.newArrayList(
352                 NetconfDeviceTest.class.getResourceAsStream("/schemas/test-module.yang")
353         );
354         return parseYangStreams(modelsToParse);
355     }
356
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;
363     }
364
365     private <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass) throws Exception {
366         final T mock = mockClass(remoteDeviceHandlerClass);
367         doNothing().when(mock).close();
368         return mock;
369     }
370
371     private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
372         final T mock = mock(remoteDeviceHandlerClass);
373         Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
374         return mock;
375     }
376
377     public RemoteDeviceId getId() {
378         return new RemoteDeviceId("test-D", InetSocketAddress.createUnresolved("localhost", 22));
379     }
380
381     public ExecutorService getExecutor() {
382         return Executors.newSingleThreadExecutor();
383     }
384
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;
391     }
392
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);
397
398         if(addMonitor) {
399             capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
400         }
401
402         capabilities.addAll(additionalCapabilities);
403
404         return NetconfSessionPreferences.fromStrings(
405                 capabilities);
406     }
407
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;
412     }
413 }