Remove StatementParserMode.SEMVER_MODE
[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.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.parser.api.YangParser;
26 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
27 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
28 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 final class AssembleSources implements AsyncFunction<List<IRSchemaSource>, EffectiveModelContext> {
36     private static final Logger LOG = LoggerFactory.getLogger(AssembleSources.class);
37
38     private final @NonNull Function<IRSchemaSource, SourceIdentifier> getIdentifier;
39     private final @NonNull SchemaContextFactoryConfiguration config;
40     private final @NonNull YangParserFactory parserFactory;
41
42     AssembleSources(final @NonNull YangParserFactory parserFactory,
43             final @NonNull SchemaContextFactoryConfiguration config) {
44         this.parserFactory = parserFactory;
45         this.config = config;
46         getIdentifier = switch (config.getStatementParserMode()) {
47             case DEFAULT_MODE -> IRSchemaSource::getIdentifier;
48         };
49     }
50
51     @Override
52     public FluentFuture<EffectiveModelContext> apply(final List<IRSchemaSource> sources)
53             throws SchemaResolutionException, ReactorException {
54         final Map<SourceIdentifier, IRSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
55         final Map<SourceIdentifier, YangModelDependencyInfo> deps =
56                 Maps.transformValues(srcs, YangModelDependencyInfo::forIR);
57
58         LOG.debug("Resolving dependency reactor {}", deps);
59
60         final DependencyResolver res = switch (config.getStatementParserMode()) {
61             case DEFAULT_MODE -> RevisionDependencyResolver.create(deps);
62         };
63
64         if (!res.getUnresolvedSources().isEmpty()) {
65             LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(),
66                 res.getUnsatisfiedImports());
67             throw new SchemaResolutionException("Failed to resolve required models",
68                     res.getResolvedSources(), res.getUnsatisfiedImports());
69         }
70
71         final YangParser parser = parserFactory.createParser(res.parserConfig());
72         config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
73         config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
74
75         for (final Entry<SourceIdentifier, IRSchemaSource> entry : srcs.entrySet()) {
76             try {
77                 parser.addSource(entry.getValue());
78             } catch (YangSyntaxErrorException | IOException e) {
79                 throw new SchemaResolutionException("Failed to add source " + entry.getKey(), e);
80             }
81         }
82
83         final EffectiveModelContext schemaContext;
84         try {
85             schemaContext = parser.buildEffectiveModel();
86         } catch (final YangParserException e) {
87             if (e.getCause() instanceof ReactorException re) {
88                 throw new SchemaResolutionException("Failed to resolve required models", re.getSourceIdentifier(), e);
89             }
90             throw new SchemaResolutionException("Failed to resolve required models", e);
91         }
92
93         return immediateFluentFuture(schemaContext);
94     }
95 }