Add/improve yang parser error reporting
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / YangTextSchemaContextResolver.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.repo;
9
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;
21 import java.net.URL;
22 import java.util.Collection;
23 import java.util.Set;
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;
48
49 public final class YangTextSchemaContextResolver implements AutoCloseable, SchemaSourceProvider<YangTextSchemaSource> {
50     private static final Logger LOG = LoggerFactory.getLogger(YangTextSchemaContextResolver.class);
51
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;
62
63     private YangTextSchemaContextResolver(final SchemaRepository repository, final SchemaSourceRegistry registry) {
64         this.repository = Preconditions.checkNotNull(repository);
65         this.registry = Preconditions.checkNotNull(registry);
66
67         final TextToASTTransformer t = TextToASTTransformer.create(repository, registry);
68         transReg = registry.registerSchemaSourceListener(t);
69
70         cache = InMemorySchemaSourceCache.createSoftCache(registry, ASTSchemaSource.class);
71     }
72
73     public static YangTextSchemaContextResolver create(final String name) {
74         final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name);
75         return new YangTextSchemaContextResolver(sharedRepo, sharedRepo);
76     }
77
78     /**
79      * Register a {@link YangTextSchemaSource}.
80      *
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
86      */
87     public YangTextSchemaSourceRegistration registerSource(@Nonnull final YangTextSchemaSource source)
88             throws SchemaSourceException, IOException, YangSyntaxErrorException {
89         checkArgument(source != null);
90
91         final ASTSchemaSource ast = TextToASTTransformer.TRANSFORMATION.apply(source).checkedGet();
92         LOG.trace("Resolved source {} to source {}", source, ast);
93
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());
103             } else {
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());
110                     }
111                 } else {
112                     LOG.debug("Expanded module {} to {}", providedId.toYangFilename(), parsedId.toYangFilename());
113                 }
114             }
115
116             text = YangTextSchemaSource.delegateForByteSource(parsedId, source);
117         } else {
118             text = source;
119         }
120
121         synchronized (this) {
122             texts.put(parsedId, text);
123             LOG.debug("Populated {} with text", parsedId);
124
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();
131
132             return new AbstractYangTextSchemaSourceRegistration(text) {
133                 @Override
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();
139                         reg.close();
140                         texts.remove(parsedId, text);
141                     }
142                 }
143             };
144         }
145     }
146
147     /**
148      * Register a URL containing a YANG text.
149      *
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
155      */
156     public YangTextSchemaSourceRegistration registerSource(@Nonnull final URL url) throws SchemaSourceException, IOException, YangSyntaxErrorException {
157         checkArgument(url != null, "Supplied URL must not be null");
158
159         final SourceIdentifier guessedId = new SourceIdentifier(url.getFile(), Optional.<String>absent());
160         return registerSource(new YangTextSchemaSource(guessedId) {
161             @Override
162             public InputStream openStream() throws IOException {
163                 return url.openStream();
164             }
165
166             @Override
167             protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
168                 return toStringHelper.add("url", url);
169             }
170         });
171     }
172
173     /**
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.
177      */
178     public Optional<SchemaContext> getSchemaContext() {
179         final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
180         Optional<SchemaContext> sc;
181         Object v;
182         do {
183             // Spin get stable context version
184             Object cv;
185             do {
186                 cv = contextVersion;
187                 sc = currentSchemaContext.get();
188                 if (version == cv) {
189                     return sc;
190                 }
191             } while (cv != contextVersion);
192
193             // Version has been updated
194             Collection<SourceIdentifier> sources;
195             do {
196                 v = version;
197                 sources = ImmutableSet.copyOf(requiredSources);
198             } while (v != version);
199
200             while (true) {
201                 final CheckedFuture<SchemaContext, SchemaResolutionException> f = factory.createSchemaContext(sources);
202                 try {
203                     sc = Optional.of(f.checkedGet());
204                     break;
205                 } catch (SchemaResolutionException e) {
206                     LOG.info("Failed to fully assemble schema context for {}", sources, e);
207                     sources = e.getResolvedSources();
208                 }
209             }
210
211             LOG.debug("Resolved schema context for {}", sources);
212
213             synchronized (this) {
214                 if (contextVersion == cv) {
215                     currentSchemaContext.set(sc);
216                     contextVersion = v;
217                 }
218             }
219         } while (version == v);
220
221         return sc;
222     }
223
224     @Override
225     public synchronized CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
226         final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
227
228         LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
229         if (ret.isEmpty()) {
230             return Futures.<YangTextSchemaSource, SchemaSourceException>immediateFailedCheckedFuture(
231                     new MissingSchemaSourceException("URL for " + sourceIdentifier + " not registered", sourceIdentifier));
232         }
233
234         return Futures.immediateCheckedFuture(ret.iterator().next());
235     }
236
237     /**
238      * Return the set of sources currently available in this resolved.
239      *
240      * @return An immutable point-in-time view of available sources.
241      */
242     public synchronized Set<SourceIdentifier> getAvailableSources() {
243         return ImmutableSet.copyOf(texts.keySet());
244     }
245
246     @Override
247     public void close() {
248         transReg.close();
249     }
250 }