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 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.YangModelDependencyInfo;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 final class AssembleSources implements AsyncFunction<List<YangIRSchemaSource>, EffectiveModelContext> {
32 private static final Logger LOG = LoggerFactory.getLogger(AssembleSources.class);
34 private final @NonNull Function<YangIRSchemaSource, SourceIdentifier> getIdentifier;
35 private final @NonNull SchemaContextFactoryConfiguration config;
36 private final @NonNull YangParserFactory parserFactory;
38 AssembleSources(final @NonNull YangParserFactory parserFactory,
39 final @NonNull SchemaContextFactoryConfiguration config) {
40 this.parserFactory = parserFactory;
42 getIdentifier = switch (config.getStatementParserMode()) {
43 case DEFAULT_MODE -> YangIRSchemaSource::sourceId;
48 public FluentFuture<EffectiveModelContext> apply(final List<YangIRSchemaSource> sources) {
49 final var srcs = Maps.uniqueIndex(sources, getIdentifier);
50 final var deps = Maps.transformValues(srcs, YangModelDependencyInfo::forIR);
51 LOG.debug("Resolving dependency reactor {}", deps);
53 final var res = switch (config.getStatementParserMode()) {
54 case DEFAULT_MODE -> RevisionDependencyResolver.create(deps);
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()));
65 final var parser = parserFactory.createParser(res.parserConfig());
66 config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
67 config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
69 for (var entry : srcs.entrySet()) {
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));
79 final EffectiveModelContext schemaContext;
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);
87 return FluentFutures.immediateFluentFuture(schemaContext);