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