2 * Copyright (c) 2015 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.parser.repo;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.base.Optional;
15 import com.google.common.base.Preconditions;
16 import com.google.common.collect.ArrayListMultimap;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.Multimap;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.Futures;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import java.util.Collection;
26 import java.util.concurrent.ConcurrentLinkedDeque;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicReference;
29 import javax.annotation.Nonnull;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
32 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
33 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
34 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
35 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
36 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
37 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
38 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
39 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
40 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
41 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
42 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
43 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
44 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
45 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
46 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
47 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
48 import org.opendaylight.yangtools.yang.model.repo.util.InMemorySchemaSourceCache;
49 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
50 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public final class YangTextSchemaContextResolver implements AutoCloseable, SchemaSourceProvider<YangTextSchemaSource> {
55 private static final Logger LOG = LoggerFactory.getLogger(YangTextSchemaContextResolver.class);
56 private static final long SOURCE_LIFETIME_SECONDS = 60;
58 private final Collection<SourceIdentifier> requiredSources = new ConcurrentLinkedDeque<>();
59 private final Multimap<SourceIdentifier, YangTextSchemaSource> texts = ArrayListMultimap.create();
60 private final AtomicReference<Optional<SchemaContext>> currentSchemaContext =
61 new AtomicReference<>(Optional.absent());
62 private final InMemorySchemaSourceCache<ASTSchemaSource> cache;
63 private final SchemaListenerRegistration transReg;
64 private final SchemaSourceRegistry registry;
65 private final SchemaRepository repository;
66 private volatile Object version = new Object();
67 private volatile Object contextVersion = version;
69 private YangTextSchemaContextResolver(final SchemaRepository repository, final SchemaSourceRegistry registry) {
70 this.repository = Preconditions.checkNotNull(repository);
71 this.registry = Preconditions.checkNotNull(registry);
73 final TextToASTTransformer t = TextToASTTransformer.create(repository, registry);
74 transReg = registry.registerSchemaSourceListener(t);
76 cache = InMemorySchemaSourceCache.createSoftCache(registry, ASTSchemaSource.class, SOURCE_LIFETIME_SECONDS,
80 public static YangTextSchemaContextResolver create(final String name) {
81 final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name);
82 return new YangTextSchemaContextResolver(sharedRepo, sharedRepo);
86 * Register a {@link YangTextSchemaSource}.
88 * @param source YANG text source
89 * @throws YangSyntaxErrorException When the YANG file is syntactically invalid
90 * @throws IOException when the URL is not readable
91 * @throws SchemaSourceException When parsing encounters general error
92 * @return a YangTextSchemaSourceRegistration
94 public YangTextSchemaSourceRegistration registerSource(@Nonnull final YangTextSchemaSource source)
95 throws SchemaSourceException, IOException, YangSyntaxErrorException {
96 checkArgument(source != null);
98 final ASTSchemaSource ast = TextToASTTransformer.transformText(source);
99 LOG.trace("Resolved source {} to source {}", source, ast);
101 // AST carries an accurate identifier, check if it matches the one supplied by the source. If it
102 // does not, check how much it differs and emit a warning.
103 final SourceIdentifier providedId = source.getIdentifier();
104 final SourceIdentifier parsedId = ast.getIdentifier();
105 final YangTextSchemaSource text;
106 if (!parsedId.equals(providedId)) {
107 if (!parsedId.getName().equals(providedId.getName())) {
108 LOG.info("Provided module name {} does not match actual text {}, corrected",
109 providedId.toYangFilename(), parsedId.toYangFilename());
111 final String sourceRev = providedId.getRevision();
112 final String astRev = parsedId.getRevision();
113 if (sourceRev != null && !SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION.equals(sourceRev)) {
114 if (!sourceRev.equals(astRev)) {
115 LOG.info("Provided module revision {} does not match actual text {}, corrected",
116 providedId.toYangFilename(), parsedId.toYangFilename());
119 LOG.debug("Expanded module {} to {}", providedId.toYangFilename(), parsedId.toYangFilename());
123 text = YangTextSchemaSource.delegateForByteSource(parsedId, source);
128 synchronized (this) {
129 texts.put(parsedId, text);
130 LOG.debug("Populated {} with text", parsedId);
132 final SchemaSourceRegistration<YangTextSchemaSource> reg = registry.registerSchemaSource(this,
133 PotentialSchemaSource.create(parsedId, YangTextSchemaSource.class, Costs.IMMEDIATE.getValue()));
134 requiredSources.add(parsedId);
135 cache.schemaSourceEncountered(ast);
136 LOG.debug("Added source {} to schema context requirements", parsedId);
137 version = new Object();
139 return new AbstractYangTextSchemaSourceRegistration(text) {
141 protected void removeRegistration() {
142 synchronized (YangTextSchemaContextResolver.this) {
143 requiredSources.remove(parsedId);
144 LOG.trace("Removed source {} from schema context requirements", parsedId);
145 version = new Object();
147 texts.remove(parsedId, text);
155 * Register a URL containing a YANG text.
157 * @param url YANG text source URL
158 * @throws YangSyntaxErrorException When the YANG file is syntactically invalid
159 * @throws IOException when the URL is not readable
160 * @throws SchemaSourceException When parsing encounters general error
161 * @return a YangTextSchemaSourceRegistration for this URL
163 public YangTextSchemaSourceRegistration registerSource(@Nonnull final URL url) throws SchemaSourceException,
164 IOException, YangSyntaxErrorException {
165 checkArgument(url != null, "Supplied URL must not be null");
167 final String path = url.getPath();
168 final String fileName = path.substring(path.lastIndexOf('/') + 1);
169 final SourceIdentifier guessedId = guessSourceIdentifier(fileName);
170 return registerSource(new YangTextSchemaSource(guessedId) {
172 public InputStream openStream() throws IOException {
173 return url.openStream();
177 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
178 return toStringHelper.add("url", url);
183 private static SourceIdentifier guessSourceIdentifier(final String fileName) {
185 return YangTextSchemaSource.identifierFromFilename(fileName);
186 } catch (IllegalArgumentException e) {
187 LOG.warn("Invalid file name format in '{}'", fileName, e);
188 return RevisionSourceIdentifier.create(fileName);
193 * Try to parse all currently available yang files and build new schema context.
194 * @return new schema context iif there is at least 1 yang file registered and
195 * new schema context was successfully built.
197 public Optional<SchemaContext> getSchemaContext() {
198 return getSchemaContext(StatementParserMode.DEFAULT_MODE);
202 * Try to parse all currently available yang files and build new schema context
203 * in dependence on specified parsing mode.
205 * @param statementParserMode mode of statement parser
206 * @return new schema context iif there is at least 1 yang file registered and
207 * new schema context was successfully built.
209 public Optional<SchemaContext> getSchemaContext(final StatementParserMode statementParserMode) {
210 final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
211 Optional<SchemaContext> sc;
214 // Spin get stable context version
218 sc = currentSchemaContext.get();
222 } while (cv != contextVersion);
224 // Version has been updated
225 Collection<SourceIdentifier> sources;
228 sources = ImmutableSet.copyOf(requiredSources);
229 } while (v != version);
232 final CheckedFuture<SchemaContext, SchemaResolutionException> f = factory.createSchemaContext(sources,
233 statementParserMode);
235 sc = Optional.of(f.checkedGet());
237 } catch (SchemaResolutionException e) {
238 LOG.info("Failed to fully assemble schema context for {}", sources, e);
239 sources = e.getResolvedSources();
243 LOG.debug("Resolved schema context for {}", sources);
245 synchronized (this) {
246 if (contextVersion == cv) {
247 currentSchemaContext.set(sc);
251 } while (version == v);
257 public SchemaContext trySchemaContext() throws SchemaResolutionException {
258 return trySchemaContext(StatementParserMode.DEFAULT_MODE);
262 public SchemaContext trySchemaContext(final StatementParserMode statementParserMode)
263 throws SchemaResolutionException {
264 final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
265 return factory.createSchemaContext(ImmutableSet.copyOf(requiredSources), statementParserMode).checkedGet();
269 public synchronized CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(
270 final SourceIdentifier sourceIdentifier) {
271 final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
273 LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
275 return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException(
276 "URL for " + sourceIdentifier + " not registered", sourceIdentifier));
279 return Futures.immediateCheckedFuture(ret.iterator().next());
283 * Return the set of sources currently available in this resolved.
285 * @return An immutable point-in-time view of available sources.
287 public synchronized Set<SourceIdentifier> getAvailableSources() {
288 return ImmutableSet.copyOf(texts.keySet());
292 public void close() {