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 static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
12 import com.google.common.base.Function;
13 import com.google.common.collect.Maps;
14 import com.google.common.util.concurrent.AsyncFunction;
15 import com.google.common.util.concurrent.FluentFuture;
16 import java.io.IOException;
17 import java.util.List;
19 import java.util.Map.Entry;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.concepts.SemVer;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
25 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
27 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
28 import org.opendaylight.yangtools.yang.parser.api.YangParser;
29 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
30 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
31 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 final class AssembleSources implements AsyncFunction<List<IRSchemaSource>, EffectiveModelContext> {
39 private static final Logger LOG = LoggerFactory.getLogger(AssembleSources.class);
41 private final @NonNull Function<IRSchemaSource, SourceIdentifier> getIdentifier;
42 private final @NonNull SchemaContextFactoryConfiguration config;
43 private final @NonNull YangParserFactory parserFactory;
45 AssembleSources(final @NonNull YangParserFactory parserFactory,
46 final @NonNull SchemaContextFactoryConfiguration config) {
47 this.parserFactory = parserFactory;
49 switch (config.getStatementParserMode()) {
51 this.getIdentifier = AssembleSources::getSemVerIdentifier;
54 this.getIdentifier = IRSchemaSource::getIdentifier;
59 public FluentFuture<EffectiveModelContext> apply(final List<IRSchemaSource> sources)
60 throws SchemaResolutionException, ReactorException {
61 final Map<SourceIdentifier, IRSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
62 final Map<SourceIdentifier, YangModelDependencyInfo> deps =
63 Maps.transformValues(srcs, YangModelDependencyInfo::forIR);
65 LOG.debug("Resolving dependency reactor {}", deps);
67 final StatementParserMode statementParserMode = config.getStatementParserMode();
68 final DependencyResolver res = statementParserMode == StatementParserMode.SEMVER_MODE
69 ? SemVerDependencyResolver.create(deps) : RevisionDependencyResolver.create(deps);
70 if (!res.getUnresolvedSources().isEmpty()) {
71 LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(),
72 res.getUnsatisfiedImports());
73 throw new SchemaResolutionException("Failed to resolve required models",
74 res.getResolvedSources(), res.getUnsatisfiedImports());
77 final YangParser parser = parserFactory.createParser(res.parserConfig());
78 config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
79 config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
81 for (final Entry<SourceIdentifier, IRSchemaSource> entry : srcs.entrySet()) {
83 parser.addSource(entry.getValue());
84 } catch (YangSyntaxErrorException | IOException e) {
85 throw new SchemaResolutionException("Failed to add source " + entry.getKey(), e);
89 final EffectiveModelContext schemaContext;
91 schemaContext = parser.buildEffectiveModel();
92 } catch (final YangParserException e) {
93 final Throwable cause = e.getCause();
94 if (cause instanceof ReactorException) {
95 throw new SchemaResolutionException("Failed to resolve required models",
96 ((ReactorException) cause).getSourceIdentifier(), e);
98 throw new SchemaResolutionException("Failed to resolve required models", e);
101 return immediateFluentFuture(schemaContext);
104 private static SemVerSourceIdentifier getSemVerIdentifier(final IRSchemaSource source) {
105 final SourceIdentifier identifier = source.getIdentifier();
106 final SemVer semver = YangModelDependencyInfo.findSemanticVersion(source.getRootStatement(), identifier);
107 if (identifier instanceof SemVerSourceIdentifier && semver == null) {
108 return (SemVerSourceIdentifier) identifier;
111 return SemVerSourceIdentifier.create(identifier.getName(), identifier.getRevision(), semver);