2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.repo;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.LinkedHashMap;
15 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.NavigableMap;
21 import javax.annotation.Nullable;
23 import org.antlr.v4.runtime.ParserRuleContext;
24 import org.antlr.v4.runtime.tree.ParseTreeWalker;
25 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
26 import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
30 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
32 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
33 import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
34 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
35 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
36 import org.opendaylight.yangtools.yang.parser.impl.YangParserListenerImpl;
37 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
38 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.google.common.base.Function;
43 import com.google.common.base.Optional;
44 import com.google.common.base.Preconditions;
45 import com.google.common.base.Predicate;
46 import com.google.common.cache.Cache;
47 import com.google.common.cache.CacheBuilder;
48 import com.google.common.collect.Collections2;
49 import com.google.common.collect.Iterables;
50 import com.google.common.collect.Lists;
51 import com.google.common.collect.Maps;
52 import com.google.common.collect.Sets;
53 import com.google.common.util.concurrent.AsyncFunction;
54 import com.google.common.util.concurrent.CheckedFuture;
55 import com.google.common.util.concurrent.FutureCallback;
56 import com.google.common.util.concurrent.Futures;
57 import com.google.common.util.concurrent.ListenableFuture;
59 final class SharedSchemaContextFactory implements SchemaContextFactory {
60 private static final ExceptionMapper<SchemaResolutionException> MAPPER = ReflectiveExceptionMapper.create("resolve sources", SchemaResolutionException.class);
61 private static final Logger LOG = LoggerFactory.getLogger(SharedSchemaContextFactory.class);
63 private final Function<SourceIdentifier, ListenableFuture<ASTSchemaSource>> requestSources = new Function<SourceIdentifier, ListenableFuture<ASTSchemaSource>>() {
65 public ListenableFuture<ASTSchemaSource> apply(final SourceIdentifier input) {
66 return repository.getSchemaSource(input, ASTSchemaSource.class);
69 private final Cache<Collection<SourceIdentifier>, SchemaContext> cache = CacheBuilder.newBuilder().weakValues().build();
71 private final AsyncFunction<List<ASTSchemaSource>, SchemaContext> assembleSources = new AsyncFunction<List<ASTSchemaSource>, SchemaContext>() {
73 public ListenableFuture<SchemaContext> apply(final List<ASTSchemaSource> sources) throws SchemaResolutionException {
74 final Map<SourceIdentifier, ASTSchemaSource> srcs =
75 Maps.uniqueIndex(sources, ASTSchemaSource.GET_IDENTIFIER);
76 final Map<SourceIdentifier, YangModelDependencyInfo> deps =
77 Maps.transformValues(srcs, ASTSchemaSource.GET_DEPINFO);
79 LOG.debug("Resolving dependency reactor {}", deps);
81 final DependencyResolver res = DependencyResolver.create(deps);
82 if (!res.getUnresolvedSources().isEmpty()) {
83 LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(), res.getUnsatisfiedImports());
85 // FIXME: push into DependencyResolver
87 throw new SchemaResolutionException("Failed to resolve required models",
88 res.getResolvedSources(), res.getUnsatisfiedImports());
91 final Map<SourceIdentifier, ParserRuleContext> asts =
92 Maps.transformValues(srcs, ASTSchemaSource.GET_AST);
93 final Map<String, NavigableMap<Date, URI>> namespaceContext = BuilderUtils.createYangNamespaceContext(
94 asts.values(), Optional.<SchemaContext>absent());
96 final ParseTreeWalker walker = new ParseTreeWalker();
97 final Map<SourceIdentifier, ModuleBuilder> sourceToBuilder = new LinkedHashMap<>();
99 for (final Entry<SourceIdentifier, ParserRuleContext> entry : asts.entrySet()) {
100 final ModuleBuilder moduleBuilder = YangParserListenerImpl.create(namespaceContext, entry.getKey().getName(),
101 walker, entry.getValue()).getModuleBuilder();
103 moduleBuilder.setSource(srcs.get(entry.getKey()).getYangText());
104 sourceToBuilder.put(entry.getKey(), moduleBuilder);
106 LOG.debug("Modules ready for integration");
108 final YangParserImpl parser = YangParserImpl.getInstance();
109 final Collection<Module> modules = parser.buildModules(sourceToBuilder.values());
110 LOG.debug("Integrated cross-references modules");
111 return Futures.immediateCheckedFuture(parser.assembleContext(modules));
115 private final SharedSchemaRepository repository;
116 // FIXME: ignored right now
117 private final SchemaSourceFilter filter;
119 // FIXME SchemaRepository should be the type for repository parameter instead of SharedSchemaRepository (final implementation)
120 public SharedSchemaContextFactory(final SharedSchemaRepository repository, final SchemaSourceFilter filter) {
121 this.repository = Preconditions.checkNotNull(repository);
122 this.filter = Preconditions.checkNotNull(filter);
126 public CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(final Collection<SourceIdentifier> requiredSources) {
127 // Make sources unique
128 final List<SourceIdentifier> uniqueSourceIdentifiers = deDuplicateSources(requiredSources);
130 final SchemaContext existing = cache.getIfPresent(uniqueSourceIdentifiers);
131 if (existing != null) {
132 LOG.debug("Returning cached context {}", existing);
133 return Futures.immediateCheckedFuture(existing);
136 // Request all sources be loaded
137 ListenableFuture<List<ASTSchemaSource>> sf = Futures.allAsList(Collections2.transform(uniqueSourceIdentifiers, requestSources));
139 // Detect mismatch between requested Source IDs and IDs that are extracted from parsed source
140 // Also remove duplicates if present
141 // We are relying on preserved order of uniqueSourceIdentifiers as well as sf
142 sf = Futures.transform(sf, new SourceIdMismatchDetector(uniqueSourceIdentifiers));
144 // Assemble sources into a schema context
145 final ListenableFuture<SchemaContext> cf = Futures.transform(sf, assembleSources);
147 // Populate cache when successful
148 Futures.addCallback(cf, new FutureCallback<SchemaContext>() {
150 public void onSuccess(final SchemaContext result) {
151 cache.put(uniqueSourceIdentifiers, result);
155 public void onFailure(final Throwable t) {
156 LOG.debug("Failed to assemble sources", t);
160 return Futures.makeChecked(cf, MAPPER);
164 * @return set (preserving ordering) from the input collection
166 private List<SourceIdentifier> deDuplicateSources(final Collection<SourceIdentifier> requiredSources) {
167 final Set<SourceIdentifier> uniqueSourceIdentifiers = Collections.unmodifiableSet(Sets.newLinkedHashSet(requiredSources));
168 if(uniqueSourceIdentifiers.size() != requiredSources.size()) {
169 LOG.warn("Duplicate sources requested for schema context, removed duplicate sources: {}", Collections2.filter(uniqueSourceIdentifiers, new Predicate<SourceIdentifier>() {
171 public boolean apply(@Nullable final SourceIdentifier input) {
172 return Iterables.frequency(requiredSources, input) > 1;
176 return Lists.newArrayList(uniqueSourceIdentifiers);
179 private static final class SourceIdMismatchDetector implements Function<List<ASTSchemaSource>, List<ASTSchemaSource>> {
180 private final List<SourceIdentifier> sourceIdentifiers;
182 public SourceIdMismatchDetector(final List<SourceIdentifier> sourceIdentifiers) {
183 this.sourceIdentifiers = sourceIdentifiers;
187 public List<ASTSchemaSource> apply(final List<ASTSchemaSource> input) {
188 final Map<SourceIdentifier, ASTSchemaSource> filtered = Maps.newLinkedHashMap();
190 for (int i = 0; i < input.size(); i++) {
192 final SourceIdentifier expectedSId = sourceIdentifiers.get(i);
193 final ASTSchemaSource astSchemaSource = input.get(i);
194 final SourceIdentifier realSId = astSchemaSource.getIdentifier();
196 if (!expectedSId.equals(realSId)) {
197 LOG.warn("Source identifier mismatch for module \"{}\", requested as {} but actually is {}. Using actual id", expectedSId.getName(), expectedSId, realSId);
200 if (filtered.containsKey(realSId)) {
201 LOG.warn("Duplicate source for module {} detected in reactor", realSId);
204 filtered.put(realSId, astSchemaSource);
207 return Lists.newArrayList(filtered.values());