Separate out Module and Submodule interfaces
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / ProcessorModuleReactor.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang2sources.plugin;
9
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;
13
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;
25 import java.util.Map;
26 import java.util.Set;
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;
36
37 /**
38  * An incremental state reactor. Allows resolution of a SchemaContext based on a set of sources.
39  *
40  * @author Robert Varga
41  */
42 final class ProcessorModuleReactor {
43     private static final Logger LOG = LoggerFactory.getLogger(ProcessorModuleReactor.class);
44
45     private final Map<SourceIdentifier, YangTextSchemaSource> modelsInProject;
46     private final Collection<ScannedDependency> dependencies;
47
48     private YangParser parser;
49
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);
55     }
56
57     ContextHolder toContext() throws IOException, YangParserException {
58         checkState(parser != null, "Context has already been assembled");
59
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);
65         }
66
67         final EffectiveModelContext schemaContext = verifyNotNull(parser.buildEffectiveModel());
68         parser = null;
69
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);
76                 modules.add(module);
77
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);
82                     }
83                 }
84             }
85         }
86
87         return new ContextHolder(schemaContext, modules, modelsInProject.keySet());
88     }
89
90     Collection<YangTextSchemaSource> getModelsInProject() {
91         return modelsInProject.values();
92     }
93
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)
97             throws IOException {
98         final Map<String, YangTextSchemaSource> byContent = new HashMap<>();
99
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);
105                 }
106             }
107         }
108         return byContent.values();
109     }
110
111     @Override
112     public String toString() {
113         return MoreObjects.toStringHelper(this).add("sources", modelsInProject.keySet()).toString();
114     }
115 }