Eliminate YangModelDependencyInfo
[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 com.google.common.base.Function;
11 import com.google.common.collect.Maps;
12 import com.google.common.util.concurrent.AsyncFunction;
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.io.IOException;
15 import java.util.List;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
18 import org.opendaylight.yangtools.yang.ir.YangIRSchemaSource;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
23 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
24 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
25 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangIRSourceInfoExtractor;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 final class AssembleSources implements AsyncFunction<List<YangIRSchemaSource>, EffectiveModelContext> {
32     private static final Logger LOG = LoggerFactory.getLogger(AssembleSources.class);
33
34     private final @NonNull Function<YangIRSchemaSource, SourceIdentifier> getIdentifier;
35     private final @NonNull SchemaContextFactoryConfiguration config;
36     private final @NonNull YangParserFactory parserFactory;
37
38     AssembleSources(final @NonNull YangParserFactory parserFactory,
39             final @NonNull SchemaContextFactoryConfiguration config) {
40         this.parserFactory = parserFactory;
41         this.config = config;
42         getIdentifier = switch (config.getStatementParserMode()) {
43             case DEFAULT_MODE -> YangIRSchemaSource::sourceId;
44         };
45     }
46
47     @Override
48     public FluentFuture<EffectiveModelContext> apply(final List<YangIRSchemaSource> sources) {
49         final var srcs = Maps.uniqueIndex(sources, getIdentifier);
50         final var deps = Maps.transformValues(srcs, YangIRSourceInfoExtractor::forIR);
51         LOG.debug("Resolving dependency reactor {}", deps);
52
53         final var res = switch (config.getStatementParserMode()) {
54             case DEFAULT_MODE -> new RevisionDependencyResolver(deps);
55         };
56
57         final var unresolved = res.unresolvedSources();
58         if (!unresolved.isEmpty()) {
59             LOG.debug("Omitting models {} due to unsatisfied imports {}", unresolved, res.unsatisfiedImports());
60             return FluentFutures.immediateFailedFluentFuture(
61                 new SchemaResolutionException("Failed to resolve required models", unresolved.get(0),
62                     res.resolvedSources(), res.unsatisfiedImports()));
63         }
64
65         final var parser = parserFactory.createParser(res.parserConfig());
66         config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
67         config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
68
69         for (var entry : srcs.entrySet()) {
70             try {
71                 parser.addSource(entry.getValue());
72             } catch (YangSyntaxErrorException | IOException e) {
73                 final var sourceId = entry.getKey();
74                 return FluentFutures.immediateFailedFluentFuture(
75                     new SchemaResolutionException("Failed to add source " + sourceId, sourceId, e));
76             }
77         }
78
79         final EffectiveModelContext schemaContext;
80         try {
81             schemaContext = parser.buildEffectiveModel();
82         } catch (final YangParserException e) {
83             return FluentFutures.immediateFailedFluentFuture(e.getCause() instanceof ReactorException re
84                 ? new SchemaResolutionException("Failed to resolve required models", re.getSourceIdentifier(), re) : e);
85         }
86
87         return FluentFutures.immediateFluentFuture(schemaContext);
88     }
89 }