2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.yang2sources.plugin;
10 import static com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Maps;
17 import com.google.common.io.CharStreams;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.Submodule;
30 import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
32 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
33 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * An incremental state reactor. Allows resolution of a SchemaContext based on a set of sources.
40 * @author Robert Varga
42 final class ProcessorModuleReactor {
43 private static final Logger LOG = LoggerFactory.getLogger(ProcessorModuleReactor.class);
45 private final Map<SourceIdentifier, YangTextSchemaSource> modelsInProject;
46 private final Collection<ScannedDependency> dependencies;
48 private YangParser parser;
50 ProcessorModuleReactor(final YangParser parser, final Collection<YangTextSchemaSource> modelsInProject,
51 final Collection<ScannedDependency> dependencies) {
52 this.parser = requireNonNull(parser);
53 this.modelsInProject = Maps.uniqueIndex(modelsInProject, YangTextSchemaSource::getIdentifier);
54 this.dependencies = ImmutableList.copyOf(dependencies);
57 ContextHolder toContext() throws IOException, YangParserException {
58 checkState(parser != null, "Context has already been assembled");
60 for (YangTextSchemaSource source : toUniqueSources(dependencies)) {
61 // This source is coming from a dependency:
62 // - its identifier should be accurate, as it should have been processed into a file with accurate name
63 // - it is not required to be parsed, hence we add it just as a library source
64 parser.addLibSource(source);
67 final EffectiveModelContext schemaContext = verifyNotNull(parser.buildEffectiveModel());
70 final Set<Module> modules = new HashSet<>();
71 for (Module module : schemaContext.getModules()) {
72 final SourceIdentifier modId = Util.moduleToIdentifier(module);
73 LOG.debug("Looking for source {}", modId);
74 if (modelsInProject.containsKey(modId)) {
75 LOG.debug("Module {} belongs to current project", module);
78 for (Submodule sub : module.getSubmodules()) {
79 final SourceIdentifier subId = Util.moduleToIdentifier(sub);
80 if (!modelsInProject.containsKey(subId)) {
81 LOG.warn("Submodule {} not found in input files", sub);
87 return new ContextHolder(schemaContext, modules, modelsInProject.keySet());
90 Collection<YangTextSchemaSource> getModelsInProject() {
91 return modelsInProject.values();
94 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE",
95 justification = "https://github.com/spotbugs/spotbugs/issues/600")
96 private static Collection<YangTextSchemaSource> toUniqueSources(final Collection<ScannedDependency> dependencies)
98 final Map<String, YangTextSchemaSource> byContent = new HashMap<>();
100 for (ScannedDependency dependency : dependencies) {
101 for (YangTextSchemaSource s : dependency.sources()) {
102 try (Reader reader = s.asCharSource(StandardCharsets.UTF_8).openStream()) {
103 final String contents = CharStreams.toString(reader);
104 byContent.putIfAbsent(contents, s);
108 return byContent.values();
112 public String toString() {
113 return MoreObjects.toStringHelper(this).add("sources", modelsInProject.keySet()).toString();