import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
+import java.util.Map;
import java.util.Set;
import javax.annotation.concurrent.NotThreadSafe;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final class ProcessorModuleReactor {
private static final Logger LOG = LoggerFactory.getLogger(ProcessorModuleReactor.class);
- private final YangTextSchemaContextResolver resolver;
- private final Set<SourceIdentifier> sourcesInProject;
+ private final Map<SourceIdentifier, YangTextSchemaSource> modelsInProject;
+ private final YangParser parser;
- ProcessorModuleReactor(final YangTextSchemaContextResolver resolver) {
- this.resolver = Preconditions.checkNotNull(resolver);
- sourcesInProject = ImmutableSet.copyOf(resolver.getAvailableSources());
+ ProcessorModuleReactor(final YangParser parser, final Collection<YangTextSchemaSource> modelsInProject) {
+ this.parser = Preconditions.checkNotNull(parser);
+ this.modelsInProject = Maps.uniqueIndex(modelsInProject, YangTextSchemaSource::getIdentifier);
}
- void registerSource(final YangTextSchemaSource source) throws SchemaSourceException, IOException,
- YangSyntaxErrorException {
- resolver.registerSource(source);
+ void registerSourceFromDependency(final YangTextSchemaSource source) throws YangSyntaxErrorException, IOException {
+ // This source is coming from a dependency:
+ // - its identifier should be accurate, as it should have been processed into a file with accurate name
+ // - it is not required to be parsed, hence we add it just as a library source
+ parser.addLibSource(source);
}
- ContextHolder toContext() throws SchemaResolutionException {
- final SchemaContext schemaContext = Verify.verifyNotNull(resolver.trySchemaContext());
+ ContextHolder toContext() throws YangParserException {
+ final SchemaContext schemaContext = Verify.verifyNotNull(parser.buildSchemaContext());
final Set<Module> modules = new HashSet<>();
for (Module module : schemaContext.getModules()) {
final SourceIdentifier modId = Util.moduleToIdentifier(module);
LOG.debug("Looking for source {}", modId);
- if (sourcesInProject.contains(modId)) {
+ if (modelsInProject.containsKey(modId)) {
LOG.debug("Module {} belongs to current project", module);
modules.add(module);
for (Module sub : module.getSubmodules()) {
final SourceIdentifier subId = Util.moduleToIdentifier(sub);
- if (sourcesInProject.contains(subId)) {
+ if (modelsInProject.containsKey(subId)) {
LOG.warn("Submodule {} not found in input files", sub);
}
}
}
}
- return new ContextHolder(schemaContext, modules, sourcesInProject);
+ return new ContextHolder(schemaContext, modules, modelsInProject.keySet());
}
Collection<YangTextSchemaSource> getModelsInProject() {
- return Collections2.transform(sourcesInProject, id -> resolver.getSourceTexts(id).iterator().next());
+ return modelsInProject.values();
}
@Override
public String toString() {
- return MoreObjects.toStringHelper(this).add("sources", sourcesInProject).add("resolver", resolver).toString();
+ return MoreObjects.toStringHelper(this).add("sources", modelsInProject.keySet()).add("parser", parser)
+ .toString();
}
}
*/
package org.opendaylight.yangtools.yang2sources.plugin;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.opendaylight.yangtools.yang.common.YangConstants;
-import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
import org.opendaylight.yangtools.yang2sources.spi.BuildContextAware;
class YangToSourcesProcessor {
private static final Logger LOG = LoggerFactory.getLogger(YangToSourcesProcessor.class);
+ private static final YangParserFactory DEFAULT_PARSER_FACTORY;
+
+ static {
+ final Iterator<YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
+ checkState(it.hasNext(), "Failed to find a YangParserFactory implementation");
+ DEFAULT_PARSER_FACTORY = it.next();
+ }
static final String LOG_PREFIX = "yang-to-sources:";
private static final String META_INF_STR = "META-INF";
static final String META_INF_YANG_STRING_JAR = META_INF_STR + "/" + YANG_STR;
static final String META_INF_YANG_SERVICES_STRING_JAR = META_INF_STR + "/" + "services";
+ private final YangParserFactory parserFactory;
private final File yangFilesRootDir;
private final Set<File> excludedFiles;
private final List<CodeGeneratorArg> codeGenerators;
this.project = requireNonNull(project);
this.inspectDependencies = inspectDependencies;
this.yangProvider = requireNonNull(yangProvider);
+ this.parserFactory = DEFAULT_PARSER_FACTORY;
}
@VisibleForTesting
try {
holder = createContextHolder(reactor);
- } catch (SchemaSourceException | YangSyntaxErrorException e) {
+ } catch (YangParserException e) {
throw new MojoFailureException("Failed to process reactor " + reactor, e);
} catch (IOException e) {
throw new MojoExecutionException("Failed to read reactor " + reactor, e);
return Optional.empty();
}
- final YangTextSchemaContextResolver resolver = YangTextSchemaContextResolver.create("maven-plugin");
+ // FIXME: add correct mode
+ final YangParser parser = parserFactory.createParser();
+ final List<YangTextSchemaSource> sourcesInProject = new ArrayList<>(yangFilesInProject.size());
for (final File f : yangFilesInProject) {
- resolver.registerSource(YangTextSchemaSource.forFile(f));
+ final YangTextSchemaSource textSource = YangTextSchemaSource.forFile(f);
+ final ASTSchemaSource astSource = TextToASTTransformer.transformText(textSource);
+
+ parser.addSource(astSource);
+
+ if (!astSource.getIdentifier().equals(textSource.getIdentifier())) {
+ // AST indicates a different source identifier, make sure we use that
+ sourcesInProject.add(YangTextSchemaSource.delegateForByteSource(astSource.getIdentifier(),
+ textSource));
+ } else {
+ sourcesInProject.add(textSource);
+ }
}
LOG.debug("Processed project files: {}", yangFilesInProject);
LOG.info("{} Project model files parsed: {}", LOG_PREFIX, yangFilesInProject.size());
- final ProcessorModuleReactor reactor = new ProcessorModuleReactor(resolver);
+ final ProcessorModuleReactor reactor = new ProcessorModuleReactor(parser, sourcesInProject);
LOG.debug("Initialized reactor {}", reactor, yangFilesInProject);
return Optional.of(reactor);
} catch (Exception e) {
}
private ContextHolder createContextHolder(final ProcessorModuleReactor reactor) throws MojoFailureException,
- IOException, SchemaSourceException, YangSyntaxErrorException {
+ IOException, YangParserException {
/**
* Set contains all modules generated from input sources. Number of
* modules may differ from number of sources due to submodules
final List<YangTextSchemaSource> sourcesInDependencies = Util.findYangFilesInDependenciesAsStream(
project);
for (YangTextSchemaSource s : toUniqueSources(sourcesInDependencies)) {
- reactor.registerSource(s);
+ reactor.registerSourceFromDependency(s);
}
}
*/
@SuppressWarnings("checkstyle:illegalCatch")
private void generateSources(final ContextHolder context) throws MojoFailureException {
- if (codeGenerators.size() == 0) {
+ if (codeGenerators.isEmpty()) {
LOG.warn("{} No code generators provided", LOG_PREFIX);
return;
}
private static <T> T getInstance(final String codeGeneratorClass, final Class<T> baseType) throws
ClassNotFoundException, InstantiationException, IllegalAccessException {
final Class<?> clazz = Class.forName(codeGeneratorClass);
-
- Preconditions.checkArgument(baseType.isAssignableFrom(clazz), "Code generator %s has to implement %s", clazz,
- baseType);
+ checkArgument(baseType.isAssignableFrom(clazz), "Code generator %s has to implement %s", clazz, baseType);
return baseType.cast(clazz.newInstance());
}
}