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.Preconditions;
15 import com.google.common.base.Verify;
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.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import java.util.Collection;
25 import java.util.Optional;
27 import java.util.concurrent.ConcurrentLinkedDeque;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.atomic.AtomicReference;
31 import javax.annotation.Nonnull;
32 import org.opendaylight.yangtools.yang.common.Revision;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
35 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
36 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
38 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
39 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
40 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
41 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
42 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
43 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
44 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
45 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
46 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
47 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
48 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
49 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
50 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
51 import org.opendaylight.yangtools.yang.model.repo.util.InMemorySchemaSourceCache;
52 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
53 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public final class YangTextSchemaContextResolver implements AutoCloseable, SchemaSourceProvider<YangTextSchemaSource> {
58 private static final Logger LOG = LoggerFactory.getLogger(YangTextSchemaContextResolver.class);
59 private static final long SOURCE_LIFETIME_SECONDS = 60;
61 private final Collection<SourceIdentifier> requiredSources = new ConcurrentLinkedDeque<>();
62 private final Multimap<SourceIdentifier, YangTextSchemaSource> texts = ArrayListMultimap.create();
63 private final AtomicReference<Optional<SchemaContext>> currentSchemaContext =
64 new AtomicReference<>(Optional.empty());
65 private final InMemorySchemaSourceCache<ASTSchemaSource> cache;
66 private final SchemaListenerRegistration transReg;
67 private final SchemaSourceRegistry registry;
68 private final SchemaRepository repository;
69 private volatile Object version = new Object();
70 private volatile Object contextVersion = version;
72 private YangTextSchemaContextResolver(final SchemaRepository repository, final SchemaSourceRegistry registry) {
73 this.repository = Preconditions.checkNotNull(repository);
74 this.registry = Preconditions.checkNotNull(registry);
76 final TextToASTTransformer t = TextToASTTransformer.create(repository, registry);
77 transReg = registry.registerSchemaSourceListener(t);
79 cache = InMemorySchemaSourceCache.createSoftCache(registry, ASTSchemaSource.class, SOURCE_LIFETIME_SECONDS,
83 public static YangTextSchemaContextResolver create(final String name) {
84 final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name);
85 return new YangTextSchemaContextResolver(sharedRepo, sharedRepo);
89 * Register a {@link YangTextSchemaSource}.
91 * @param source YANG text source
92 * @return a YangTextSchemaSourceRegistration
93 * @throws YangSyntaxErrorException When the YANG file is syntactically invalid
94 * @throws IOException when the URL is not readable
95 * @throws SchemaSourceException When parsing encounters general error
97 public YangTextSchemaSourceRegistration registerSource(@Nonnull final YangTextSchemaSource source)
98 throws SchemaSourceException, IOException, YangSyntaxErrorException {
99 checkArgument(source != null);
101 final ASTSchemaSource ast = TextToASTTransformer.transformText(source);
102 LOG.trace("Resolved source {} to source {}", source, ast);
104 // AST carries an accurate identifier, check if it matches the one supplied by the source. If it
105 // does not, check how much it differs and emit a warning.
106 final SourceIdentifier providedId = source.getIdentifier();
107 final SourceIdentifier parsedId = ast.getIdentifier();
108 final YangTextSchemaSource text;
109 if (!parsedId.equals(providedId)) {
110 if (!parsedId.getName().equals(providedId.getName())) {
111 LOG.info("Provided module name {} does not match actual text {}, corrected",
112 providedId.toYangFilename(), parsedId.toYangFilename());
114 final Optional<Revision> sourceRev = providedId.getRevision();
115 final Optional<Revision> astRev = parsedId.getRevision();
116 if (sourceRev.isPresent()) {
117 if (!sourceRev.equals(astRev)) {
118 LOG.info("Provided module revision {} does not match actual text {}, corrected",
119 providedId.toYangFilename(), parsedId.toYangFilename());
122 LOG.debug("Expanded module {} to {}", providedId.toYangFilename(), parsedId.toYangFilename());
126 text = YangTextSchemaSource.delegateForByteSource(parsedId, source);
131 synchronized (this) {
132 texts.put(parsedId, text);
133 LOG.debug("Populated {} with text", parsedId);
135 final SchemaSourceRegistration<YangTextSchemaSource> reg = registry.registerSchemaSource(this,
136 PotentialSchemaSource.create(parsedId, YangTextSchemaSource.class, Costs.IMMEDIATE.getValue()));
137 requiredSources.add(parsedId);
138 cache.schemaSourceEncountered(ast);
139 LOG.debug("Added source {} to schema context requirements", parsedId);
140 version = new Object();
142 return new AbstractYangTextSchemaSourceRegistration(text) {
144 protected void removeRegistration() {
145 synchronized (YangTextSchemaContextResolver.this) {
146 requiredSources.remove(parsedId);
147 LOG.trace("Removed source {} from schema context requirements", parsedId);
148 version = new Object();
150 texts.remove(parsedId, text);
158 * Register a URL containing a YANG text.
160 * @param url YANG text source URL
161 * @return a YangTextSchemaSourceRegistration for this URL
162 * @throws YangSyntaxErrorException When the YANG file is syntactically invalid
163 * @throws IOException when the URL is not readable
164 * @throws SchemaSourceException When parsing encounters general error
166 public YangTextSchemaSourceRegistration registerSource(@Nonnull final URL url) throws SchemaSourceException,
167 IOException, YangSyntaxErrorException {
168 checkArgument(url != null, "Supplied URL must not be null");
170 final String path = url.getPath();
171 final String fileName = path.substring(path.lastIndexOf('/') + 1);
172 final SourceIdentifier guessedId = guessSourceIdentifier(fileName);
173 return registerSource(new YangTextSchemaSource(guessedId) {
175 public InputStream openStream() throws IOException {
176 return url.openStream();
180 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
181 return toStringHelper.add("url", url);
186 private static SourceIdentifier guessSourceIdentifier(final String fileName) {
188 return YangTextSchemaSource.identifierFromFilename(fileName);
189 } catch (IllegalArgumentException e) {
190 LOG.warn("Invalid file name format in '{}'", fileName, e);
191 return RevisionSourceIdentifier.create(fileName);
196 * Try to parse all currently available yang files and build new schema context.
198 * @return new schema context iif there is at least 1 yang file registered and
199 * new schema context was successfully built.
201 public Optional<SchemaContext> getSchemaContext() {
202 return getSchemaContext(StatementParserMode.DEFAULT_MODE);
206 * Try to parse all currently available yang files and build new schema context depending on specified parsing mode.
208 * @param statementParserMode mode of statement parser
209 * @return new schema context iif there is at least 1 yang file registered and
210 * new schema context was successfully built.
212 public Optional<SchemaContext> getSchemaContext(final StatementParserMode statementParserMode) {
213 final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
214 Optional<SchemaContext> sc;
217 // Spin get stable context version
221 sc = currentSchemaContext.get();
225 } while (cv != contextVersion);
227 // Version has been updated
228 Collection<SourceIdentifier> sources;
231 sources = ImmutableSet.copyOf(requiredSources);
232 } while (ver != version);
235 final ListenableFuture<SchemaContext> f = factory.createSchemaContext(sources, statementParserMode);
237 sc = Optional.of(f.get());
239 } catch (InterruptedException e) {
240 throw new RuntimeException("Interrupted while assembling schema context", e);
241 } catch (ExecutionException e) {
242 LOG.info("Failed to fully assemble schema context for {}", sources, e);
243 final Throwable cause = e.getCause();
244 Verify.verify(cause instanceof SchemaResolutionException);
245 sources = ((SchemaResolutionException) cause).getResolvedSources();
249 LOG.debug("Resolved schema context for {}", sources);
251 synchronized (this) {
252 if (contextVersion == cv) {
253 currentSchemaContext.set(sc);
254 contextVersion = ver;
257 } while (version == ver);
263 public synchronized ListenableFuture<YangTextSchemaSource> getSource(
264 final SourceIdentifier sourceIdentifier) {
265 final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
267 LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
269 return Futures.immediateFailedFuture(new MissingSchemaSourceException(
270 "URL for " + sourceIdentifier + " not registered", sourceIdentifier));
273 return Futures.immediateFuture(ret.iterator().next());
277 * Return the set of sources currently available in this resolved.
279 * @return An immutable point-in-time view of available sources.
281 public synchronized Set<SourceIdentifier> getAvailableSources() {
282 return ImmutableSet.copyOf(texts.keySet());
286 public synchronized Collection<YangTextSchemaSource> getSourceTexts(final SourceIdentifier sourceIdentifier) {
287 return ImmutableSet.copyOf(texts.get(sourceIdentifier));
291 public SchemaContext trySchemaContext() throws SchemaResolutionException {
292 return trySchemaContext(StatementParserMode.DEFAULT_MODE);
296 @SuppressWarnings("checkstyle:avoidHidingCauseException")
297 public SchemaContext trySchemaContext(final StatementParserMode statementParserMode)
298 throws SchemaResolutionException {
299 final ListenableFuture<SchemaContext> future = repository
300 .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT)
301 .createSchemaContext(ImmutableSet.copyOf(requiredSources), statementParserMode);
305 } catch (InterruptedException e) {
306 throw new RuntimeException("Interrupted while waiting for SchemaContext assembly", e);
307 } catch (ExecutionException e) {
308 final Throwable cause = e.getCause();
309 if (cause instanceof SchemaResolutionException) {
310 throw (SchemaResolutionException) cause;
313 throw new SchemaResolutionException("Failed to assemble SchemaContext", e);
318 public void close() {