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