af0b8a85dc44be5000b029a5132ba61b655d960f
[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.parser.api.YangParser;
30 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
31 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * An incremental state reactor. Allows resolution of a SchemaContext based on a set of sources.
38  *
39  * @author Robert Varga
40  */
41 final class ProcessorModuleReactor {
42     private static final Logger LOG = LoggerFactory.getLogger(ProcessorModuleReactor.class);
43
44     private final Map<SourceIdentifier, YangTextSchemaSource> modelsInProject;
45     private final Collection<ScannedDependency> dependencies;
46
47     private YangParser parser;
48
49     ProcessorModuleReactor(final YangParser parser, final Collection<YangTextSchemaSource> modelsInProject,
50         final Collection<ScannedDependency> dependencies) {
51         this.parser = requireNonNull(parser);
52         this.modelsInProject = Maps.uniqueIndex(modelsInProject, YangTextSchemaSource::getIdentifier);
53         this.dependencies = ImmutableList.copyOf(dependencies);
54     }
55
56     ContextHolder toContext() throws IOException, YangParserException {
57         checkState(parser != null, "Context has already been assembled");
58
59         for (YangTextSchemaSource source : toUniqueSources(dependencies)) {
60             // This source is coming from a dependency:
61             // - its identifier should be accurate, as it should have been processed into a file with accurate name
62             // - it is not required to be parsed, hence we add it just as a library source
63             parser.addLibSource(source);
64         }
65
66         final EffectiveModelContext schemaContext = verifyNotNull(parser.buildEffectiveModel());
67         parser = null;
68
69         final Set<Module> modules = new HashSet<>();
70         for (Module module : schemaContext.getModules()) {
71             final SourceIdentifier modId = Util.moduleToIdentifier(module);
72             LOG.debug("Looking for source {}", modId);
73             if (modelsInProject.containsKey(modId)) {
74                 LOG.debug("Module {} belongs to current project", module);
75                 modules.add(module);
76
77                 for (Module sub : module.getSubmodules()) {
78                     final SourceIdentifier subId = Util.moduleToIdentifier(sub);
79                     if (!modelsInProject.containsKey(subId)) {
80                         LOG.warn("Submodule {} not found in input files", sub);
81                     }
82                 }
83             }
84         }
85
86         return new ContextHolder(schemaContext, modules, modelsInProject.keySet());
87     }
88
89     Collection<YangTextSchemaSource> getModelsInProject() {
90         return modelsInProject.values();
91     }
92
93     @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE",
94         justification = "https://github.com/spotbugs/spotbugs/issues/600")
95     private static Collection<YangTextSchemaSource> toUniqueSources(final Collection<ScannedDependency> dependencies)
96             throws IOException {
97         final Map<String, YangTextSchemaSource> byContent = new HashMap<>();
98
99         for (ScannedDependency dependency : dependencies) {
100             for (YangTextSchemaSource s : dependency.sources()) {
101                 try (Reader reader = s.asCharSource(StandardCharsets.UTF_8).openStream()) {
102                     final String contents = CharStreams.toString(reader);
103                     byContent.putIfAbsent(contents, s);
104                 }
105             }
106         }
107         return byContent.values();
108     }
109
110     @Override
111     public String toString() {
112         return MoreObjects.toStringHelper(this).add("sources", modelsInProject.keySet()).toString();
113     }
114 }