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 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;
35 final class AssembleSources implements AsyncFunction<List<IRSchemaSource>, EffectiveModelContext> {
36 private static final Logger LOG = LoggerFactory.getLogger(AssembleSources.class);
38 private final @NonNull Function<IRSchemaSource, SourceIdentifier> getIdentifier;
39 private final @NonNull SchemaContextFactoryConfiguration config;
40 private final @NonNull YangParserFactory parserFactory;
42 AssembleSources(final @NonNull YangParserFactory parserFactory,
43 final @NonNull SchemaContextFactoryConfiguration config) {
44 this.parserFactory = parserFactory;
46 getIdentifier = switch (config.getStatementParserMode()) {
47 case DEFAULT_MODE -> IRSchemaSource::getIdentifier;
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);
58 LOG.debug("Resolving dependency reactor {}", deps);
60 final DependencyResolver res = switch (config.getStatementParserMode()) {
61 case DEFAULT_MODE -> RevisionDependencyResolver.create(deps);
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());
71 final YangParser parser = parserFactory.createParser(res.parserConfig());
72 config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
73 config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
75 for (final Entry<SourceIdentifier, IRSchemaSource> entry : srcs.entrySet()) {
77 parser.addSource(entry.getValue());
78 } catch (YangSyntaxErrorException | IOException e) {
79 throw new SchemaResolutionException("Failed to add source " + entry.getKey(), 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);