f1dbd0dca048b8c5615bce4a5be3befcb5d12ef3
[yangtools.git] / yang / yang-repo-fs / src / test / java / org / opendaylight / yangtools / yang / model / repo / fs / FilesystemSchemaSourceCacheIntegrationTest.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.model.repo.fs;
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.assertNotNull;
15 import static org.junit.Assert.fail;
16 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
17
18 import com.google.common.base.MoreObjects.ToStringHelper;
19 import com.google.common.collect.Lists;
20 import com.google.common.io.Files;
21 import com.google.common.util.concurrent.FutureCallback;
22 import com.google.common.util.concurrent.Futures;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import com.google.common.util.concurrent.MoreExecutors;
25 import java.io.ByteArrayInputStream;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.charset.StandardCharsets;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Optional;
34 import java.util.concurrent.ExecutionException;
35 import org.junit.Test;
36 import org.opendaylight.yangtools.yang.common.Revision;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
40 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
41 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
42 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
43 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
44 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
45 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
46 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
47 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
48
49 public class FilesystemSchemaSourceCacheIntegrationTest {
50     @Test
51     public void testWithCacheStartup() throws Exception {
52         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
53
54         class CountingSchemaListener implements SchemaSourceListener {
55             List<PotentialSchemaSource<?>> registeredSources = new ArrayList<>();
56
57             @Override
58             public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
59             }
60
61             @Override
62             public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
63                 for (final PotentialSchemaSource<?> source : sources) {
64                     registeredSources.add(source);
65                 }
66             }
67
68             @Override
69             public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
70             }
71         }
72
73         final File tempDir = Files.createTempDir();
74
75         final CountingSchemaListener listener = new CountingSchemaListener();
76         sharedSchemaRepository.registerSchemaSourceListener(listener);
77
78         final File test = new File(tempDir, "test.yang");
79         Files.asCharSink(test, StandardCharsets.UTF_8).write("content-test");
80
81         final File test2 = new File(tempDir, "test@2012-12-12.yang");
82         Files.asCharSink(test2, StandardCharsets.UTF_8).write("content-test-2012");
83
84         final File test3 = new File(tempDir, "test@2013-12-12.yang");
85         Files.asCharSink(test3, StandardCharsets.UTF_8).write("content-test-2013");
86
87         final File test4 = new File(tempDir, "module@2010-12-12.yang");
88         Files.asCharSink(test4, StandardCharsets.UTF_8).write("content-module-2010");
89
90         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(
91                 sharedSchemaRepository, YangTextSchemaSource.class, tempDir);
92         sharedSchemaRepository.registerSchemaSourceListener(cache);
93
94         assertEquals(4, listener.registeredSources.size());
95
96         assertThat(Lists.transform(listener.registeredSources, PotentialSchemaSource::getSourceIdentifier),
97                 both(hasItem(RevisionSourceIdentifier.create("test", Optional.empty())))
98                         .and(hasItem(RevisionSourceIdentifier.create("test", Revision.of("2012-12-12"))))
99                         .and(hasItem(RevisionSourceIdentifier.create("test", Revision.of("2013-12-12"))))
100                         .and(hasItem(RevisionSourceIdentifier.create("module", Revision.of("2010-12-12"))))
101         );
102     }
103
104     @Test
105     public void testWithCacheRunning() throws Exception {
106         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
107
108         final File storageDir = Files.createTempDir();
109
110         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(
111                 sharedSchemaRepository, YangTextSchemaSource.class, storageDir);
112         sharedSchemaRepository.registerSchemaSourceListener(cache);
113
114         final SourceIdentifier runningId = RevisionSourceIdentifier.create("running", Revision.of("2012-12-12"));
115
116         sharedSchemaRepository.registerSchemaSource(sourceIdentifier -> immediateFluentFuture(
117             new YangTextSchemaSource(runningId) {
118                 @Override
119                 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
120                     return toStringHelper;
121                 }
122
123                 @Override
124                 public InputStream openStream() throws IOException {
125                     return new ByteArrayInputStream("running".getBytes(StandardCharsets.UTF_8));
126                 }
127
128                 @Override
129                 public Optional<String> getSymbolicName() {
130                     return Optional.empty();
131                 }
132             }), PotentialSchemaSource.create(runningId, YangTextSchemaSource.class,
133                 PotentialSchemaSource.Costs.REMOTE_IO.getValue()));
134
135         final TextToIRTransformer transformer = TextToIRTransformer.create(sharedSchemaRepository,
136             sharedSchemaRepository);
137         sharedSchemaRepository.registerSchemaSourceListener(transformer);
138
139         // Request schema to make repository notify the cache
140         final ListenableFuture<EffectiveModelContext> schemaFuture = sharedSchemaRepository
141                 .createEffectiveModelContextFactory()
142                 .createEffectiveModelContext(runningId);
143         Futures.addCallback(schemaFuture, new FutureCallback<SchemaContext>() {
144             @Override
145             public void onSuccess(final SchemaContext result) {
146                 fail("Creation of schema context should fail from non-regular sources");
147             }
148
149             @Override
150             public void onFailure(final Throwable cause) {
151                 // Creation of schema context fails, since we do not provide regular sources, but we just want
152                 // to check cache
153                 final List<File> cachedSchemas = Arrays.asList(storageDir.listFiles());
154                 assertEquals(1, cachedSchemas.size());
155                 assertEquals(Files.getNameWithoutExtension(cachedSchemas.get(0).getName()), "running@2012-12-12");
156             }
157         }, MoreExecutors.directExecutor());
158
159         try {
160             schemaFuture.get();
161         } catch (final ExecutionException e) {
162             assertNotNull(e.getCause());
163             assertEquals(MissingSchemaSourceException.class, e.getCause().getClass());
164             return;
165         }
166
167         fail("Creation of schema context should fail from non-regular sources");
168     }
169 }