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 com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Maps;
15 import com.google.common.io.CharStreams;
16 import java.io.IOException;
17 import java.io.Reader;
18 import java.nio.charset.StandardCharsets;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.HashSet;
24 import javax.annotation.concurrent.NotThreadSafe;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
28 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
29 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
30 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * An incremental state reactor. Allows resolution of a SchemaContext based on a set of sources.
37 * @author Robert Varga
40 final class ProcessorModuleReactor {
41 private static final Logger LOG = LoggerFactory.getLogger(ProcessorModuleReactor.class);
43 private final Map<SourceIdentifier, YangTextSchemaSource> modelsInProject;
44 private final Collection<ScannedDependency> dependencies;
45 private final YangParser parser;
47 ProcessorModuleReactor(final YangParser parser, final Collection<YangTextSchemaSource> modelsInProject,
48 final Collection<ScannedDependency> dependencies) {
49 this.parser = Preconditions.checkNotNull(parser);
50 this.modelsInProject = Maps.uniqueIndex(modelsInProject, YangTextSchemaSource::getIdentifier);
51 this.dependencies = ImmutableList.copyOf(dependencies);
54 ContextHolder toContext() throws IOException, YangParserException {
55 for (YangTextSchemaSource source : toUniqueSources(dependencies)) {
56 // This source is coming from a dependency:
57 // - its identifier should be accurate, as it should have been processed into a file with accurate name
58 // - it is not required to be parsed, hence we add it just as a library source
59 parser.addLibSource(source);
62 final SchemaContext schemaContext = Verify.verifyNotNull(parser.buildSchemaContext());
64 final Set<Module> modules = new HashSet<>();
65 for (Module module : schemaContext.getModules()) {
66 final SourceIdentifier modId = Util.moduleToIdentifier(module);
67 LOG.debug("Looking for source {}", modId);
68 if (modelsInProject.containsKey(modId)) {
69 LOG.debug("Module {} belongs to current project", module);
72 for (Module sub : module.getSubmodules()) {
73 final SourceIdentifier subId = Util.moduleToIdentifier(sub);
74 if (!modelsInProject.containsKey(subId)) {
75 LOG.warn("Submodule {} not found in input files", sub);
81 return new ContextHolder(schemaContext, modules, modelsInProject.keySet());
84 Collection<YangTextSchemaSource> getModelsInProject() {
85 return modelsInProject.values();
88 private static Collection<YangTextSchemaSource> toUniqueSources(final Collection<ScannedDependency> dependencies)
90 final Map<String, YangTextSchemaSource> byContent = new HashMap<>();
92 for (ScannedDependency dependency : dependencies) {
93 for (YangTextSchemaSource s : dependency.sources()) {
94 try (Reader reader = s.asCharSource(StandardCharsets.UTF_8).openStream()) {
95 final String contents = CharStreams.toString(reader);
96 byContent.putIfAbsent(contents, s);
100 return byContent.values();
104 public String toString() {
105 return MoreObjects.toStringHelper(this).add("sources", modelsInProject.keySet()).add("parser", parser)