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.yangtools.yang.parser.repo;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ArrayListMultimap;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.Multimap;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.Futures;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import java.util.Collection;
24 import java.util.concurrent.ConcurrentLinkedDeque;
25 import java.util.concurrent.atomic.AtomicReference;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
28 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
29 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
30 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
32 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
33 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
34 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
35 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
36 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
37 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
38 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
39 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
40 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
41 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
42 import org.opendaylight.yangtools.yang.model.repo.util.InMemorySchemaSourceCache;
43 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
44 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 public class URLSchemaContextResolver implements AutoCloseable, SchemaSourceProvider<YangTextSchemaSource> {
50 private static final Logger LOG = LoggerFactory.getLogger(URLSchemaContextResolver.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;
64 private URLSchemaContextResolver(final SchemaRepository repository, final SchemaSourceRegistry registry) {
65 this.repository = Preconditions.checkNotNull(repository);
66 this.registry = Preconditions.checkNotNull(registry);
68 final TextToASTTransformer t = TextToASTTransformer.create(repository, registry);
69 transReg = registry.registerSchemaSourceListener(t);
71 cache = InMemorySchemaSourceCache.createSoftCache(registry, ASTSchemaSource.class);
74 public static URLSchemaContextResolver create(final String name) {
75 final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name);
76 return new URLSchemaContextResolver(sharedRepo, sharedRepo);
80 * Register a URL hosting a YANG Text file.
83 * @throws YangSyntaxErrorException When the YANG file is syntactically invalid
84 * @throws IOException when the URL is not readable
85 * @throws SchemaSourceException When parsing encounters general error
86 * @return new instance of AbstractURLRegistration if the URL is not null
88 public URLRegistration registerSource(final URL url) throws SchemaSourceException, IOException, YangSyntaxErrorException {
89 checkArgument(url != null, "Supplied URL must not be null");
91 final SourceIdentifier guessedId = new SourceIdentifier(url.getFile(), Optional.<String>absent());
92 final YangTextSchemaSource text = new YangTextSchemaSource(guessedId) {
94 public InputStream openStream() throws IOException {
95 return url.openStream();
99 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
100 return toStringHelper.add("url", url);
104 final ASTSchemaSource ast = TextToASTTransformer.TRANSFORMATION.apply(text).checkedGet();
105 LOG.trace("Resolved URL {} to source {}", url, ast);
107 final SourceIdentifier resolvedId = ast.getIdentifier();
109 synchronized (this) {
110 texts.put(resolvedId, text);
111 LOG.debug("Populated {} with text", resolvedId);
113 final SchemaSourceRegistration<YangTextSchemaSource> reg = registry.registerSchemaSource(this,
114 PotentialSchemaSource.create(resolvedId, YangTextSchemaSource.class, Costs.IMMEDIATE.getValue()));
115 requiredSources.add(resolvedId);
116 cache.schemaSourceEncountered(ast);
117 LOG.debug("Added source {} to schema context requirements", resolvedId);
118 version = new Object();
120 return new AbstractURLRegistration(text) {
122 protected void removeRegistration() {
123 synchronized (URLSchemaContextResolver.this) {
124 requiredSources.remove(resolvedId);
125 LOG.trace("Removed source {} from schema context requirements", resolvedId);
126 version = new Object();
128 texts.remove(resolvedId, text);
136 * Try to parse all currently available yang files and build new schema context.
137 * @return new schema context iif there is at least 1 yang file registered and
138 * new schema context was successfully built.
140 public Optional<SchemaContext> getSchemaContext() {
141 final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
142 Optional<SchemaContext> sc;
145 // Spin get stable context version
149 sc = currentSchemaContext.get();
153 } while (cv != contextVersion);
155 // Version has been updated
156 Collection<SourceIdentifier> sources;
159 sources = ImmutableSet.copyOf(requiredSources);
160 } while (v != version);
163 final CheckedFuture<SchemaContext, SchemaResolutionException> f = factory.createSchemaContext(sources);
165 sc = Optional.of(f.checkedGet());
167 } catch (SchemaResolutionException e) {
168 LOG.debug("Failed to fully assemble schema context for {}", sources, e);
169 sources = e.getResolvedSources();
173 LOG.debug("Resolved schema context for {}", sources);
175 synchronized (this) {
176 if (contextVersion == cv) {
177 currentSchemaContext.set(sc);
181 } while (version == v);
187 public synchronized CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
188 final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
190 LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
192 return Futures.<YangTextSchemaSource, SchemaSourceException>immediateFailedCheckedFuture(
193 new MissingSchemaSourceException("URL for " + sourceIdentifier + " not registered", sourceIdentifier));
196 return Futures.immediateCheckedFuture(ret.iterator().next());
200 public void close() {