Remove deprecated SchemaRepository.createSchemaContextFactory()
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaRepositoryTest.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.yangtools.yang.parser.repo;
9
10 import static org.hamcrest.CoreMatchers.both;
11 import static org.hamcrest.CoreMatchers.hasItem;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertSame;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19 import static org.mockito.Mockito.spy;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
23
24 import com.google.common.base.MoreObjects.ToStringHelper;
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.Lists;
27 import com.google.common.io.Files;
28 import com.google.common.util.concurrent.FutureCallback;
29 import com.google.common.util.concurrent.Futures;
30 import com.google.common.util.concurrent.ListenableFuture;
31 import com.google.common.util.concurrent.MoreExecutors;
32 import java.io.ByteArrayInputStream;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.nio.charset.StandardCharsets;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.Optional;
41 import java.util.concurrent.ExecutionException;
42 import org.junit.Test;
43 import org.opendaylight.yangtools.yang.common.Revision;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
46 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
47 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
48 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
49 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
50 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
51 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
52 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
53 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
54 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
55 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
56
57 public class SharedSchemaRepositoryTest {
58
59     @Test
60     public void testSourceWithAndWithoutRevision() throws Exception {
61         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
62
63         final SourceIdentifier idNoRevision = loadAndRegisterSource(sharedSchemaRepository,
64             "/no-revision/imported.yang");
65         final SourceIdentifier id2 = loadAndRegisterSource(sharedSchemaRepository,
66             "/no-revision/imported@2012-12-12.yang");
67
68         ListenableFuture<ASTSchemaSource> source = sharedSchemaRepository.getSchemaSource(idNoRevision,
69             ASTSchemaSource.class);
70         assertEquals(idNoRevision, source.get().getIdentifier());
71         source = sharedSchemaRepository.getSchemaSource(id2, ASTSchemaSource.class);
72         assertEquals(id2, source.get().getIdentifier());
73     }
74
75     private static SourceIdentifier loadAndRegisterSource(final SharedSchemaRepository sharedSchemaRepository,
76             final String resourceName) throws Exception {
77         final SettableSchemaProvider<ASTSchemaSource> sourceProvider = getImmediateYangSourceProviderFromResource(
78             resourceName);
79         sourceProvider.setResult();
80         final SourceIdentifier idNoRevision = sourceProvider.getId();
81         sourceProvider.register(sharedSchemaRepository);
82         return idNoRevision;
83     }
84
85     @Test
86     public void testSimpleSchemaContext() throws Exception {
87         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
88
89         final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = getImmediateYangSourceProviderFromResource(
90             "/ietf/ietf-inet-types@2010-09-24.yang");
91         remoteInetTypesYang.register(sharedSchemaRepository);
92         final ListenableFuture<ASTSchemaSource> registeredSourceFuture = sharedSchemaRepository.getSchemaSource(
93             remoteInetTypesYang.getId(), ASTSchemaSource.class);
94         assertFalse(registeredSourceFuture.isDone());
95
96         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
97         final ListenableFuture<SchemaContext> schemaContextFuture =
98                 fact.createSchemaContext(ImmutableList.of(remoteInetTypesYang.getId()));
99
100         assertFalse(schemaContextFuture.isDone());
101
102         // Make source appear
103         remoteInetTypesYang.setResult();
104         assertEquals(remoteInetTypesYang.getSchemaSourceRepresentation(), registeredSourceFuture.get());
105
106         // Verify schema created successfully
107         assertTrue(schemaContextFuture.isDone());
108         final SchemaContext firstSchemaContext = schemaContextFuture.get();
109         assertSchemaContext(firstSchemaContext, 1);
110
111         // Try same schema second time
112         final ListenableFuture<SchemaContext> secondSchemaFuture = sharedSchemaRepository.createSchemaContextFactory()
113                 .createSchemaContext(ImmutableList.of(remoteInetTypesYang.getId()));
114
115         // Verify second schema created successfully immediately
116         assertTrue(secondSchemaFuture.isDone());
117         // Assert same context instance is returned from first and second attempt
118         assertSame(firstSchemaContext, secondSchemaFuture.get());
119     }
120
121     @Test
122     public void testTwoSchemaContextsSharingSource() throws Exception {
123         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
124
125         final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = getImmediateYangSourceProviderFromResource(
126             "/ietf/ietf-inet-types@2010-09-24.yang");
127         remoteInetTypesYang.register(sharedSchemaRepository);
128         remoteInetTypesYang.setResult();
129         final SettableSchemaProvider<ASTSchemaSource> remoteTopologyYang = getImmediateYangSourceProviderFromResource(
130             "/ietf/network-topology@2013-10-21.yang");
131         remoteTopologyYang.register(sharedSchemaRepository);
132         remoteTopologyYang.setResult();
133         final SettableSchemaProvider<ASTSchemaSource> remoteModuleNoRevYang =
134                 getImmediateYangSourceProviderFromResource("/no-revision/module-without-revision.yang");
135         remoteModuleNoRevYang.register(sharedSchemaRepository);
136
137         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
138         final ListenableFuture<SchemaContext> inetAndTopologySchemaContextFuture = fact
139                 .createSchemaContext(ImmutableList.of(remoteInetTypesYang.getId(), remoteTopologyYang.getId()));
140         assertTrue(inetAndTopologySchemaContextFuture.isDone());
141         assertSchemaContext(inetAndTopologySchemaContextFuture.get(), 2);
142
143         final ListenableFuture<SchemaContext> inetAndNoRevSchemaContextFuture =
144                 fact.createSchemaContext(ImmutableList.of(remoteInetTypesYang.getId(), remoteModuleNoRevYang.getId()));
145         assertFalse(inetAndNoRevSchemaContextFuture.isDone());
146
147         remoteModuleNoRevYang.setResult();
148         assertTrue(inetAndNoRevSchemaContextFuture.isDone());
149         assertSchemaContext(inetAndNoRevSchemaContextFuture.get(), 2);
150     }
151
152     @Test
153     public void testFailedSchemaContext() throws Exception {
154         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
155
156         final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = getImmediateYangSourceProviderFromResource(
157             "/ietf/ietf-inet-types@2010-09-24.yang");
158         remoteInetTypesYang.register(sharedSchemaRepository);
159
160         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
161
162         // Make source appear
163         final Throwable ex = new IllegalStateException("failed schema");
164         remoteInetTypesYang.setException(ex);
165
166         final ListenableFuture<SchemaContext> schemaContextFuture = fact.createSchemaContext(
167             ImmutableList.of(remoteInetTypesYang.getId()));
168
169         try {
170             schemaContextFuture.get();
171         } catch (final ExecutionException e) {
172             assertNotNull(e.getCause());
173             assertNotNull(e.getCause().getCause());
174             assertSame(ex, e.getCause().getCause());
175             return;
176         }
177
178         fail("Schema context creation should have failed");
179     }
180
181     @Test
182     public void testDifferentCosts() throws Exception {
183         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
184
185         final SettableSchemaProvider<ASTSchemaSource> immediateInetTypesYang = spy(
186             getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"));
187         immediateInetTypesYang.register(sharedSchemaRepository);
188         immediateInetTypesYang.setResult();
189
190         final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = spy(
191             getRemoteYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"));
192         remoteInetTypesYang.register(sharedSchemaRepository);
193         remoteInetTypesYang.setResult();
194
195         final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
196         final ListenableFuture<SchemaContext> schemaContextFuture =
197                 fact.createSchemaContext(ImmutableList.of(remoteInetTypesYang.getId()));
198
199         assertSchemaContext(schemaContextFuture.get(), 1);
200
201         final SourceIdentifier id = immediateInetTypesYang.getId();
202         verify(remoteInetTypesYang, times(0)).getSource(id);
203         verify(immediateInetTypesYang).getSource(id);
204     }
205
206     @Test
207     public void testWithCacheStartup() throws Exception {
208         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
209
210         class CountingSchemaListener implements SchemaSourceListener {
211             List<PotentialSchemaSource<?>> registeredSources = new ArrayList<>();
212
213             @Override
214             public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
215             }
216
217             @Override
218             public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
219                 for (final PotentialSchemaSource<?> source : sources) {
220                     registeredSources.add(source);
221                 }
222             }
223
224             @Override
225             public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
226             }
227         }
228
229         final File storageDir = Files.createTempDir();
230
231         final CountingSchemaListener listener = new CountingSchemaListener();
232         sharedSchemaRepository.registerSchemaSourceListener(listener);
233
234         final File test = new File(storageDir, "test.yang");
235         Files.asCharSink(test, StandardCharsets.UTF_8).write("content-test");
236
237         final File test2 = new File(storageDir, "test@2012-12-12.yang");
238         Files.asCharSink(test2, StandardCharsets.UTF_8).write("content-test-2012");
239
240         final File test3 = new File(storageDir, "test@2013-12-12.yang");
241         Files.asCharSink(test3, StandardCharsets.UTF_8).write("content-test-2013");
242
243         final File test4 = new File(storageDir, "module@2010-12-12.yang");
244         Files.asCharSink(test4, StandardCharsets.UTF_8).write("content-module-2010");
245
246         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(
247                 sharedSchemaRepository, YangTextSchemaSource.class, storageDir);
248         sharedSchemaRepository.registerSchemaSourceListener(cache);
249
250         assertEquals(4, listener.registeredSources.size());
251
252         assertThat(Lists.transform(listener.registeredSources, PotentialSchemaSource::getSourceIdentifier),
253                 both(hasItem(RevisionSourceIdentifier.create("test", Optional.empty())))
254                         .and(hasItem(RevisionSourceIdentifier.create("test", Revision.of("2012-12-12"))))
255                         .and(hasItem(RevisionSourceIdentifier.create("test", Revision.of("2013-12-12"))))
256                         .and(hasItem(RevisionSourceIdentifier.create("module", Revision.of("2010-12-12"))))
257         );
258     }
259
260     @Test
261     public void testWithCacheRunning() throws Exception {
262         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
263
264         final File storageDir = Files.createTempDir();
265
266         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(
267                 sharedSchemaRepository, YangTextSchemaSource.class, storageDir);
268         sharedSchemaRepository.registerSchemaSourceListener(cache);
269
270         final SourceIdentifier runningId = RevisionSourceIdentifier.create("running", Revision.of("2012-12-12"));
271
272         sharedSchemaRepository.registerSchemaSource(sourceIdentifier -> immediateFluentFuture(
273             new YangTextSchemaSource(runningId) {
274                 @Override
275                 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
276                     return toStringHelper;
277                 }
278
279                 @Override
280                 public InputStream openStream() throws IOException {
281                     return new ByteArrayInputStream("running".getBytes(StandardCharsets.UTF_8));
282                 }
283             }), PotentialSchemaSource.create(runningId, YangTextSchemaSource.class,
284                 PotentialSchemaSource.Costs.REMOTE_IO.getValue()));
285
286         final TextToASTTransformer transformer = TextToASTTransformer.create(sharedSchemaRepository,
287             sharedSchemaRepository);
288         sharedSchemaRepository.registerSchemaSourceListener(transformer);
289
290         // Request schema to make repository notify the cache
291         final ListenableFuture<SchemaContext> schemaFuture = sharedSchemaRepository
292                 .createSchemaContextFactory()
293                 .createSchemaContext(ImmutableList.of(runningId));
294         Futures.addCallback(schemaFuture, new FutureCallback<SchemaContext>() {
295             @Override
296             public void onSuccess(final SchemaContext result) {
297                 fail("Creation of schema context should fail from non-regular sources");
298             }
299
300             @Override
301             public void onFailure(final Throwable cause) {
302                 // Creation of schema context fails, since we do not provide regular sources, but we just want
303                 // to check cache
304                 final List<File> cachedSchemas = Arrays.asList(storageDir.listFiles());
305                 assertEquals(1, cachedSchemas.size());
306                 assertEquals(Files.getNameWithoutExtension(cachedSchemas.get(0).getName()), "running@2012-12-12");
307             }
308         }, MoreExecutors.directExecutor());
309
310         try {
311             schemaFuture.get();
312         } catch (final ExecutionException e) {
313             assertNotNull(e.getCause());
314             assertEquals(MissingSchemaSourceException.class, e.getCause().getClass());
315             return;
316         }
317
318         fail("Creation of schema context should fail from non-regular sources");
319     }
320
321     private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
322         assertNotNull(schemaContext);
323         assertEquals(moduleSize, schemaContext.getModules().size());
324     }
325
326     static SettableSchemaProvider<ASTSchemaSource> getRemoteYangSourceProviderFromResource(final String resourceName)
327             throws Exception {
328         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
329         return SettableSchemaProvider.createRemote(TextToASTTransformer.transformText(yangSource),
330             ASTSchemaSource.class);
331     }
332
333     static SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(final String resourceName)
334             throws Exception {
335         final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
336         return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
337             ASTSchemaSource.class);
338     }
339 }