Remove RevisionSourceIdentifier
[yangtools.git] / parser / 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 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;
14
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;
23 import java.net.URL;
24 import java.time.Duration;
25 import java.util.Collection;
26 import java.util.Optional;
27 import java.util.Set;
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;
56
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);
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;
71
72     private YangTextSchemaContextResolver(final SchemaRepository repository, final SchemaSourceRegistry registry) {
73         this.repository = requireNonNull(repository);
74         this.registry = requireNonNull(registry);
75
76         final TextToIRTransformer t = TextToIRTransformer.create(repository, registry);
77         transReg = registry.registerSchemaSourceListener(t);
78
79         cache = GuavaSchemaSourceCache.createSoftCache(registry, IRSchemaSource.class, SOURCE_LIFETIME);
80     }
81
82     public static @NonNull YangTextSchemaContextResolver create(final String name) {
83         final SharedSchemaRepository sharedRepo = new SharedSchemaRepository(name);
84         return new YangTextSchemaContextResolver(sharedRepo, sharedRepo);
85     }
86
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);
90     }
91
92     /**
93      * Register a {@link YangTextSchemaSource}.
94      *
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
100      */
101     public @NonNull YangTextSchemaSourceRegistration registerSource(final @NonNull YangTextSchemaSource source)
102             throws SchemaSourceException, IOException, YangSyntaxErrorException {
103         checkArgument(source != null);
104
105         final IRSchemaSource ast = TextToIRTransformer.transformText(source);
106         LOG.trace("Resolved source {} to source {}", source, ast);
107
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());
117             } else {
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());
123                     }
124                 } else {
125                     LOG.debug("Expanded module {} to {}", providedId.toYangFilename(), parsedId.toYangFilename());
126                 }
127             }
128
129             text = YangTextSchemaSource.delegateForByteSource(parsedId, source);
130         } else {
131             text = source;
132         }
133
134         synchronized (this) {
135             texts.put(parsedId, text);
136             LOG.debug("Populated {} with text", parsedId);
137
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();
144
145             return new AbstractYangTextSchemaSourceRegistration(text) {
146                 @Override
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();
152                         reg.close();
153                         texts.remove(parsedId, text);
154                     }
155                 }
156             };
157         }
158     }
159
160     /**
161      * Register a URL containing a YANG text.
162      *
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
168      */
169     public @NonNull YangTextSchemaSourceRegistration registerSource(final @NonNull URL url)
170             throws SchemaSourceException, IOException, YangSyntaxErrorException {
171         checkArgument(url != null, "Supplied URL must not be null");
172
173         final String path = url.getPath();
174         final String fileName = path.substring(path.lastIndexOf('/') + 1);
175         return registerSource(YangTextSchemaSource.forURL(url, guessSourceIdentifier(fileName)));
176     }
177
178     private static SourceIdentifier guessSourceIdentifier(final @NonNull String fileName) {
179         try {
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);
184         }
185     }
186
187     /**
188      * Try to parse all currently available yang files and build new schema context.
189      *
190      * @return new schema context iif there is at least 1 yang file registered and
191      *         new schema context was successfully built.
192      */
193     public Optional<? extends EffectiveModelContext> getEffectiveModelContext() {
194         return getEffectiveModelContext(StatementParserMode.DEFAULT_MODE);
195     }
196
197     /**
198      * Try to parse all currently available yang files and build new schema context depending on specified parsing mode.
199      *
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.
203      */
204     public Optional<? extends EffectiveModelContext> getEffectiveModelContext(
205             final StatementParserMode statementParserMode) {
206         final EffectiveModelContextFactory factory = repository.createEffectiveModelContextFactory(
207             config(statementParserMode));
208         Optional<EffectiveModelContext> sc;
209         Object ver;
210         do {
211             // Spin get stable context version
212             Object cv;
213             do {
214                 cv = contextVersion;
215                 sc = currentSchemaContext.get();
216                 if (version == cv) {
217                     return sc;
218                 }
219             } while (cv != contextVersion);
220
221             // Version has been updated
222             Collection<SourceIdentifier> sources;
223             do {
224                 ver = version;
225                 sources = ImmutableSet.copyOf(requiredSources);
226             } while (ver != version);
227
228             while (true) {
229                 final ListenableFuture<EffectiveModelContext> f = factory.createEffectiveModelContext(sources);
230                 try {
231                     sc = Optional.of(f.get());
232                     break;
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();
240                 }
241             }
242
243             LOG.debug("Resolved schema context for {}", sources);
244
245             synchronized (this) {
246                 if (contextVersion == cv) {
247                     currentSchemaContext.set(sc);
248                     contextVersion = ver;
249                 }
250             }
251         } while (version == ver);
252
253         return sc;
254     }
255
256     @Override
257     public synchronized @NonNull FluentFuture<YangTextSchemaSource> getSource(
258             final SourceIdentifier sourceIdentifier) {
259         final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
260
261         LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
262         if (ret.isEmpty()) {
263             return immediateFailedFluentFuture(new MissingSchemaSourceException("URL for " + sourceIdentifier
264                 + " not registered", sourceIdentifier));
265         }
266
267         return immediateFluentFuture(ret.iterator().next());
268     }
269
270     /**
271      * Return the set of sources currently available in this resolved.
272      *
273      * @return An immutable point-in-time view of available sources.
274      */
275     public synchronized Set<SourceIdentifier> getAvailableSources() {
276         return ImmutableSet.copyOf(texts.keySet());
277     }
278
279     @Beta
280     public synchronized Collection<YangTextSchemaSource> getSourceTexts(final SourceIdentifier sourceIdentifier) {
281         return ImmutableSet.copyOf(texts.get(sourceIdentifier));
282     }
283
284     @Beta
285     public EffectiveModelContext trySchemaContext() throws SchemaResolutionException {
286         return trySchemaContext(StatementParserMode.DEFAULT_MODE);
287     }
288
289     @Beta
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));
296
297         try {
298             return future.get();
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;
305             }
306
307             throw new SchemaResolutionException("Failed to assemble SchemaContext", e);
308         }
309     }
310
311     @Override
312     public void close() {
313         transReg.close();
314     }
315
316     private static SchemaContextFactoryConfiguration config(final StatementParserMode statementParserMode) {
317         return SchemaContextFactoryConfiguration.builder().setStatementParserMode(statementParserMode).build();
318     }
319 }