Bug 5403 - Support yang-library schema resolution
[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.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;
20
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;
73
74 public class NetconfDeviceTest {
75
76     private static final NetconfMessage notification;
77
78     private static final ContainerNode compositeNode;
79
80     static {
81         try {
82             compositeNode = mockClass(ContainerNode.class);
83         } catch (final Exception e) {
84             throw new RuntimeException(e);
85         }
86         try {
87             notification = new NetconfMessage(XmlUtil.readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
88         } catch (Exception e) {
89             throw new ExceptionInInitializerError(e);
90         }
91     }
92
93     private static final DOMRpcResult rpcResultC = new DefaultDOMRpcResult(compositeNode);
94
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;
101
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;
105
106     private static final NetconfDeviceSchemasResolver stateSchemasResolver = new NetconfDeviceSchemasResolver() {
107
108         @Override
109         public NetconfDeviceSchemas resolve(final NetconfDeviceRpc deviceRpc, final NetconfSessionPreferences remoteSessionCapabilities, final RemoteDeviceId id) {
110             return NetconfStateSchemas.EMPTY;
111         }
112     };
113
114     @Test
115     public void testNetconfDeviceFailFirstSchemaFailSecondEmpty() throws Exception {
116         final ArrayList<String> capList = Lists.newArrayList(TEST_CAPABILITY);
117
118         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
119         final NetconfDeviceCommunicator listener = getListener();
120
121         final SchemaContextFactory schemaFactory = getSchemaFactory();
122
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));
130
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())
137                 .setId(getId())
138                 .setSalFacade(facade)
139                 .build();
140
141         // Monitoring not supported
142         final NetconfSessionPreferences sessionCaps = getSessionCaps(false, capList);
143         device.onRemoteSessionUp(sessionCaps, listener);
144
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));
148     }
149
150     @Test
151     public void testNetconfDeviceMissingSource() throws Exception {
152         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
153         final NetconfDeviceCommunicator listener = getListener();
154         final SchemaContext schema = getSchema();
155
156         final SchemaContextFactory schemaFactory = getSchemaFactory();
157
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>() {
161             @Override
162             public Object answer(final InvocationOnMock invocation) throws Throwable {
163                 if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
164                     return Futures.immediateFailedCheckedFuture(schemaResolutionException);
165                 } else {
166                     return Futures.immediateCheckedFuture(schema);
167                 }
168             }
169         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
170
171         final NetconfDeviceSchemasResolver stateSchemasResolver = new NetconfDeviceSchemasResolver() {
172             @Override
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));
179             }
180         };
181
182         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
183                 = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), schemaFactory, stateSchemasResolver);
184
185         final NetconfDevice device = new NetconfDeviceBuilder()
186                 .setReconnectOnSchemasChange(true)
187                 .setSchemaResourcesDTO(schemaResourcesDTO)
188                 .setGlobalProcessingExecutor(getExecutor())
189                 .setId(getId())
190                 .setSalFacade(facade)
191                 .build();
192         // Monitoring supported
193         final NetconfSessionPreferences sessionCaps = getSessionCaps(true, Lists.newArrayList(TEST_CAPABILITY, TEST_CAPABILITY2));
194         device.onRemoteSessionUp(sessionCaps, listener);
195
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));
198     }
199
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));
205         return mock;
206     }
207
208     @Test
209     public void testNotificationBeforeSchema() throws Exception {
210         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
211         final NetconfDeviceCommunicator listener = getListener();
212
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())
219                 .setId(getId())
220                 .setSalFacade(facade)
221                 .build();
222
223         device.onNotification(notification);
224         device.onNotification(notification);
225
226         verify(facade, times(0)).onNotification(any(DOMNotification.class));
227
228         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
229                 Lists.newArrayList(TEST_CAPABILITY));
230
231         final DOMRpcService deviceRpc = mock(DOMRpcService.class);
232
233         device.handleSalInitializationSuccess(NetconfToNotificationTest.getNotificationSchemaContext(getClass(), false), sessionCaps, deviceRpc);
234
235         verify(facade, timeout(10000).times(2)).onNotification(any(DOMNotification.class));
236
237         device.onNotification(notification);
238         verify(facade, timeout(10000).times(3)).onNotification(any(DOMNotification.class));
239     }
240
241     @Test
242     public void testNetconfDeviceReconnect() throws Exception {
243         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
244         final NetconfDeviceCommunicator listener = getListener();
245
246         final SchemaContextFactory schemaContextProviderFactory = getSchemaFactory();
247
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())
254                 .setId(getId())
255                 .setSalFacade(facade)
256                 .build();
257         final NetconfSessionPreferences sessionCaps = getSessionCaps(true,
258                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
259         device.onRemoteSessionUp(sessionCaps, listener);
260
261         verify(schemaContextProviderFactory, timeout(5000)).createSchemaContext(any(Collection.class));
262         verify(facade, timeout(5000)).onDeviceConnected(any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
263
264         device.onRemoteSessionDown();
265         verify(facade, timeout(5000)).onDeviceDisconnected();
266
267         device.onRemoteSessionUp(sessionCaps, listener);
268
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));
271     }
272
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;
277     }
278
279     private static SchemaContext parseYangStreams(final List<InputStream> streams) {
280         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
281                 .newBuild();
282         final SchemaContext schemaContext;
283         try {
284             schemaContext = reactor.buildEffective(streams);
285         } catch (ReactorException e) {
286             throw new RuntimeException("Unable to build schema context from " + streams, e);
287         }
288         return schemaContext;
289     }
290
291     public static SchemaContext getSchema() {
292         final List<InputStream> modelsToParse = Lists.newArrayList(
293                 NetconfDeviceTest.class.getResourceAsStream("/schemas/test-module.yang")
294         );
295         return parseYangStreams(modelsToParse);
296     }
297
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;
304     }
305
306     private <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass) throws Exception {
307         final T mock = mockClass(remoteDeviceHandlerClass);
308         doNothing().when(mock).close();
309         return mock;
310     }
311
312     private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
313         final T mock = mock(remoteDeviceHandlerClass);
314         Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
315         return mock;
316     }
317
318     public RemoteDeviceId getId() {
319         return new RemoteDeviceId("test-D", InetSocketAddress.createUnresolved("localhost", 22));
320     }
321
322     public ExecutorService getExecutor() {
323         return Executors.newSingleThreadExecutor();
324     }
325
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;
332     }
333
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);
338
339         if(addMonitor) {
340             capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
341         }
342
343         capabilities.addAll(additionalCapabilities);
344
345         return NetconfSessionPreferences.fromStrings(
346                 capabilities);
347     }
348
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;
353     }
354 }