1e5a56ad9002a1a3d4967eff809120861a96b6df
[yangtools.git] / yang / yang-repo-fs / src / main / java / org / opendaylight / yangtools / yang / model / repo / fs / FilesystemSchemaSourceCache.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 com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
13 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
14
15 import com.google.common.util.concurrent.FluentFuture;
16 import java.io.File;
17 import java.io.FilenameFilter;
18 import java.io.IOException;
19 import java.nio.charset.StandardCharsets;
20 import java.nio.file.FileVisitResult;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.nio.file.SimpleFileVisitor;
24 import java.nio.file.StandardCopyOption;
25 import java.nio.file.attribute.BasicFileAttributes;
26 import java.time.format.DateTimeParseException;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.TreeMap;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35 import org.opendaylight.yangtools.yang.common.Revision;
36 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
38 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
39 import org.opendaylight.yangtools.yang.model.repo.spi.AbstractSchemaSourceCache;
40 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
41 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
42 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Cache implementation that stores schemas in form of files under provided folder.
48  */
49 public final class FilesystemSchemaSourceCache<T extends SourceRepresentation> extends AbstractSchemaSourceCache<T> {
50     private static final Logger LOG = LoggerFactory.getLogger(FilesystemSchemaSourceCache.class);
51
52     // Init storage adapters
53     private static final Map<Class<? extends SourceRepresentation>, StorageAdapter<? extends SourceRepresentation>>
54         STORAGE_ADAPTERS = Collections.singletonMap(YangTextSource.class, new YangTextStorageAdapter());
55
56     private static final Pattern CACHED_FILE_PATTERN =
57             Pattern.compile("(?<moduleName>[^@]+)" + "(@(?<revision>" + Revision.STRING_FORMAT_PATTERN + "))?");
58
59     private final Class<T> representation;
60     private final File storageDirectory;
61
62     public FilesystemSchemaSourceCache(final SchemaSourceRegistry consumer, final Class<T> representation,
63             final File storageDirectory) {
64         super(consumer, representation, Costs.LOCAL_IO);
65         this.representation = representation;
66         this.storageDirectory = requireNonNull(storageDirectory);
67
68         checkSupportedRepresentation(representation);
69
70         checkArgument(storageDirectory.mkdirs() || storageDirectory.isDirectory(),
71                 "Unable to create cache directory at %s", storageDirectory);
72         checkArgument(storageDirectory.canWrite());
73         checkArgument(storageDirectory.canRead());
74
75         init();
76     }
77
78     private static void checkSupportedRepresentation(final Class<? extends SourceRepresentation> representation) {
79         for (final var supportedRepresentation : STORAGE_ADAPTERS.keySet()) {
80             if (supportedRepresentation.isAssignableFrom(representation)) {
81                 return;
82             }
83         }
84
85         throw new IllegalArgumentException(String.format(
86             "This cache does not support representation: %s, supported representations are: %s",
87             representation, STORAGE_ADAPTERS.keySet()));
88     }
89
90     /**
91      * Restore cache state.
92      */
93     private void init() {
94
95         final CachedModulesFileVisitor fileVisitor = new CachedModulesFileVisitor();
96         try {
97             Files.walkFileTree(storageDirectory.toPath(), fileVisitor);
98         } catch (final IOException e) {
99             LOG.warn("Unable to restore cache from {}. Starting with an empty cache", storageDirectory, e);
100             return;
101         }
102
103         fileVisitor.getCachedSchemas().stream().forEach(this::register);
104     }
105
106     @Override
107     public synchronized FluentFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
108         final File file = sourceIdToFile(sourceIdentifier, storageDirectory);
109         if (file.exists() && file.canRead()) {
110             LOG.trace("Source {} found in cache as {}", sourceIdentifier, file);
111             final var restored = STORAGE_ADAPTERS.get(representation).restore(sourceIdentifier, file);
112             return immediateFluentFuture(representation.cast(restored));
113         }
114
115         LOG.debug("Source {} not found in cache as {}", sourceIdentifier, file);
116         return immediateFailedFluentFuture(new MissingSchemaSourceException(sourceIdentifier, "Source not found"));
117     }
118
119     @Override
120     protected synchronized void offer(final T source) {
121         LOG.trace("Source {} offered to cache", source.sourceId());
122         final File file = sourceIdToFile(source);
123         if (file.exists()) {
124             LOG.debug("Source {} already in cache as {}", source.sourceId(), file);
125             return;
126         }
127
128         storeSource(file, source);
129         register(source.sourceId());
130         LOG.trace("Source {} stored in cache as {}", source.sourceId(), file);
131     }
132
133     private File sourceIdToFile(final T source) {
134         return sourceIdToFile(source.sourceId(), storageDirectory);
135     }
136
137     static File sourceIdToFile(final SourceIdentifier identifier, final File storageDirectory) {
138         final Revision rev = identifier.revision();
139         final File file;
140         if (rev == null) {
141             // FIXME: this does not look right
142             file = findFileWithNewestRev(identifier, storageDirectory);
143         } else {
144             file = new File(storageDirectory, identifier.toYangFilename());
145         }
146         return file;
147     }
148
149     private static File findFileWithNewestRev(final SourceIdentifier identifier, final File storageDirectory) {
150         File[] files = storageDirectory.listFiles(new FilenameFilter() {
151             final Pattern pat = Pattern.compile(Pattern.quote(identifier.name().getLocalName())
152                     + "(\\.yang|@\\d\\d\\d\\d-\\d\\d-\\d\\d.yang)");
153
154             @Override
155             public boolean accept(final File dir, final String name) {
156                 return pat.matcher(name).matches();
157             }
158         });
159
160         if (files.length == 0) {
161             return new File(storageDirectory, identifier.toYangFilename());
162         }
163         if (files.length == 1) {
164             return files[0];
165         }
166
167         File file = null;
168         TreeMap<Optional<Revision>, File> map = new TreeMap<>(Revision::compare);
169         for (File sorted : files) {
170             String fileName = sorted.getName();
171             Matcher match = Revision.STRING_FORMAT_PATTERN.matcher(fileName);
172             if (match.find()) {
173                 String revStr = match.group();
174                 Revision rev;
175                 try {
176                     rev = Revision.of(revStr);
177                 } catch (final DateTimeParseException e) {
178                     LOG.info("Unable to parse date from yang file name {}, falling back to not-present", fileName, e);
179                     rev = null;
180                 }
181
182                 map.put(Optional.ofNullable(rev), sorted);
183
184             } else {
185                 map.put(Optional.empty(), sorted);
186             }
187         }
188         file = map.lastEntry().getValue();
189
190         return file;
191     }
192
193     private void storeSource(final File file, final T schemaRepresentation) {
194         STORAGE_ADAPTERS.get(representation).store(file, schemaRepresentation);
195     }
196
197     private abstract static class StorageAdapter<T extends SourceRepresentation> {
198         private final Class<T> supportedType;
199
200         protected StorageAdapter(final Class<T> supportedType) {
201             this.supportedType = supportedType;
202         }
203
204         void store(final File file, final SourceRepresentation schemaSourceRepresentation) {
205             checkArgument(supportedType.isAssignableFrom(schemaSourceRepresentation.getClass()),
206                     "Cannot store schema source %s, this adapter only supports %s", schemaSourceRepresentation,
207                     supportedType);
208
209             storeAsType(file, supportedType.cast(schemaSourceRepresentation));
210         }
211
212         // FIXME: use java.nio.filePath
213         protected abstract void storeAsType(File file, T cast);
214
215         T restore(final SourceIdentifier sourceIdentifier, final File cachedSource) {
216             checkArgument(cachedSource.isFile());
217             checkArgument(cachedSource.exists());
218             checkArgument(cachedSource.canRead());
219             return restoreAsType(sourceIdentifier, cachedSource);
220         }
221
222         abstract T restoreAsType(SourceIdentifier sourceIdentifier, File cachedSource);
223     }
224
225     private static final class YangTextStorageAdapter extends StorageAdapter<YangTextSource> {
226         protected YangTextStorageAdapter() {
227             super(YangTextSource.class);
228         }
229
230         @Override
231         protected void storeAsType(final File file, final YangTextSource cast) {
232             try (var castStream = cast.asByteSource(StandardCharsets.UTF_8).openStream()) {
233                 Files.copy(castStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
234             } catch (final IOException e) {
235                 throw new IllegalStateException("Cannot store schema source " + cast.sourceId() + " to " + file, e);
236             }
237         }
238
239         @Override
240         YangTextSource restoreAsType(final SourceIdentifier sourceIdentifier, final File cachedSource) {
241             return YangTextSource.forPath(cachedSource.toPath(), sourceIdentifier);
242         }
243     }
244
245     private static final class CachedModulesFileVisitor extends SimpleFileVisitor<Path> {
246         private final List<SourceIdentifier> cachedSchemas = new ArrayList<>();
247
248         @Override
249         public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
250             final FileVisitResult fileVisitResult = super.visitFile(file, attrs);
251             String fileName = file.toFile().getName();
252             fileName = com.google.common.io.Files.getNameWithoutExtension(fileName);
253
254             final Optional<SourceIdentifier> si = getSourceIdentifier(fileName);
255             if (si.isPresent()) {
256                 LOG.trace("Restoring cached file {} as {}", file, si.orElseThrow());
257                 cachedSchemas.add(si.orElseThrow());
258             } else {
259                 LOG.debug("Skipping cached file {}, cannot restore source identifier from filename: {},"
260                         + " does not match {}", file, fileName, CACHED_FILE_PATTERN);
261             }
262             return fileVisitResult;
263         }
264
265         private static Optional<SourceIdentifier> getSourceIdentifier(final String fileName) {
266             final Matcher matcher = CACHED_FILE_PATTERN.matcher(fileName);
267             if (matcher.matches()) {
268                 final String moduleName = matcher.group("moduleName");
269                 final String revision = matcher.group("revision");
270                 return Optional.of(new SourceIdentifier(moduleName, revision));
271             }
272             return Optional.empty();
273         }
274
275         @Override
276         public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException {
277             LOG.warn("Unable to restore cached file {}. Ignoring", file, exc);
278             return FileVisitResult.CONTINUE;
279         }
280
281         public List<SourceIdentifier> getCachedSchemas() {
282             return cachedSchemas;
283         }
284     }
285 }