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.netconf.test.tool.schemacache;
10 import com.google.common.base.MoreObjects.ToStringHelper;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.io.InputStream;
15 import java.util.HashMap;
17 import java.util.Optional;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21 import org.opendaylight.netconf.test.tool.TestToolUtils;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
24 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
26 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
27 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
28 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
29 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
30 import org.opendaylight.yangtools.yang.model.repo.util.AbstractSchemaSourceCache;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Cache implementation that stores schemas in form of files under provided folder.
37 public final class SchemaSourceCache<T extends SchemaSourceRepresentation>
38 extends AbstractSchemaSourceCache<T> {
40 private static final Logger LOG = LoggerFactory.getLogger(SchemaSourceCache.class);
42 public static final Pattern CACHED_FILE_PATTERN = Pattern.compile(
43 ".*/(?<moduleName>[^@]+)" + "(@(?<revision>" + Revision.STRING_FORMAT_PATTERN + "))?.yang");
45 private final Class<T> representation;
46 private final Set<String> modelList;
47 private Map<String, ModelData> cachedSchemas;
49 public SchemaSourceCache(
50 final SchemaSourceRegistry consumer, final Class<T> representation, final Set<String> modelList) {
51 super(consumer, representation, Costs.LOCAL_IO);
52 this.representation = representation;
53 this.modelList = Preconditions.checkNotNull(modelList);
58 * Restore cache state.
61 cachedSchemas = new HashMap<>();
62 for (String modelPath: modelList) {
63 Optional<SourceIdentifier> sourceIdentifierOptional = getSourceIdentifier(modelPath);
64 if (sourceIdentifierOptional.isPresent()) {
65 SourceIdentifier sourceIdentifier = sourceIdentifierOptional.get();
66 cachedSchemas.put(sourceIdentifier.toYangFilename(), new ModelData(sourceIdentifier, modelPath));
68 LOG.debug("Skipping caching model {}, cannot restore source identifier from model path,"
69 + " does not match {}", modelPath, CACHED_FILE_PATTERN);
72 for (final ModelData cachedSchema : cachedSchemas.values()) {
73 register(cachedSchema.getId());
78 public synchronized ListenableFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
79 ModelData modelData = cachedSchemas.get(sourceIdentifier.toYangFilename());
80 if (modelData != null) {
81 final SchemaSourceRepresentation restored = restoreAsType(modelData.getId(), modelData.getPath());
82 return Futures.immediateFuture(representation.cast(restored));
85 LOG.debug("Source {} not found in cache as {}", sourceIdentifier);
86 return Futures.immediateFailedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
90 protected synchronized void offer(final T source) {
91 LOG.trace("Source {} offered to cache", source.getIdentifier());
94 private static YangTextSchemaSource restoreAsType(final SourceIdentifier sourceIdentifier,
95 final String cachedSource) {
96 return new YangTextSchemaSource(sourceIdentifier) {
99 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
100 return toStringHelper;
104 public InputStream openStream() {
105 return TestToolUtils.getDataAsStream(cachedSource);
110 private static Optional<SourceIdentifier> getSourceIdentifier(final String fileName) {
111 final Matcher matcher = CACHED_FILE_PATTERN.matcher(fileName);
112 if (matcher.matches()) {
113 final String moduleName = matcher.group("moduleName");
114 final Optional<Revision> revision = Revision.ofNullable(matcher.group("revision"));
115 return Optional.of(RevisionSourceIdentifier.create(moduleName, revision));
117 return Optional.empty();