1594fd4212704836a7207008bfb619f9cafd67b6
[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.parser.impl.util.YangModelDependencyInfo;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
44 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
45 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
47 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
48 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 final class SharedSchemaContextFactory implements SchemaContextFactory {
53     private static final ExceptionMapper<SchemaResolutionException> MAPPER = ReflectiveExceptionMapper.create("resolve sources", SchemaResolutionException.class);
54     private static final Logger LOG = LoggerFactory.getLogger(SharedSchemaContextFactory.class);
55
56     private final Function<SourceIdentifier, ListenableFuture<ASTSchemaSource>> requestSources = new Function<SourceIdentifier, ListenableFuture<ASTSchemaSource>>() {
57         @Override
58         public ListenableFuture<ASTSchemaSource> apply(final SourceIdentifier input) {
59             return repository.getSchemaSource(input, ASTSchemaSource.class);
60         }
61     };
62     private final Cache<Collection<SourceIdentifier>, SchemaContext> cache = CacheBuilder.newBuilder().weakValues().build();
63     private final SharedSchemaRepository repository;
64     // FIXME: ignored right now
65     private final SchemaSourceFilter filter;
66
67     // FIXME SchemaRepository should be the type for repository parameter instead of SharedSchemaRepository (final implementation)
68     public SharedSchemaContextFactory(final SharedSchemaRepository repository, final SchemaSourceFilter filter) {
69         this.repository = Preconditions.checkNotNull(repository);
70         this.filter = Preconditions.checkNotNull(filter);
71     }
72
73     @Override
74     public CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
75             final Collection<SourceIdentifier> requiredSources, java.util.function.Predicate<QName> isFeatureSupported) {
76         // Make sources unique
77         final List<SourceIdentifier> uniqueSourceIdentifiers = deDuplicateSources(requiredSources);
78
79         final SchemaContext existing = cache.getIfPresent(uniqueSourceIdentifiers);
80         if (existing != null) {
81             LOG.debug("Returning cached context {}", existing);
82             return Futures.immediateCheckedFuture(existing);
83         }
84
85         // Request all sources be loaded
86         ListenableFuture<List<ASTSchemaSource>> sf = Futures.allAsList(Collections2.transform(uniqueSourceIdentifiers, requestSources));
87
88         // Detect mismatch between requested Source IDs and IDs that are extracted from parsed source
89         // Also remove duplicates if present
90         // We are relying on preserved order of uniqueSourceIdentifiers as well as sf
91         sf = Futures.transform(sf, new SourceIdMismatchDetector(uniqueSourceIdentifiers));
92
93         // Assemble sources into a schema context
94         final AssembleSources assembleSources = new AssembleSources(isFeatureSupported);
95         final ListenableFuture<SchemaContext> cf = Futures.transform(sf, assembleSources);
96
97         // Populate cache when successful
98         Futures.addCallback(cf, new FutureCallback<SchemaContext>() {
99             @Override
100             public void onSuccess(final SchemaContext result) {
101                 cache.put(uniqueSourceIdentifiers, result);
102             }
103
104             @Override
105             public void onFailure(final Throwable t) {
106                 LOG.debug("Failed to assemble sources", t);
107             }
108         });
109
110         return Futures.makeChecked(cf, MAPPER);
111     }
112
113     /**
114      * @return set (preserving ordering) from the input collection
115      */
116     private static List<SourceIdentifier> deDuplicateSources(final Collection<SourceIdentifier> requiredSources) {
117         final Set<SourceIdentifier> uniqueSourceIdentifiers = new LinkedHashSet<>(requiredSources);
118         if (uniqueSourceIdentifiers.size() == requiredSources.size()) {
119             // Can potentially reuse input
120             return ImmutableList.copyOf(requiredSources);
121         }
122
123         LOG.warn("Duplicate sources requested for schema context, removed duplicate sources: {}",
124             Collections2.filter(uniqueSourceIdentifiers, new Predicate<SourceIdentifier>() {
125                 @Override
126                 public boolean apply(@Nullable final SourceIdentifier input) {
127                     return Iterables.frequency(requiredSources, input) > 1;
128                 }
129             }));
130         return ImmutableList.copyOf(uniqueSourceIdentifiers);
131     }
132
133     private static final class SourceIdMismatchDetector implements Function<List<ASTSchemaSource>, List<ASTSchemaSource>> {
134         private final List<SourceIdentifier> sourceIdentifiers;
135
136         public SourceIdMismatchDetector(final List<SourceIdentifier> sourceIdentifiers) {
137             this.sourceIdentifiers = Preconditions.checkNotNull(sourceIdentifiers);
138         }
139
140         @Override
141         public List<ASTSchemaSource> apply(final List<ASTSchemaSource> input) {
142             final Map<SourceIdentifier, ASTSchemaSource> filtered = new LinkedHashMap<>();
143
144             for (int i = 0; i < input.size(); i++) {
145
146                 final SourceIdentifier expectedSId = sourceIdentifiers.get(i);
147                 final ASTSchemaSource astSchemaSource = input.get(i);
148                 final SourceIdentifier realSId = astSchemaSource.getIdentifier();
149
150                 if (!expectedSId.equals(realSId)) {
151                     LOG.warn("Source identifier mismatch for module \"{}\", requested as {} but actually is {}. Using actual id",
152                         expectedSId.getName(), expectedSId, realSId);
153                 }
154
155                 if (filtered.containsKey(realSId)) {
156                     LOG.warn("Duplicate source for module {} detected in reactor", realSId);
157                 }
158
159                 filtered.put(realSId, astSchemaSource);
160
161             }
162             return ImmutableList.copyOf(filtered.values());
163         }
164     }
165
166     private static final class AssembleSources implements AsyncFunction<List<ASTSchemaSource>, SchemaContext> {
167
168         private final java.util.function.Predicate<QName> isFeatureSupported;
169
170         private AssembleSources(final java.util.function.Predicate<QName> isFeatureSupported) {
171             this.isFeatureSupported = Preconditions.checkNotNull(isFeatureSupported);
172         }
173
174         @Override
175         public ListenableFuture<SchemaContext> apply(List<ASTSchemaSource> sources) throws SchemaResolutionException,
176                 SourceException, ReactorException {
177             final Map<SourceIdentifier, ASTSchemaSource> srcs =
178                     Maps.uniqueIndex(sources, ASTSchemaSource.GET_IDENTIFIER);
179             final Map<SourceIdentifier, YangModelDependencyInfo> deps =
180                     Maps.transformValues(srcs, ASTSchemaSource.GET_DEPINFO);
181
182             LOG.debug("Resolving dependency reactor {}", deps);
183
184             final DependencyResolver res = DependencyResolver.create(deps);
185             if (!res.getUnresolvedSources().isEmpty()) {
186                 LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(), res.getUnsatisfiedImports());
187                 throw new SchemaResolutionException("Failed to resolve required models",
188                         res.getResolvedSources(), res.getUnsatisfiedImports());
189             }
190
191             final Map<SourceIdentifier, ParserRuleContext> asts = Maps.transformValues(srcs, ASTSchemaSource.GET_AST);
192             final CrossSourceStatementReactor.BuildAction reactor =
193                     YangInferencePipeline.RFC6020_REACTOR.newBuild(isFeatureSupported);
194
195             for (final Entry<SourceIdentifier, ParserRuleContext> e : asts.entrySet()) {
196                 final ParserRuleContext parserRuleCtx = e.getValue();
197                 Preconditions.checkArgument(parserRuleCtx instanceof StatementContext,
198                         "Unsupported context class %s for source %s", parserRuleCtx.getClass(), e.getKey());
199
200                 reactor.addSource(new YangStatementSourceImpl(e.getKey(), (StatementContext) parserRuleCtx));
201             }
202
203             SchemaContext schemaContext = reactor.buildEffective();
204
205             return Futures.immediateCheckedFuture(schemaContext);
206         }
207     }
208 }