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