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;
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
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.Maps;
17 import com.google.common.util.concurrent.AsyncFunction;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.FutureCallback;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.TreeMap;
30 import org.antlr.v4.runtime.ParserRuleContext;
31 import org.antlr.v4.runtime.tree.ParseTreeWalker;
32 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
33 import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper;
34 import org.opendaylight.yangtools.yang.model.api.Module;
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.parser.builder.impl.BuilderUtils;
41 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
42 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
43 import org.opendaylight.yangtools.yang.parser.impl.YangParserListenerImpl;
44 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
45 import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 final class SharedSchemaContextFactory implements SchemaContextFactory {
50 private static final ExceptionMapper<SchemaResolutionException> MAPPER = ReflectiveExceptionMapper.create("resolve sources", SchemaResolutionException.class);
51 private static final Logger LOG = LoggerFactory.getLogger(SharedSchemaContextFactory.class);
53 private final Function<SourceIdentifier, ListenableFuture<ASTSchemaSource>> requestSources = new Function<SourceIdentifier, ListenableFuture<ASTSchemaSource>>() {
55 public ListenableFuture<ASTSchemaSource> apply(final SourceIdentifier input) {
56 return repository.getSchemaSource(input, ASTSchemaSource.class);
59 private final Cache<Collection<SourceIdentifier>, SchemaContext> cache = CacheBuilder.newBuilder().softValues().build();
61 private final AsyncFunction<List<ASTSchemaSource>, SchemaContext> assembleSources = new AsyncFunction<List<ASTSchemaSource>, SchemaContext>() {
63 public ListenableFuture<SchemaContext> apply(final List<ASTSchemaSource> sources) throws SchemaResolutionException {
64 final Map<SourceIdentifier, ASTSchemaSource> srcs =
65 Maps.uniqueIndex(sources, ASTSchemaSource.GET_IDENTIFIER);
66 final Map<SourceIdentifier, YangModelDependencyInfo> deps =
67 Maps.transformValues(srcs, ASTSchemaSource.GET_DEPINFO);
69 LOG.debug("Resolving dependency reactor {}", deps);
71 final DependencyResolver res = DependencyResolver.create(deps);
72 if (!res.getUnresolvedSources().isEmpty()) {
73 LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(), res.getUnsatisfiedImports());
75 // FIXME: push into DependencyResolver
77 throw new SchemaResolutionException("Failed to resolve required models",
78 res.getResolvedSources(), res.getUnsatisfiedImports());
81 final Map<SourceIdentifier, ParserRuleContext> asts =
82 Maps.transformValues(srcs, ASTSchemaSource.GET_AST);
83 final Map<String, TreeMap<Date, URI>> namespaceContext = BuilderUtils.createYangNamespaceContext(
84 asts.values(), Optional.<SchemaContext> absent());
86 final ParseTreeWalker walker = new ParseTreeWalker();
87 final Map<SourceIdentifier, ModuleBuilder> sourceToBuilder = new LinkedHashMap<>();
89 for (Entry<SourceIdentifier, ParserRuleContext> entry : asts.entrySet()) {
90 ModuleBuilder moduleBuilder = YangParserListenerImpl.create(namespaceContext, entry.getKey().getName(),
91 walker, entry.getValue()).getModuleBuilder();
93 moduleBuilder.setSource(srcs.get(entry.getKey()).getYangText());
94 sourceToBuilder.put(entry.getKey(), moduleBuilder);
96 LOG.debug("Modules ready for integration");
98 final YangParserImpl parser = YangParserImpl.getInstance();
99 final Collection<Module> modules = parser.buildModules(sourceToBuilder.values());
100 LOG.debug("Integrated cross-references modules");
101 return Futures.immediateCheckedFuture(parser.assembleContext(modules));
105 private final SharedSchemaRepository repository;
106 // FIXME: ignored right now
107 private final SchemaSourceFilter filter;
109 public SharedSchemaContextFactory(final SharedSchemaRepository repository, final SchemaSourceFilter filter) {
110 this.repository = Preconditions.checkNotNull(repository);
111 this.filter = Preconditions.checkNotNull(filter);
115 public CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(final Collection<SourceIdentifier> requiredSources) {
116 final SchemaContext existing = cache.getIfPresent(requiredSources);
117 if (existing != null) {
118 LOG.debug("Returning cached context {}", existing);
119 return Futures.immediateCheckedFuture(existing);
122 // Request all sources be loaded
123 final ListenableFuture<List<ASTSchemaSource>> sf = Futures.allAsList(Collections2.transform(requiredSources, requestSources));
125 // Assemble sources into a schema context
126 final ListenableFuture<SchemaContext> cf = Futures.transform(sf, assembleSources);
128 // Populate cache when successful
129 Futures.addCallback(cf, new FutureCallback<SchemaContext>() {
131 public void onSuccess(final SchemaContext result) {
132 cache.put(requiredSources, result);
136 public void onFailure(final Throwable t) {
137 LOG.debug("Failed to assemble sources", t);
141 return Futures.makeChecked(cf, MAPPER);