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.yangtools.yang.parser.repo;
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;
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;
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;
57 public class SharedSchemaRepositoryTest {
60 public void testSourceWithAndWithoutRevision() throws Exception {
61 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
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");
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());
75 private static SourceIdentifier loadAndRegisterSource(final SharedSchemaRepository sharedSchemaRepository,
76 final String resourceName) throws Exception {
77 final SettableSchemaProvider<ASTSchemaSource> sourceProvider = getImmediateYangSourceProviderFromResource(
79 sourceProvider.setResult();
80 final SourceIdentifier idNoRevision = sourceProvider.getId();
81 sourceProvider.register(sharedSchemaRepository);
86 public void testSimpleSchemaContext() throws Exception {
87 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
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());
96 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
97 final ListenableFuture<SchemaContext> schemaContextFuture =
98 fact.createSchemaContext(ImmutableList.of(remoteInetTypesYang.getId()));
100 assertFalse(schemaContextFuture.isDone());
102 // Make source appear
103 remoteInetTypesYang.setResult();
104 assertEquals(remoteInetTypesYang.getSchemaSourceRepresentation(), registeredSourceFuture.get());
106 // Verify schema created successfully
107 assertTrue(schemaContextFuture.isDone());
108 final SchemaContext firstSchemaContext = schemaContextFuture.get();
109 assertSchemaContext(firstSchemaContext, 1);
111 // Try same schema second time
112 final ListenableFuture<SchemaContext> secondSchemaFuture = sharedSchemaRepository.createSchemaContextFactory()
113 .createSchemaContext(ImmutableList.of(remoteInetTypesYang.getId()));
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());
122 public void testTwoSchemaContextsSharingSource() throws Exception {
123 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
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);
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);
143 final ListenableFuture<SchemaContext> inetAndNoRevSchemaContextFuture =
144 fact.createSchemaContext(ImmutableList.of(remoteInetTypesYang.getId(), remoteModuleNoRevYang.getId()));
145 assertFalse(inetAndNoRevSchemaContextFuture.isDone());
147 remoteModuleNoRevYang.setResult();
148 assertTrue(inetAndNoRevSchemaContextFuture.isDone());
149 assertSchemaContext(inetAndNoRevSchemaContextFuture.get(), 2);
153 public void testFailedSchemaContext() throws Exception {
154 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
156 final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = getImmediateYangSourceProviderFromResource(
157 "/ietf/ietf-inet-types@2010-09-24.yang");
158 remoteInetTypesYang.register(sharedSchemaRepository);
160 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
162 // Make source appear
163 final Throwable ex = new IllegalStateException("failed schema");
164 remoteInetTypesYang.setException(ex);
166 final ListenableFuture<SchemaContext> schemaContextFuture = fact.createSchemaContext(
167 ImmutableList.of(remoteInetTypesYang.getId()));
170 schemaContextFuture.get();
171 } catch (final ExecutionException e) {
172 assertNotNull(e.getCause());
173 assertNotNull(e.getCause().getCause());
174 assertSame(ex, e.getCause().getCause());
178 fail("Schema context creation should have failed");
182 public void testDifferentCosts() throws Exception {
183 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
185 final SettableSchemaProvider<ASTSchemaSource> immediateInetTypesYang = spy(
186 getImmediateYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"));
187 immediateInetTypesYang.register(sharedSchemaRepository);
188 immediateInetTypesYang.setResult();
190 final SettableSchemaProvider<ASTSchemaSource> remoteInetTypesYang = spy(
191 getRemoteYangSourceProviderFromResource("/ietf/ietf-inet-types@2010-09-24.yang"));
192 remoteInetTypesYang.register(sharedSchemaRepository);
193 remoteInetTypesYang.setResult();
195 final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory();
196 final ListenableFuture<SchemaContext> schemaContextFuture =
197 fact.createSchemaContext(ImmutableList.of(remoteInetTypesYang.getId()));
199 assertSchemaContext(schemaContextFuture.get(), 1);
201 final SourceIdentifier id = immediateInetTypesYang.getId();
202 verify(remoteInetTypesYang, times(0)).getSource(id);
203 verify(immediateInetTypesYang).getSource(id);
207 public void testWithCacheStartup() throws Exception {
208 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
210 class CountingSchemaListener implements SchemaSourceListener {
211 List<PotentialSchemaSource<?>> registeredSources = new ArrayList<>();
214 public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
218 public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
219 for (final PotentialSchemaSource<?> source : sources) {
220 registeredSources.add(source);
225 public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
229 final File storageDir = Files.createTempDir();
231 final CountingSchemaListener listener = new CountingSchemaListener();
232 sharedSchemaRepository.registerSchemaSourceListener(listener);
234 final File test = new File(storageDir, "test.yang");
235 Files.asCharSink(test, StandardCharsets.UTF_8).write("content-test");
237 final File test2 = new File(storageDir, "test@2012-12-12.yang");
238 Files.asCharSink(test2, StandardCharsets.UTF_8).write("content-test-2012");
240 final File test3 = new File(storageDir, "test@2013-12-12.yang");
241 Files.asCharSink(test3, StandardCharsets.UTF_8).write("content-test-2013");
243 final File test4 = new File(storageDir, "module@2010-12-12.yang");
244 Files.asCharSink(test4, StandardCharsets.UTF_8).write("content-module-2010");
246 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(
247 sharedSchemaRepository, YangTextSchemaSource.class, storageDir);
248 sharedSchemaRepository.registerSchemaSourceListener(cache);
250 assertEquals(4, listener.registeredSources.size());
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"))))
261 public void testWithCacheRunning() throws Exception {
262 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
264 final File storageDir = Files.createTempDir();
266 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(
267 sharedSchemaRepository, YangTextSchemaSource.class, storageDir);
268 sharedSchemaRepository.registerSchemaSourceListener(cache);
270 final SourceIdentifier runningId = RevisionSourceIdentifier.create("running", Revision.of("2012-12-12"));
272 sharedSchemaRepository.registerSchemaSource(sourceIdentifier -> immediateFluentFuture(
273 new YangTextSchemaSource(runningId) {
275 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
276 return toStringHelper;
280 public InputStream openStream() throws IOException {
281 return new ByteArrayInputStream("running".getBytes(StandardCharsets.UTF_8));
283 }), PotentialSchemaSource.create(runningId, YangTextSchemaSource.class,
284 PotentialSchemaSource.Costs.REMOTE_IO.getValue()));
286 final TextToASTTransformer transformer = TextToASTTransformer.create(sharedSchemaRepository,
287 sharedSchemaRepository);
288 sharedSchemaRepository.registerSchemaSourceListener(transformer);
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>() {
296 public void onSuccess(final SchemaContext result) {
297 fail("Creation of schema context should fail from non-regular sources");
301 public void onFailure(final Throwable cause) {
302 // Creation of schema context fails, since we do not provide regular sources, but we just want
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");
308 }, MoreExecutors.directExecutor());
312 } catch (final ExecutionException e) {
313 assertNotNull(e.getCause());
314 assertEquals(MissingSchemaSourceException.class, e.getCause().getClass());
318 fail("Creation of schema context should fail from non-regular sources");
321 private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
322 assertNotNull(schemaContext);
323 assertEquals(moduleSize, schemaContext.getModules().size());
326 static SettableSchemaProvider<ASTSchemaSource> getRemoteYangSourceProviderFromResource(final String resourceName)
328 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
329 return SettableSchemaProvider.createRemote(TextToASTTransformer.transformText(yangSource),
330 ASTSchemaSource.class);
333 static SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(final String resourceName)
335 final YangTextSchemaSource yangSource = YangTextSchemaSource.forResource(resourceName);
336 return SettableSchemaProvider.createImmediate(TextToASTTransformer.transformText(yangSource),
337 ASTSchemaSource.class);