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;
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;
15 import com.google.common.annotations.Beta;
16 import com.google.common.base.Verify;
17 import com.google.common.collect.ArrayListMultimap;
18 import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Multimap;
20 import com.google.common.util.concurrent.FluentFuture;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import java.io.IOException;
24 import java.time.Duration;
25 import java.util.Collection;
26 import java.util.Optional;
28 import java.util.concurrent.ConcurrentLinkedDeque;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.atomic.AtomicReference;
31 import org.eclipse.jdt.annotation.NonNull;
32 import org.opendaylight.yangtools.yang.common.Revision;
33 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
34 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
35 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
36 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
37 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
38 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
39 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
40 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
41 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
42 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
43 import org.opendaylight.yangtools.yang.model.repo.spi.GuavaSchemaSourceCache;
44 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
45 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
46 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
47 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
48 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
49 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
50 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
51 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
52 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
53 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
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 Duration SOURCE_LIFETIME = Duration.ofSeconds(60);
61 private final Collection<SourceIdentifier> requiredSources = new ConcurrentLinkedDeque<>();
62 private final Multimap<SourceIdentifier, YangTextSchemaSource> texts = ArrayListMultimap.create();
63 private final AtomicReference<Optional<EffectiveModelContext>> currentSchemaContext =
64 new AtomicReference<>(Optional.empty());
65 private final GuavaSchemaSourceCache<IRSchemaSource> 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 = requireNonNull(repository);
74 this.registry = requireNonNull(registry);
76 final TextToIRTransformer t = TextToIRTransformer.create(repository, registry);
77 transReg = registry.registerSchemaSourceListener(t);
79 cache = GuavaSchemaSourceCache.createSoftCache(registry, IRSchemaSource.class, SOURCE_LIFETIME);
82 public static @NonNull YangTextSchemaContextResolver create(final String name) {
83 final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name);
84 return new YangTextSchemaContextResolver(sharedRepo, sharedRepo);
87 public static @NonNull YangTextSchemaContextResolver create(final String name, final YangParserFactory factory) {
88 final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name, factory);
89 return new YangTextSchemaContextResolver(sharedRepo, sharedRepo);
93 * Register a {@link YangTextSchemaSource}.
95 * @param source YANG text source
96 * @return a YangTextSchemaSourceRegistration
97 * @throws YangSyntaxErrorException When the YANG file is syntactically invalid
98 * @throws IOException when the URL is not readable
99 * @throws SchemaSourceException When parsing encounters general error
101 public @NonNull YangTextSchemaSourceRegistration registerSource(final @NonNull YangTextSchemaSource source)
102 throws SchemaSourceException, IOException, YangSyntaxErrorException {
103 checkArgument(source != null);
105 final IRSchemaSource ast = TextToIRTransformer.transformText(source);
106 LOG.trace("Resolved source {} to source {}", source, ast);
108 // AST carries an accurate identifier, check if it matches the one supplied by the source. If it
109 // does not, check how much it differs and emit a warning.
110 final SourceIdentifier providedId = source.getIdentifier();
111 final SourceIdentifier parsedId = ast.getIdentifier();
112 final YangTextSchemaSource text;
113 if (!parsedId.equals(providedId)) {
114 if (!parsedId.name().equals(providedId.name())) {
115 LOG.info("Provided module name {} does not match actual text {}, corrected",
116 providedId.toYangFilename(), parsedId.toYangFilename());
118 final Revision sourceRev = providedId.revision();
119 if (sourceRev != null) {
120 if (!sourceRev.equals(parsedId.revision())) {
121 LOG.info("Provided module revision {} does not match actual text {}, corrected",
122 providedId.toYangFilename(), parsedId.toYangFilename());
125 LOG.debug("Expanded module {} to {}", providedId.toYangFilename(), parsedId.toYangFilename());
129 text = YangTextSchemaSource.delegateForByteSource(parsedId, source);
134 synchronized (this) {
135 texts.put(parsedId, text);
136 LOG.debug("Populated {} with text", parsedId);
138 final SchemaSourceRegistration<YangTextSchemaSource> reg = registry.registerSchemaSource(this,
139 PotentialSchemaSource.create(parsedId, YangTextSchemaSource.class, Costs.IMMEDIATE.getValue()));
140 requiredSources.add(parsedId);
141 cache.schemaSourceEncountered(ast);
142 LOG.debug("Added source {} to schema context requirements", parsedId);
143 version = new Object();
145 return new AbstractYangTextSchemaSourceRegistration(text) {
147 protected void removeRegistration() {
148 synchronized (YangTextSchemaContextResolver.this) {
149 requiredSources.remove(parsedId);
150 LOG.trace("Removed source {} from schema context requirements", parsedId);
151 version = new Object();
153 texts.remove(parsedId, text);
161 * Register a URL containing a YANG text.
163 * @param url YANG text source URL
164 * @return a YangTextSchemaSourceRegistration for this URL
165 * @throws YangSyntaxErrorException When the YANG file is syntactically invalid
166 * @throws IOException when the URL is not readable
167 * @throws SchemaSourceException When parsing encounters general error
169 public @NonNull YangTextSchemaSourceRegistration registerSource(final @NonNull URL url)
170 throws SchemaSourceException, IOException, YangSyntaxErrorException {
171 checkArgument(url != null, "Supplied URL must not be null");
173 final String path = url.getPath();
174 final String fileName = path.substring(path.lastIndexOf('/') + 1);
175 return registerSource(YangTextSchemaSource.forURL(url, guessSourceIdentifier(fileName)));
178 private static SourceIdentifier guessSourceIdentifier(final @NonNull String fileName) {
180 return YangTextSchemaSource.identifierFromFilename(fileName);
181 } catch (final IllegalArgumentException e) {
182 LOG.warn("Invalid file name format in '{}'", fileName, e);
183 return new SourceIdentifier(fileName);
188 * Try to parse all currently available yang files and build new schema context.
190 * @return new schema context iif there is at least 1 yang file registered and
191 * new schema context was successfully built.
193 public Optional<? extends EffectiveModelContext> getEffectiveModelContext() {
194 return getEffectiveModelContext(StatementParserMode.DEFAULT_MODE);
198 * Try to parse all currently available yang files and build new schema context depending on specified parsing mode.
200 * @param statementParserMode mode of statement parser
201 * @return new schema context iif there is at least 1 yang file registered and
202 * new schema context was successfully built.
204 public Optional<? extends EffectiveModelContext> getEffectiveModelContext(
205 final StatementParserMode statementParserMode) {
206 final EffectiveModelContextFactory factory = repository.createEffectiveModelContextFactory(
207 config(statementParserMode));
208 Optional<EffectiveModelContext> sc;
211 // Spin get stable context version
215 sc = currentSchemaContext.get();
219 } while (cv != contextVersion);
221 // Version has been updated
222 Collection<SourceIdentifier> sources;
225 sources = ImmutableSet.copyOf(requiredSources);
226 } while (ver != version);
229 final ListenableFuture<EffectiveModelContext> f = factory.createEffectiveModelContext(sources);
231 sc = Optional.of(f.get());
233 } catch (final InterruptedException e) {
234 throw new IllegalStateException("Interrupted while assembling schema context", e);
235 } catch (final ExecutionException e) {
236 LOG.info("Failed to fully assemble schema context for {}", sources, e);
237 final Throwable cause = e.getCause();
238 Verify.verify(cause instanceof SchemaResolutionException);
239 sources = ((SchemaResolutionException) cause).getResolvedSources();
243 LOG.debug("Resolved schema context for {}", sources);
245 synchronized (this) {
246 if (contextVersion == cv) {
247 currentSchemaContext.set(sc);
248 contextVersion = ver;
251 } while (version == ver);
257 public synchronized @NonNull FluentFuture<YangTextSchemaSource> getSource(
258 final SourceIdentifier sourceIdentifier) {
259 final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
261 LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
263 return immediateFailedFluentFuture(new MissingSchemaSourceException("URL for " + sourceIdentifier
264 + " not registered", sourceIdentifier));
267 return immediateFluentFuture(ret.iterator().next());
271 * Return the set of sources currently available in this resolved.
273 * @return An immutable point-in-time view of available sources.
275 public synchronized Set<SourceIdentifier> getAvailableSources() {
276 return ImmutableSet.copyOf(texts.keySet());
280 public synchronized Collection<YangTextSchemaSource> getSourceTexts(final SourceIdentifier sourceIdentifier) {
281 return ImmutableSet.copyOf(texts.get(sourceIdentifier));
285 public EffectiveModelContext trySchemaContext() throws SchemaResolutionException {
286 return trySchemaContext(StatementParserMode.DEFAULT_MODE);
290 @SuppressWarnings("checkstyle:avoidHidingCauseException")
291 public EffectiveModelContext trySchemaContext(final StatementParserMode statementParserMode)
292 throws SchemaResolutionException {
293 final ListenableFuture<EffectiveModelContext> future = repository
294 .createEffectiveModelContextFactory(config(statementParserMode))
295 .createEffectiveModelContext(ImmutableSet.copyOf(requiredSources));
299 } catch (final InterruptedException e) {
300 throw new IllegalStateException("Interrupted while waiting for SchemaContext assembly", e);
301 } catch (final ExecutionException e) {
302 final Throwable cause = e.getCause();
303 if (cause instanceof SchemaResolutionException) {
304 throw (SchemaResolutionException) cause;
307 throw new SchemaResolutionException("Failed to assemble SchemaContext", e);
312 public void close() {
316 private static SchemaContextFactoryConfiguration config(final StatementParserMode statementParserMode) {
317 return SchemaContextFactoryConfiguration.builder().setStatementParserMode(statementParserMode).build();