BUG-7568: Use YangTextSchemaSource to emit schema files
[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.Collections2;
14 import com.google.common.collect.ImmutableSet;
15 import java.io.IOException;
16 import java.util.Collection;
17 import java.util.HashSet;
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.YangSyntaxErrorException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * An incremental state reactor. Allows resolution of a SchemaContext based on a set of sources.
33  *
34  * @author Robert Varga
35  */
36 @NotThreadSafe
37 final class ProcessorModuleReactor {
38     private static final Logger LOG = LoggerFactory.getLogger(ProcessorModuleReactor.class);
39
40     private final YangTextSchemaContextResolver resolver;
41     private final Set<SourceIdentifier> sourcesInProject;
42
43     ProcessorModuleReactor(final YangTextSchemaContextResolver resolver) {
44         this.resolver = Preconditions.checkNotNull(resolver);
45         sourcesInProject = ImmutableSet.copyOf(resolver.getAvailableSources());
46     }
47
48     void registerSource(final YangTextSchemaSource source) throws SchemaSourceException, IOException,
49             YangSyntaxErrorException {
50         resolver.registerSource(source);
51     }
52
53     ContextHolder toContext() throws SchemaResolutionException {
54         final SchemaContext schemaContext = Verify.verifyNotNull(resolver.trySchemaContext());
55
56         final Set<Module> modules = new HashSet<>();
57         for (Module module : schemaContext.getModules()) {
58             final SourceIdentifier modId = Util.moduleToIdentifier(module);
59             LOG.debug("Looking for source {}", modId);
60             if (sourcesInProject.contains(modId)) {
61                 LOG.debug("Module {} belongs to current project", module);
62                 modules.add(module);
63
64                 for (Module sub : module.getSubmodules()) {
65                     final SourceIdentifier subId = Util.moduleToIdentifier(sub);
66                     if (sourcesInProject.contains(subId)) {
67                         LOG.warn("Submodule {} not found in input files", sub);
68                     }
69                 }
70             }
71         }
72
73         return new ContextHolder(schemaContext, modules, sourcesInProject);
74     }
75
76     Collection<YangTextSchemaSource> getModelsInProject() {
77         return Collections2.transform(sourcesInProject, id -> resolver.getSourceTexts(id).iterator().next());
78     }
79
80     @Override
81     public String toString() {
82         return MoreObjects.toStringHelper(this).add("sources", sourcesInProject).add("resolver", resolver).toString();
83     }
84 }