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.Collections2;
14 import com.google.common.collect.ImmutableSet;
15 import java.io.IOException;
16 import java.util.Collection;
17 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.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;
32 * An incremental state reactor. Allows resolution of a SchemaContext based on a set of sources.
34 * @author Robert Varga
37 final class ProcessorModuleReactor {
38 private static final Logger LOG = LoggerFactory.getLogger(ProcessorModuleReactor.class);
40 private final YangTextSchemaContextResolver resolver;
41 private final Set<SourceIdentifier> sourcesInProject;
43 ProcessorModuleReactor(final YangTextSchemaContextResolver resolver) {
44 this.resolver = Preconditions.checkNotNull(resolver);
45 sourcesInProject = ImmutableSet.copyOf(resolver.getAvailableSources());
48 void registerSource(final YangTextSchemaSource source) throws SchemaSourceException, IOException,
49 YangSyntaxErrorException {
50 resolver.registerSource(source);
53 ContextHolder toContext() throws SchemaResolutionException {
54 final SchemaContext schemaContext = Verify.verifyNotNull(resolver.trySchemaContext());
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);
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);
73 return new ContextHolder(schemaContext, modules, sourcesInProject);
76 Collection<YangTextSchemaSource> getModelsInProject() {
77 return Collections2.transform(sourcesInProject, id -> resolver.getSourceTexts(id).iterator().next());
81 public String toString() {
82 return MoreObjects.toStringHelper(this).add("sources", sourcesInProject).add("resolver", resolver).toString();