Remove AbstractStatementSupport.createEmptyDeclared()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaContextFactory.java
1 /*
2  * Copyright (c) 2014 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 java.util.Objects.requireNonNull;
11 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
12
13 import com.google.common.base.Function;
14 import com.google.common.cache.Cache;
15 import com.google.common.cache.CacheBuilder;
16 import com.google.common.collect.Collections2;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.Iterables;
19 import com.google.common.collect.Maps;
20 import com.google.common.util.concurrent.AsyncFunction;
21 import com.google.common.util.concurrent.FluentFuture;
22 import com.google.common.util.concurrent.FutureCallback;
23 import com.google.common.util.concurrent.Futures;
24 import com.google.common.util.concurrent.ListenableFuture;
25 import com.google.common.util.concurrent.MoreExecutors;
26 import com.google.common.util.concurrent.SettableFuture;
27 import java.io.IOException;
28 import java.util.Collection;
29 import java.util.LinkedHashMap;
30 import java.util.LinkedHashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Set;
35 import java.util.concurrent.ExecutionException;
36 import org.eclipse.jdt.annotation.NonNull;
37 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
38 import org.opendaylight.yangtools.concepts.SemVer;
39 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
40 import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
41 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
42 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
43 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
44 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
45 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
46 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
47 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
48 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
49 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
50 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
51 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo;
52 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 final class SharedSchemaContextFactory implements EffectiveModelContextFactory {
57     private static final Logger LOG = LoggerFactory.getLogger(SharedSchemaContextFactory.class);
58
59     private final Cache<Collection<SourceIdentifier>, EffectiveModelContext> revisionCache = CacheBuilder.newBuilder()
60             .weakValues().build();
61     private final Cache<Collection<SourceIdentifier>, EffectiveModelContext> semVerCache = CacheBuilder.newBuilder()
62             .weakValues().build();
63     private final @NonNull SharedSchemaRepository repository;
64     private final @NonNull SchemaContextFactoryConfiguration config;
65
66     SharedSchemaContextFactory(final @NonNull SharedSchemaRepository repository,
67             final @NonNull SchemaContextFactoryConfiguration config) {
68         this.repository = requireNonNull(repository);
69         this.config = requireNonNull(config);
70     }
71
72     @Override
73     public @NonNull ListenableFuture<EffectiveModelContext> createEffectiveModelContext(
74             final @NonNull Collection<SourceIdentifier> requiredSources) {
75         return createSchemaContext(requiredSources,
76                 config.getStatementParserMode() == StatementParserMode.SEMVER_MODE ? semVerCache : revisionCache,
77                 new AssembleSources(repository.factory(), config));
78     }
79
80     private @NonNull ListenableFuture<EffectiveModelContext> createSchemaContext(
81             final Collection<SourceIdentifier> requiredSources,
82             final Cache<Collection<SourceIdentifier>, EffectiveModelContext> cache,
83             final AsyncFunction<List<IRSchemaSource>, EffectiveModelContext> assembleSources) {
84         // Make sources unique
85         final List<SourceIdentifier> uniqueSourceIdentifiers = deDuplicateSources(requiredSources);
86
87         final EffectiveModelContext existing = cache.getIfPresent(uniqueSourceIdentifiers);
88         if (existing != null) {
89             LOG.debug("Returning cached context {}", existing);
90             return immediateFluentFuture(existing);
91         }
92
93         // Request all sources be loaded
94         ListenableFuture<List<IRSchemaSource>> sf = Futures.allAsList(Collections2.transform(uniqueSourceIdentifiers,
95             this::requestSource));
96
97         // Detect mismatch between requested Source IDs and IDs that are extracted from parsed source
98         // Also remove duplicates if present
99         // We are relying on preserved order of uniqueSourceIdentifiers as well as sf
100         sf = Futures.transform(sf, new SourceIdMismatchDetector(uniqueSourceIdentifiers),
101             MoreExecutors.directExecutor());
102
103         // Assemble sources into a schema context
104         final ListenableFuture<EffectiveModelContext> cf = Futures.transformAsync(sf, assembleSources,
105             MoreExecutors.directExecutor());
106
107         final SettableFuture<EffectiveModelContext> rf = SettableFuture.create();
108         Futures.addCallback(cf, new FutureCallback<EffectiveModelContext>() {
109             @Override
110             public void onSuccess(final EffectiveModelContext result) {
111                 // Deduplicate concurrent loads
112                 final EffectiveModelContext existing;
113                 try {
114                     existing = cache.get(uniqueSourceIdentifiers, () -> result);
115                 } catch (ExecutionException e) {
116                     LOG.warn("Failed to recheck result with cache, will use computed value", e);
117                     rf.set(result);
118                     return;
119                 }
120
121                 rf.set(existing);
122             }
123
124             @Override
125             public void onFailure(final Throwable cause) {
126                 LOG.debug("Failed to assemble sources", cause);
127                 rf.setException(cause);
128             }
129         }, MoreExecutors.directExecutor());
130
131         return rf;
132     }
133
134     private ListenableFuture<IRSchemaSource> requestSource(final @NonNull SourceIdentifier identifier) {
135         return repository.getSchemaSource(identifier, IRSchemaSource.class);
136     }
137
138     /**
139      * Return a set of de-duplicated inputs.
140      *
141      * @return set (preserving ordering) from the input collection
142      */
143     private static List<SourceIdentifier> deDuplicateSources(final Collection<SourceIdentifier> requiredSources) {
144         final Set<SourceIdentifier> uniqueSourceIdentifiers = new LinkedHashSet<>(requiredSources);
145         if (uniqueSourceIdentifiers.size() == requiredSources.size()) {
146             // Can potentially reuse input
147             return ImmutableList.copyOf(requiredSources);
148         }
149
150         LOG.warn("Duplicate sources requested for schema context, removed duplicate sources: {}",
151             Collections2.filter(uniqueSourceIdentifiers, input -> Iterables.frequency(requiredSources, input) > 1));
152         return ImmutableList.copyOf(uniqueSourceIdentifiers);
153     }
154
155     @SuppressModernizer
156     private static final class SourceIdMismatchDetector implements Function<List<IRSchemaSource>,
157             List<IRSchemaSource>> {
158         private final List<SourceIdentifier> sourceIdentifiers;
159
160         SourceIdMismatchDetector(final List<SourceIdentifier> sourceIdentifiers) {
161             this.sourceIdentifiers = requireNonNull(sourceIdentifiers);
162         }
163
164         @Override
165         public List<IRSchemaSource> apply(final List<IRSchemaSource> input) {
166             final Map<SourceIdentifier, IRSchemaSource> filtered = new LinkedHashMap<>();
167
168             for (int i = 0; i < input.size(); i++) {
169
170                 final SourceIdentifier expectedSId = sourceIdentifiers.get(i);
171                 final IRSchemaSource irSchemaSource = input.get(i);
172                 final SourceIdentifier realSId = irSchemaSource.getIdentifier();
173
174                 if (!expectedSId.equals(realSId)) {
175                     LOG.warn("Source identifier mismatch for module \"{}\", requested as {} but actually is {}. "
176                         + "Using actual id", expectedSId.getName(), expectedSId, realSId);
177                 }
178
179                 if (filtered.containsKey(realSId)) {
180                     LOG.warn("Duplicate source for module {} detected in reactor", realSId);
181                 }
182
183                 filtered.put(realSId, irSchemaSource);
184
185             }
186             return ImmutableList.copyOf(filtered.values());
187         }
188     }
189
190     private static final class AssembleSources implements AsyncFunction<List<IRSchemaSource>, EffectiveModelContext> {
191         private final @NonNull YangParserFactory parserFactory;
192         private final @NonNull SchemaContextFactoryConfiguration config;
193         private final @NonNull Function<IRSchemaSource, SourceIdentifier> getIdentifier;
194
195         private AssembleSources(final @NonNull YangParserFactory parserFactory,
196                 final @NonNull SchemaContextFactoryConfiguration config) {
197             this.parserFactory = parserFactory;
198             this.config = config;
199             switch (config.getStatementParserMode()) {
200                 case SEMVER_MODE:
201                     this.getIdentifier = AssembleSources::getSemVerIdentifier;
202                     break;
203                 default:
204                     this.getIdentifier = IRSchemaSource::getIdentifier;
205             }
206         }
207
208         @Override
209         public FluentFuture<EffectiveModelContext> apply(final List<IRSchemaSource> sources)
210                 throws SchemaResolutionException, ReactorException {
211             final Map<SourceIdentifier, IRSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
212             final Map<SourceIdentifier, YangModelDependencyInfo> deps =
213                     Maps.transformValues(srcs, YangModelDependencyInfo::forIR);
214
215             LOG.debug("Resolving dependency reactor {}", deps);
216
217             final StatementParserMode statementParserMode = config.getStatementParserMode();
218             final DependencyResolver res = statementParserMode == StatementParserMode.SEMVER_MODE
219                     ? SemVerDependencyResolver.create(deps) : RevisionDependencyResolver.create(deps);
220             if (!res.getUnresolvedSources().isEmpty()) {
221                 LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(),
222                     res.getUnsatisfiedImports());
223                 throw new SchemaResolutionException("Failed to resolve required models",
224                         res.getResolvedSources(), res.getUnsatisfiedImports());
225             }
226
227             final YangParser parser = parserFactory.createParser(statementParserMode);
228             config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
229             config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
230
231             for (final Entry<SourceIdentifier, IRSchemaSource> entry : srcs.entrySet()) {
232                 try {
233                     parser.addSource(entry.getValue());
234                 } catch (YangSyntaxErrorException | IOException e) {
235                     throw new SchemaResolutionException("Failed to add source " + entry.getKey(), e);
236                 }
237             }
238
239             final EffectiveModelContext schemaContext;
240             try {
241                 schemaContext = parser.buildEffectiveModel();
242             } catch (final YangParserException e) {
243                 throw new SchemaResolutionException("Failed to resolve required models", e);
244             }
245
246             return immediateFluentFuture(schemaContext);
247         }
248
249         private static SemVerSourceIdentifier getSemVerIdentifier(final IRSchemaSource source) {
250             final SourceIdentifier identifier = source.getIdentifier();
251             final SemVer semver = YangModelDependencyInfo.findSemanticVersion(source.getRootStatement(), identifier);
252             if (identifier instanceof SemVerSourceIdentifier && semver == null) {
253                 return (SemVerSourceIdentifier) identifier;
254             }
255
256             return SemVerSourceIdentifier.create(identifier.getName(), identifier.getRevision(), semver);
257         }
258     }
259 }