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 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;
34 final class AssembleSources implements AsyncFunction<List<YangIRSchemaSource>, EffectiveModelContext> {
35 private static final Logger LOG = LoggerFactory.getLogger(AssembleSources.class);
37 private final @NonNull Function<YangIRSchemaSource, SourceIdentifier> getIdentifier;
38 private final @NonNull SchemaContextFactoryConfiguration config;
39 private final @NonNull YangParserFactory parserFactory;
41 AssembleSources(final @NonNull YangParserFactory parserFactory,
42 final @NonNull SchemaContextFactoryConfiguration config) {
43 this.parserFactory = parserFactory;
45 getIdentifier = switch (config.getStatementParserMode()) {
46 case DEFAULT_MODE -> YangIRSchemaSource::sourceId;
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);
57 LOG.debug("Resolving dependency reactor {}", deps);
59 final DependencyResolver res = switch (config.getStatementParserMode()) {
60 case DEFAULT_MODE -> RevisionDependencyResolver.create(deps);
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());
70 final YangParser parser = parserFactory.createParser(res.parserConfig());
71 config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
72 config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
74 for (var entry : srcs.entrySet()) {
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);
83 final EffectiveModelContext schemaContext;
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);
90 throw new SchemaResolutionException("Failed to resolve required models", e);
93 return immediateFluentFuture(schemaContext);