Fix failure source not being reported
[yangtools.git] / parser / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / AssembleSources.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 static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
11
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;
18 import java.util.Map;
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;
37
38 final class AssembleSources implements AsyncFunction<List<IRSchemaSource>, EffectiveModelContext> {
39     private static final Logger LOG = LoggerFactory.getLogger(AssembleSources.class);
40
41     private final @NonNull Function<IRSchemaSource, SourceIdentifier> getIdentifier;
42     private final @NonNull SchemaContextFactoryConfiguration config;
43     private final @NonNull YangParserFactory parserFactory;
44
45     AssembleSources(final @NonNull YangParserFactory parserFactory,
46             final @NonNull SchemaContextFactoryConfiguration config) {
47         this.parserFactory = parserFactory;
48         this.config = config;
49         switch (config.getStatementParserMode()) {
50             case SEMVER_MODE:
51                 this.getIdentifier = AssembleSources::getSemVerIdentifier;
52                 break;
53             default:
54                 this.getIdentifier = IRSchemaSource::getIdentifier;
55         }
56     }
57
58     @Override
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);
64
65         LOG.debug("Resolving dependency reactor {}", deps);
66
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());
75         }
76
77         final YangParser parser = parserFactory.createParser(res.parserConfig());
78         config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
79         config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
80
81         for (final Entry<SourceIdentifier, IRSchemaSource> entry : srcs.entrySet()) {
82             try {
83                 parser.addSource(entry.getValue());
84             } catch (YangSyntaxErrorException | IOException e) {
85                 throw new SchemaResolutionException("Failed to add source " + entry.getKey(), e);
86             }
87         }
88
89         final EffectiveModelContext schemaContext;
90         try {
91             schemaContext = parser.buildEffectiveModel();
92         } catch (final YangParserException e) {
93             if (e.getCause() instanceof ReactorException re) {
94                 throw new SchemaResolutionException("Failed to resolve required models", re.getSourceIdentifier(), e);
95             }
96             throw new SchemaResolutionException("Failed to resolve required models", e);
97         }
98
99         return immediateFluentFuture(schemaContext);
100     }
101
102     private static SemVerSourceIdentifier getSemVerIdentifier(final IRSchemaSource source) {
103         final SourceIdentifier identifier = source.getIdentifier();
104         final SemVer semver = YangModelDependencyInfo.findSemanticVersion(source.getRootStatement(), identifier);
105         if (identifier instanceof SemVerSourceIdentifier && semver == null) {
106             return (SemVerSourceIdentifier) identifier;
107         }
108
109         return SemVerSourceIdentifier.create(identifier.getName(), identifier.getRevision(), semver);
110     }
111 }