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.model.repo.fs;
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.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
17 import com.google.common.base.MoreObjects.ToStringHelper;
18 import com.google.common.collect.Lists;
19 import com.google.common.io.Files;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
23 import java.io.IOException;
24 import java.io.Reader;
25 import java.io.StringReader;
26 import java.nio.charset.StandardCharsets;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.concurrent.ExecutionException;
31 import org.junit.jupiter.api.Test;
32 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
33 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
34 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
35 import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
36 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
37 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
38 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
39 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
40 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
41 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
43 public class FilesystemSchemaSourceCacheIntegrationTest {
45 public void testWithCacheStartup() throws IOException {
46 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
48 class CountingSchemaListener implements SchemaSourceListener {
49 List<PotentialSchemaSource<?>> registeredSources = new ArrayList<>();
52 public void schemaSourceEncountered(final SourceRepresentation source) {
56 public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
57 for (final PotentialSchemaSource<?> source : sources) {
58 registeredSources.add(source);
63 public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
67 final File tempDir = Files.createTempDir();
69 final CountingSchemaListener listener = new CountingSchemaListener();
70 sharedSchemaRepository.registerSchemaSourceListener(listener);
72 final File test = new File(tempDir, "test.yang");
73 Files.asCharSink(test, StandardCharsets.UTF_8).write("content-test");
75 final File test2 = new File(tempDir, "test@2012-12-12.yang");
76 Files.asCharSink(test2, StandardCharsets.UTF_8).write("content-test-2012");
78 final File test3 = new File(tempDir, "test@2013-12-12.yang");
79 Files.asCharSink(test3, StandardCharsets.UTF_8).write("content-test-2013");
81 final File test4 = new File(tempDir, "module@2010-12-12.yang");
82 Files.asCharSink(test4, StandardCharsets.UTF_8).write("content-module-2010");
84 final FilesystemSchemaSourceCache<YangTextSource> cache = new FilesystemSchemaSourceCache<>(
85 sharedSchemaRepository, YangTextSource.class, tempDir);
86 sharedSchemaRepository.registerSchemaSourceListener(cache);
88 assertEquals(4, listener.registeredSources.size());
90 assertThat(Lists.transform(listener.registeredSources, PotentialSchemaSource::getSourceIdentifier),
91 both(hasItem(new SourceIdentifier("test")))
92 .and(hasItem(new SourceIdentifier("test", "2012-12-12")))
93 .and(hasItem(new SourceIdentifier("test", "2013-12-12")))
94 .and(hasItem(new SourceIdentifier("module", "2010-12-12")))
99 public void testWithCacheRunning() {
100 final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
102 final File storageDir = Files.createTempDir();
104 final FilesystemSchemaSourceCache<YangTextSource> cache = new FilesystemSchemaSourceCache<>(
105 sharedSchemaRepository, YangTextSource.class, storageDir);
106 sharedSchemaRepository.registerSchemaSourceListener(cache);
108 final SourceIdentifier runningId = new SourceIdentifier("running", "2012-12-12");
110 sharedSchemaRepository.registerSchemaSource(sourceIdentifier -> FluentFutures.immediateFluentFuture(
111 new YangTextSource(runningId) {
113 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
114 return toStringHelper;
118 public Reader openStream() throws IOException {
119 return new StringReader("running");
123 public String symbolicName() {
126 }), PotentialSchemaSource.create(runningId, YangTextSource.class,
127 PotentialSchemaSource.Costs.REMOTE_IO.getValue()));
129 final TextToIRTransformer transformer = TextToIRTransformer.create(sharedSchemaRepository,
130 sharedSchemaRepository);
131 sharedSchemaRepository.registerSchemaSourceListener(transformer);
133 // Request schema to make repository notify the cache
134 final ListenableFuture<EffectiveModelContext> schemaFuture = sharedSchemaRepository
135 .createEffectiveModelContextFactory()
136 .createEffectiveModelContext(runningId);
138 final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(schemaFuture)).getCause();
139 assertInstanceOf(MissingSchemaSourceException.class, cause);
141 // Creation of schema context fails, since we do not provide regular sources, but we just want
143 final List<File> cachedSchemas = Arrays.asList(storageDir.listFiles());
144 assertEquals(1, cachedSchemas.size());
145 assertEquals(Files.getNameWithoutExtension(cachedSchemas.get(0).getName()), "running@2012-12-12");