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.util;
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Strings;
14 import com.google.common.collect.Lists;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
18 import java.io.FileInputStream;
19 import java.io.FilenameFilter;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.nio.file.FileVisitResult;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.SimpleFileVisitor;
26 import java.nio.file.StandardCopyOption;
27 import java.nio.file.attribute.BasicFileAttributes;
28 import java.text.DateFormat;
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.Collections;
32 import java.util.Date;
33 import java.util.List;
35 import java.util.TreeMap;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
39 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
40 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
41 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
42 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
43 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
44 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Cache implementation that stores schemas in form of files under provided folder
51 public final class FilesystemSchemaSourceCache<T extends SchemaSourceRepresentation> extends AbstractSchemaSourceCache<T> {
53 private static final Logger LOG = LoggerFactory.getLogger(FilesystemSchemaSourceCache.class);
55 // Init storage adapters
56 private static final Map<Class<? extends SchemaSourceRepresentation>, StorageAdapter<? extends SchemaSourceRepresentation>> STORAGE_ADAPTERS =
57 Collections.singletonMap(
58 YangTextSchemaSource.class, new YangTextSchemaStorageAdapter());
60 private static final Pattern CACHED_FILE_PATTERN =
62 "(?<moduleName>[^@]+)" +
63 "(@(?<revision>" + SourceIdentifier.REVISION_PATTERN + "))?");
65 private final Class<T> representation;
66 private final File storageDirectory;
68 public FilesystemSchemaSourceCache(
69 final SchemaSourceRegistry consumer, final Class<T> representation, final File storageDirectory) {
70 super(consumer, representation, Costs.LOCAL_IO);
71 this.representation = representation;
72 this.storageDirectory = Preconditions.checkNotNull(storageDirectory);
74 checkSupportedRepresentation(representation);
76 if(!storageDirectory.exists()) {
77 Preconditions.checkArgument(storageDirectory.mkdirs(), "Unable to create cache directory at %s", storageDirectory);
79 Preconditions.checkArgument(storageDirectory.exists());
80 Preconditions.checkArgument(storageDirectory.isDirectory());
81 Preconditions.checkArgument(storageDirectory.canWrite());
82 Preconditions.checkArgument(storageDirectory.canRead());
87 private static void checkSupportedRepresentation(final Class<? extends SchemaSourceRepresentation> representation) {
88 for (final Class<? extends SchemaSourceRepresentation> supportedRepresentation : STORAGE_ADAPTERS.keySet()) {
89 if(supportedRepresentation.isAssignableFrom(representation)) {
94 throw new IllegalArgumentException(String.format(
95 "This cache does not support representation: %s, supported representations are: %s", representation, STORAGE_ADAPTERS.keySet()));
101 private void init() {
103 final CachedModulesFileVisitor fileVisitor = new CachedModulesFileVisitor();
105 Files.walkFileTree(storageDirectory.toPath(), fileVisitor);
106 } catch (final IOException e) {
107 LOG.warn("Unable to restore cache from {}. Starting with empty cache", storageDirectory);
111 for (final SourceIdentifier cachedSchema : fileVisitor.getCachedSchemas()) {
112 register(cachedSchema);
117 public synchronized CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
118 final File file = sourceIdToFile(sourceIdentifier, storageDirectory);
119 if(file.exists() && file.canRead()) {
120 LOG.trace("Source {} found in cache as {}", sourceIdentifier, file);
121 final SchemaSourceRepresentation restored = STORAGE_ADAPTERS.get(representation).restore(sourceIdentifier, file);
122 return Futures.immediateCheckedFuture(representation.cast(restored));
125 LOG.debug("Source {} not found in cache as {}", sourceIdentifier, file);
126 return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
130 protected synchronized void offer(final T source) {
131 LOG.trace("Source {} offered to cache", source.getIdentifier());
132 final File file = sourceIdToFile(source);
134 LOG.debug("Source {} already in cache as {}", source.getIdentifier(), file);
138 storeSource(file, source);
139 register(source.getIdentifier());
140 LOG.trace("Source {} stored in cache as {}", source.getIdentifier(), file);
143 private File sourceIdToFile(final T source) {
144 return sourceIdToFile(source.getIdentifier(), storageDirectory);
147 static File sourceIdToFile(final SourceIdentifier identifier, final File storageDirectory) {
148 final String rev = identifier.getRevision();
150 if (Strings.isNullOrEmpty(rev)) {
151 file = findFileWithNewestRev(identifier, storageDirectory);
153 file = new File(storageDirectory, identifier.toYangFilename());
158 private static File findFileWithNewestRev(final SourceIdentifier identifier, final File storageDirectory) {
159 File[] files = storageDirectory.listFiles(new FilenameFilter() {
160 final Pattern p = Pattern.compile(Pattern.quote(identifier.getName()) + "(\\.yang|@\\d\\d\\d\\d-\\d\\d-\\d\\d.yang)");
163 public boolean accept(final File dir, final String name) {
164 return p.matcher(name).matches();
168 if (files.length == 0) {
169 return new File(storageDirectory, identifier.toYangFilename());
171 if (files.length == 1) {
176 TreeMap<Date, File> map = new TreeMap<>();
177 for (File sorted : files) {
178 String fileName = sorted.getName();
179 Matcher m = SourceIdentifier.REVISION_PATTERN.matcher(fileName);
181 String revStr = m.group();
183 * FIXME: Consider using string for comparison.
184 * String is comparable, pattern check tested format
185 * so comparing as ASCII string should be sufficient
187 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
189 Date d = df.parse(revStr);
191 } catch (final ParseException e) {
192 LOG.info("Unable to parse date from yang file name {}", fileName);
193 map.put(new Date(0L), sorted);
197 map.put(new Date(0L), sorted);
200 file = map.lastEntry().getValue();
205 private void storeSource(final File file, final T schemaRepresentation) {
206 STORAGE_ADAPTERS.get(representation).store(file, schemaRepresentation);
209 private static abstract class StorageAdapter<T extends SchemaSourceRepresentation> {
211 private final Class<T> supportedType;
213 protected StorageAdapter(final Class<T> supportedType) {
214 this.supportedType = supportedType;
217 void store(final File file, final SchemaSourceRepresentation schemaSourceRepresentation) {
218 Preconditions.checkArgument(supportedType.isAssignableFrom(schemaSourceRepresentation.getClass()),
219 "Cannot store schema source %s, this adapter only supports %s", schemaSourceRepresentation, supportedType);
221 storeAsType(file, supportedType.cast(schemaSourceRepresentation));
225 protected abstract void storeAsType(final File file, final T cast);
227 public T restore(final SourceIdentifier sourceIdentifier, final File cachedSource) {
228 Preconditions.checkArgument(cachedSource.isFile());
229 Preconditions.checkArgument(cachedSource.exists());
230 Preconditions.checkArgument(cachedSource.canRead());
231 return restoreAsType(sourceIdentifier, cachedSource);
234 protected abstract T restoreAsType(final SourceIdentifier sourceIdentifier, final File cachedSource);
237 private static final class YangTextSchemaStorageAdapter extends StorageAdapter<YangTextSchemaSource> {
239 protected YangTextSchemaStorageAdapter() {
240 super(YangTextSchemaSource.class);
244 protected void storeAsType(final File file, final YangTextSchemaSource cast) {
245 try (final InputStream castStream = cast.openStream()) {
246 Files.copy(castStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
247 } catch (final IOException e) {
248 throw new IllegalStateException("Cannot store schema source " + cast.getIdentifier() + " to " + file, e);
253 public YangTextSchemaSource restoreAsType(final SourceIdentifier sourceIdentifier, final File cachedSource) {
254 return new YangTextSchemaSource(sourceIdentifier) {
257 protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
258 return toStringHelper;
262 public InputStream openStream() throws IOException {
263 return new FileInputStream(cachedSource);
269 private static final class CachedModulesFileVisitor extends SimpleFileVisitor<Path> {
270 private final List<SourceIdentifier> cachedSchemas = Lists.newArrayList();
273 public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
274 final FileVisitResult fileVisitResult = super.visitFile(file, attrs);
275 String fileName = file.toFile().getName();
276 fileName = com.google.common.io.Files.getNameWithoutExtension(fileName);
278 final Optional<SourceIdentifier> si = getSourceIdentifier(fileName);
280 LOG.trace("Restoring cached file {} as {}", file, si.get());
281 cachedSchemas.add(si.get());
283 LOG.debug("Skipping cached file {}, cannot restore source identifier from filename: {}, does not match {}", file, fileName, CACHED_FILE_PATTERN);
285 return fileVisitResult;
288 private static Optional<SourceIdentifier> getSourceIdentifier(final String fileName) {
289 final Matcher matcher = CACHED_FILE_PATTERN.matcher(fileName);
290 if (matcher.matches()) {
291 final String moduleName = matcher.group("moduleName");
292 final String revision = matcher.group("revision");
293 return Optional.of(new SourceIdentifier(moduleName, Optional.fromNullable(revision)));
295 return Optional.absent();
299 public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException {
300 LOG.warn("Unable to restore cached file {}. Ignoring", file, exc);
301 return FileVisitResult.CONTINUE;
304 public List<SourceIdentifier> getCachedSchemas() {
305 return cachedSchemas;