Promote SchemaSourceRepresentation
[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 org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.ir.YangIRSchemaSource;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
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.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.repo.YangModelDependencyInfo;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 final class AssembleSources implements AsyncFunction<List<YangIRSchemaSource>, EffectiveModelContext> {
35     private static final Logger LOG = LoggerFactory.getLogger(AssembleSources.class);
36
37     private final @NonNull Function<YangIRSchemaSource, SourceIdentifier> getIdentifier;
38     private final @NonNull SchemaContextFactoryConfiguration config;
39     private final @NonNull YangParserFactory parserFactory;
40
41     AssembleSources(final @NonNull YangParserFactory parserFactory,
42             final @NonNull SchemaContextFactoryConfiguration config) {
43         this.parserFactory = parserFactory;
44         this.config = config;
45         getIdentifier = switch (config.getStatementParserMode()) {
46             case DEFAULT_MODE -> YangIRSchemaSource::sourceId;
47         };
48     }
49
50     @Override
51     public FluentFuture<EffectiveModelContext> apply(final List<YangIRSchemaSource> sources)
52             throws SchemaResolutionException, ReactorException {
53         final Map<SourceIdentifier, YangIRSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
54         final Map<SourceIdentifier, YangModelDependencyInfo> deps =
55                 Maps.transformValues(srcs, YangModelDependencyInfo::forIR);
56
57         LOG.debug("Resolving dependency reactor {}", deps);
58
59         final DependencyResolver res = switch (config.getStatementParserMode()) {
60             case DEFAULT_MODE -> RevisionDependencyResolver.create(deps);
61         };
62
63         if (!res.getUnresolvedSources().isEmpty()) {
64             LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(),
65                 res.getUnsatisfiedImports());
66             throw new SchemaResolutionException("Failed to resolve required models",
67                     res.getResolvedSources(), res.getUnsatisfiedImports());
68         }
69
70         final YangParser parser = parserFactory.createParser(res.parserConfig());
71         config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
72         config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
73
74         for (var entry : srcs.entrySet()) {
75             try {
76                 parser.addSource(entry.getValue());
77             } catch (YangSyntaxErrorException | IOException e) {
78                 final var sourceId = entry.getKey();
79                 throw new SchemaResolutionException("Failed to add source " + sourceId, sourceId, 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 }