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 com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ArrayListMultimap;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.Multimap;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.Futures;
19 import java.io.IOException;
20 import java.io.InputStream;
22 import java.util.Collection;
24 import java.util.concurrent.ConcurrentLinkedDeque;
25 import java.util.concurrent.atomic.AtomicReference;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
29 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
30 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
32 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
33 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
34 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
35 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
36 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
37 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
38 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
39 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
40 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
41 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
42 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
43 import org.opendaylight.yangtools.yang.model.repo.util.InMemorySchemaSourceCache;
44 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
45 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public final class YangTextSchemaContextResolver implements AutoCloseable, SchemaSourceProvider<YangTextSchemaSource> {
50 private static final Logger LOG = LoggerFactory.getLogger(YangTextSchemaContextResolver.class);
52 private final Collection<SourceIdentifier> requiredSources = new ConcurrentLinkedDeque<>();
53 private final Multimap<SourceIdentifier, YangTextSchemaSource> texts = ArrayListMultimap.create();
54 private final AtomicReference<Optional<SchemaContext>> currentSchemaContext =
55 new AtomicReference<>(Optional.<SchemaContext>absent());
56 private final InMemorySchemaSourceCache<ASTSchemaSource> cache;
57 private final SchemaListenerRegistration transReg;
58 private final SchemaSourceRegistry registry;
59 private final SchemaRepository repository;
60 private volatile Object version = new Object();
61 private volatile Object contextVersion = version;
63 private YangTextSchemaContextResolver(final SchemaRepository repository, final SchemaSourceRegistry registry) {
64 this.repository = Preconditions.checkNotNull(repository);
65 this.registry = Preconditions.checkNotNull(registry);
67 final TextToASTTransformer t = TextToASTTransformer.create(repository, registry);
68 transReg = registry.registerSchemaSourceListener(t);
70 cache = InMemorySchemaSourceCache.createSoftCache(registry, ASTSchemaSource.class);
73 public static YangTextSchemaContextResolver create(final String name) {
74 final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name);
75 return new YangTextSchemaContextResolver(sharedRepo, sharedRepo);
79 * Register a {@link YangTextSchemaSource}.
81 * @param source YANG text source
82 * @throws YangSyntaxErrorException When the YANG file is syntactically invalid
83 * @throws IOException when the URL is not readable
84 * @throws SchemaSourceException When parsing encounters general error
85 * @return a YangTextSchemaSourceRegistration
87 public YangTextSchemaSourceRegistration registerSource(@Nonnull final YangTextSchemaSource source)
88 throws SchemaSourceException, IOException, YangSyntaxErrorException {
89 checkArgument(source != null);
91 final ASTSchemaSource ast = TextToASTTransformer.TRANSFORMATION.apply(source).checkedGet();
92 LOG.trace("Resolved source {} to source {}", source, ast);
94 // AST carries an accurate identifier, check if it matches the one supplied by the source. If it
95 // does not, check how much it differs and emit a warning.
96 final SourceIdentifier providedId = source.getIdentifier();
97 final SourceIdentifier parsedId = ast.getIdentifier();
98 final YangTextSchemaSource text;
99 if (!parsedId.equals(providedId)) {
100 if (!parsedId.getName().equals(providedId.getName())) {
101 LOG.info("Provided module name {} does not match actual text {}, corrected", providedId.toYangFilename(),
102 parsedId.toYangFilename());
104 final String sourceRev = providedId.getRevision();
105 final String astRev = parsedId.getRevision();
106 if (sourceRev != null) {
107 if (!sourceRev.equals(astRev)) {
108 LOG.info("Provided module revision {} does not match actual text {}, corrected",
109 providedId.toYangFilename(), parsedId.toYangFilename());
112 LOG.debug("Expanded module {} to {}", providedId.toYangFilename(), parsedId.toYangFilename());
116 text = YangTextSchemaSource.delegateForByteSource(parsedId, source);
121 synchronized (this) {
122 texts.put(parsedId, text);
123 LOG.debug("Populated {} with text", parsedId);
125 final SchemaSourceRegistration<YangTextSchemaSource> reg = registry.registerSchemaSource(this,
126 PotentialSchemaSource.create(parsedId, YangTextSchemaSource.class, Costs.IMMEDIATE.getValue()));
127 requiredSources.add(parsedId);
128 cache.schemaSourceEncountered(ast);
129 LOG.debug("Added source {} to schema context requirements", parsedId);
130 version = new Object();
132 return new AbstractYangTextSchemaSourceRegistration(text) {
134 protected void removeRegistration() {
135 synchronized (YangTextSchemaContextResolver.this) {
136 requiredSources.remove(parsedId);
137 LOG.trace("Removed source {} from schema context requirements", parsedId);
138 version = new Object();
140 texts.remove(parsedId, text);
148 * Register a URL containing a YANG text.
150 * @param url YANG text source URL
151 * @throws YangSyntaxErrorException When the YANG file is syntactically invalid
152 * @throws IOException when the URL is not readable
153 * @throws SchemaSourceException When parsing encounters general error
154 * @return a YangTextSchemaSourceRegistration for this URL
156 public YangTextSchemaSourceRegistration registerSource(@Nonnull final URL url) throws SchemaSourceException, IOException, YangSyntaxErrorException {
157 checkArgument(url != null, "Supplied URL must not be null");
159 final SourceIdentifier guessedId = new SourceIdentifier(url.getFile(), Optional.<String>absent());
160 return registerSource(new YangTextSchemaSource(guessedId) {
162 public InputStream openStream() throws IOException {
163 return url.openStream();
167 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
168 return toStringHelper.add("url", url);
174 * Try to parse all currently available yang files and build new schema context.
175 * @return new schema context iif there is at least 1 yang file registered and
176 * new schema context was successfully built.
178 public Optional<SchemaContext> getSchemaContext() {
179 final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
180 Optional<SchemaContext> sc;
183 // Spin get stable context version
187 sc = currentSchemaContext.get();
191 } while (cv != contextVersion);
193 // Version has been updated
194 Collection<SourceIdentifier> sources;
197 sources = ImmutableSet.copyOf(requiredSources);
198 } while (v != version);
201 final CheckedFuture<SchemaContext, SchemaResolutionException> f = factory.createSchemaContext(sources);
203 sc = Optional.of(f.checkedGet());
205 } catch (SchemaResolutionException e) {
206 LOG.info("Failed to fully assemble schema context for {}", sources, e);
207 sources = e.getResolvedSources();
211 LOG.debug("Resolved schema context for {}", sources);
213 synchronized (this) {
214 if (contextVersion == cv) {
215 currentSchemaContext.set(sc);
219 } while (version == v);
225 public synchronized CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
226 final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
228 LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
230 return Futures.<YangTextSchemaSource, SchemaSourceException>immediateFailedCheckedFuture(
231 new MissingSchemaSourceException("URL for " + sourceIdentifier + " not registered", sourceIdentifier));
234 return Futures.immediateCheckedFuture(ret.iterator().next());
238 * Return the set of sources currently available in this resolved.
240 * @return An immutable point-in-time view of available sources.
242 public synchronized Set<SourceIdentifier> getAvailableSources() {
243 return ImmutableSet.copyOf(texts.keySet());
247 public void close() {