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.Maps;
14 import java.io.IOException;
15 import java.util.Collection;
16 import java.util.HashSet;
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;
31 * An incremental state reactor. Allows resolution of a SchemaContext based on a set of sources.
33 * @author Robert Varga
36 final class ProcessorModuleReactor {
37 private static final Logger LOG = LoggerFactory.getLogger(ProcessorModuleReactor.class);
39 private final Map<SourceIdentifier, YangTextSchemaSource> modelsInProject;
40 private final YangParser parser;
42 ProcessorModuleReactor(final YangParser parser, final Collection<YangTextSchemaSource> modelsInProject) {
43 this.parser = Preconditions.checkNotNull(parser);
44 this.modelsInProject = Maps.uniqueIndex(modelsInProject, YangTextSchemaSource::getIdentifier);
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);
54 ContextHolder toContext() throws YangParserException {
55 final SchemaContext schemaContext = Verify.verifyNotNull(parser.buildSchemaContext());
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);
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);
74 return new ContextHolder(schemaContext, modules, modelsInProject.keySet());
77 Collection<YangTextSchemaSource> getModelsInProject() {
78 return modelsInProject.values();
82 public String toString() {
83 return MoreObjects.toStringHelper(this).add("sources", modelsInProject.keySet()).add("parser", parser)