Switch YangToSourcesProcessor to use YangParser
[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 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.Maps;
14 import java.io.IOException;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19 import javax.annotation.concurrent.NotThreadSafe;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
23 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
24 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * An incremental state reactor. Allows resolution of a SchemaContext based on a set of sources.
32  *
33  * @author Robert Varga
34  */
35 @NotThreadSafe
36 final class ProcessorModuleReactor {
37     private static final Logger LOG = LoggerFactory.getLogger(ProcessorModuleReactor.class);
38
39     private final Map<SourceIdentifier, YangTextSchemaSource> modelsInProject;
40     private final YangParser parser;
41
42     ProcessorModuleReactor(final YangParser parser, final Collection<YangTextSchemaSource> modelsInProject) {
43         this.parser = Preconditions.checkNotNull(parser);
44         this.modelsInProject = Maps.uniqueIndex(modelsInProject, YangTextSchemaSource::getIdentifier);
45     }
46
47     void registerSourceFromDependency(final YangTextSchemaSource source) throws YangSyntaxErrorException, IOException {
48         // This source is coming from a dependency:
49         // - its identifier should be accurate, as it should have been processed into a file with accurate name
50         // - it is not required to be parsed, hence we add it just as a library source
51         parser.addLibSource(source);
52     }
53
54     ContextHolder toContext() throws YangParserException {
55         final SchemaContext schemaContext = Verify.verifyNotNull(parser.buildSchemaContext());
56
57         final Set<Module> modules = new HashSet<>();
58         for (Module module : schemaContext.getModules()) {
59             final SourceIdentifier modId = Util.moduleToIdentifier(module);
60             LOG.debug("Looking for source {}", modId);
61             if (modelsInProject.containsKey(modId)) {
62                 LOG.debug("Module {} belongs to current project", module);
63                 modules.add(module);
64
65                 for (Module sub : module.getSubmodules()) {
66                     final SourceIdentifier subId = Util.moduleToIdentifier(sub);
67                     if (modelsInProject.containsKey(subId)) {
68                         LOG.warn("Submodule {} not found in input files", sub);
69                     }
70                 }
71             }
72         }
73
74         return new ContextHolder(schemaContext, modules, modelsInProject.keySet());
75     }
76
77     Collection<YangTextSchemaSource> getModelsInProject() {
78         return modelsInProject.values();
79     }
80
81     @Override
82     public String toString() {
83         return MoreObjects.toStringHelper(this).add("sources", modelsInProject.keySet()).add("parser", parser)
84                 .toString();
85     }
86 }