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