X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=yang%2Fyang-maven-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang2sources%2Fplugin%2FUtil.java;h=3656c859b825b55f0be31b7eff417a7c8b341bb9;hb=e8ac56902440704082470574abf0cd159f66293b;hp=2948eac274c5a890f738c9436062a87d65779f8f;hpb=52f7fb68ef766fc17d8861eee0ea444045b9cb4d;p=yangtools.git diff --git a/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/Util.java b/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/Util.java index 2948eac274..3656c859b8 100644 --- a/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/Util.java +++ b/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/Util.java @@ -8,26 +8,24 @@ package org.opendaylight.yangtools.yang2sources.plugin; import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_FILE_EXTENSION; +import static org.opendaylight.yangtools.yang2sources.plugin.YangToSourcesProcessor.LOG_PREFIX; +import static org.opendaylight.yangtools.yang2sources.plugin.YangToSourcesProcessor.META_INF_YANG_STRING; +import static org.opendaylight.yangtools.yang2sources.plugin.YangToSourcesProcessor.META_INF_YANG_STRING_JAR; -import com.google.common.base.Splitter; -import com.google.common.collect.Iterables; -import java.io.Closeable; +import com.google.common.io.ByteSource; +import com.google.common.io.ByteStreams; import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; +import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import org.apache.commons.io.FileUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.ArtifactResolutionRequest; @@ -37,46 +35,27 @@ import org.apache.maven.model.Plugin; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; import org.apache.maven.repository.RepositorySystem; +import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; import org.opendaylight.yangtools.yang.model.api.Module; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier; +import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; +import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; final class Util { /** - * It isn't desirable to create instances of this class + * It isn't desirable to create instances of this class. */ private Util() { } - static final String YANG_SUFFIX = "yang"; - private static final Logger LOG = LoggerFactory.getLogger(Util.class); - static Collection listFiles(final File root, final Collection excludedFiles) throws FileNotFoundException { - if (!root.exists()) { - LOG.warn("{} YANG source directory {} not found. No code will be generated.", YangToSourcesProcessor - .LOG_PREFIX, root.toString()); - - return Collections.emptyList(); - } - Collection result = new ArrayList<>(); - Collection yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true); - for (File f : yangFiles) { - if (excludedFiles.contains(f)) { - LOG.info("{} {} file excluded {}", YangToSourcesProcessor.LOG_PREFIX, Util.YANG_SUFFIX.toUpperCase(), - f); - } else { - result.add(f); - } - } - - return result; - } - static List getClassPath(final MavenProject project) { - List dependencies = new ArrayList<>(); + final List dependencies = new ArrayList<>(); for (Artifact element : project.getArtifacts()) { File asFile = element.getFile(); if (isJar(asFile) || asFile.isDirectory()) { @@ -103,8 +82,7 @@ final class Util { final ArtifactRepository localRepo, final List remoteRepos) { Plugin plugin = project.getPlugin(YangToSourcesMojo.PLUGIN_NAME); if (plugin == null) { - LOG.warn("{} {} not found, dependencies version check skipped", YangToSourcesProcessor.LOG_PREFIX, - YangToSourcesMojo.PLUGIN_NAME); + LOG.warn("{} {} not found, dependencies version check skipped", LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME); } else { Map> pluginDependencies = new HashMap<>(); getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos); @@ -167,123 +145,96 @@ final class Util { for (org.apache.maven.artifact.Artifact d : dependencies) { if (artifact.getGroupId().equals(d.getGroupId()) && artifact.getArtifactId().equals(d.getArtifactId())) { if (!(artifact.getVersion().equals(d.getVersion()))) { - LOG.warn("{} Dependency resolution conflict:", YangToSourcesProcessor.LOG_PREFIX); - LOG.warn("{} '{}' dependency [{}] has different version than one declared in current project [{}]" + - ". It is recommended to fix this problem because it may cause compilation errors.", - YangToSourcesProcessor.LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME, artifact, d); + LOG.warn("{} Dependency resolution conflict:", LOG_PREFIX); + LOG.warn("{} '{}' dependency [{}] has different version than one declared in current project [{}]" + + ". It is recommended to fix this problem because it may cause compilation errors.", + LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME, artifact, d); } } } } - private static final String JAR_SUFFIX = ".jar"; - private static boolean isJar(final File element) { - return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)); + return element.isFile() && element.getName().endsWith(".jar"); } - static final class YangsInZipsResult implements Closeable { - private final List yangStreams; - private final List zipInputStreams; - - private YangsInZipsResult(final List yangStreams, - final List zipInputStreams) { - this.yangStreams = yangStreams; - this.zipInputStreams = zipInputStreams; - } - - @Override - public void close() throws IOException { - for (Closeable is : zipInputStreams) { - is.close(); - } - } - - public List getYangStreams() { - return this.yangStreams; - } - } - - static YangsInZipsResult findYangFilesInDependenciesAsStream(final MavenProject project) + @SuppressWarnings("checkstyle:illegalCatch") + static List findYangFilesInDependenciesAsStream(final MavenProject project) throws MojoFailureException { - List yangsFromDependencies = new ArrayList<>(); - List zips = new ArrayList<>(); try { - List filesOnCp = Util.getClassPath(project); - LOG.info("{} Searching for yang files in following dependencies: {}", YangToSourcesProcessor.LOG_PREFIX, - filesOnCp); + final List filesOnCp = Util.getClassPath(project); + LOG.info("{} Searching for yang files in following dependencies: {}", LOG_PREFIX, filesOnCp); + final List yangsFromDependencies = new ArrayList<>(); for (File file : filesOnCp) { - List foundFilesForReporting = new ArrayList<>(); + final List foundFilesForReporting = new ArrayList<>(); // is it jar file or directory? if (file.isDirectory()) { //FIXME: code duplicate - File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING); + File yangDir = new File(file, META_INF_YANG_STRING); if (yangDir.exists() && yangDir.isDirectory()) { File[] yangFiles = yangDir.listFiles( (dir, name) -> name.endsWith(RFC6020_YANG_FILE_EXTENSION) && new File(dir, name).isFile()); for (final File yangFile : yangFiles) { - yangsFromDependencies.add(new YangSourceFromFile(yangFile)); + foundFilesForReporting.add(yangFile.getName()); + yangsFromDependencies.add(YangTextSchemaSource.forFile(yangFile)); } } - } else { - ZipFile zip = new ZipFile(file); - zips.add(zip); + try (ZipFile zip = new ZipFile(file)) { + final Enumeration entries = zip.entries(); + while (entries.hasMoreElements()) { + final ZipEntry entry = entries.nextElement(); + final String entryName = entry.getName(); - Enumeration entries = zip.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - String entryName = entry.getName(); + if (entryName.startsWith(META_INF_YANG_STRING_JAR) && !entry.isDirectory() + && entryName.endsWith(RFC6020_YANG_FILE_EXTENSION)) { + foundFilesForReporting.add(entryName); - if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR) - && !entry.isDirectory() && entryName.endsWith(RFC6020_YANG_FILE_EXTENSION)) { - foundFilesForReporting.add(entryName); - yangsFromDependencies.add(new YangSourceInZipFile(zip, entry)); + yangsFromDependencies.add(YangTextSchemaSource.delegateForByteSource( + entryName.substring(entryName.lastIndexOf('/') + 1), + ByteSource.wrap(ByteStreams.toByteArray(zip.getInputStream(entry))))); + } } } } if (foundFilesForReporting.size() > 0) { - LOG.info("{} Found {} yang files in {}: {}", YangToSourcesProcessor.LOG_PREFIX, - foundFilesForReporting.size(), file, foundFilesForReporting); + LOG.info("{} Found {} yang files in {}: {}", LOG_PREFIX, foundFilesForReporting.size(), file, + foundFilesForReporting); } } + + return yangsFromDependencies; } catch (Exception e) { throw new MojoFailureException(e.getMessage(), e); } - return new YangsInZipsResult(yangsFromDependencies, zips); } /** - * Find all dependencies which contains yang sources - * + * Find all dependencies which contains yang sources. * Returns collection of YANG files and Zip files which contains YANG files. * - * FIXME: Rename to what class is actually doing. - * - * @param project - * @return - * @throws MojoFailureException */ + // FIXME: Rename to what class is actually doing. + @SuppressWarnings("checkstyle:illegalCatch") static Collection findYangFilesInDependencies(final MavenProject project) throws MojoFailureException { final List yangsFilesFromDependencies = new ArrayList<>(); - List filesOnCp; + final List filesOnCp; try { filesOnCp = Util.getClassPath(project); } catch (Exception e) { throw new MojoFailureException("Failed to scan for YANG files in dependencies", e); } - LOG.info("{} Searching for yang files in following dependencies: {}", YangToSourcesProcessor.LOG_PREFIX, - filesOnCp); + LOG.info("{} Searching for yang files in following dependencies: {}", LOG_PREFIX, filesOnCp); for (File file : filesOnCp) { try { // is it jar file or directory? if (file.isDirectory()) { //FIXME: code duplicate - File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING); + File yangDir = new File(file, META_INF_YANG_STRING); if (yangDir.exists() && yangDir.isDirectory()) { File[] yangFiles = yangDir.listFiles( (dir, name) -> name.endsWith(RFC6020_YANG_FILE_EXTENSION) && new File(dir, name).isFile()); @@ -298,10 +249,9 @@ final class Util { ZipEntry entry = entries.nextElement(); String entryName = entry.getName(); - if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR) + if (entryName.startsWith(META_INF_YANG_STRING_JAR) && !entry.isDirectory() && entryName.endsWith(RFC6020_YANG_FILE_EXTENSION)) { - LOG.debug("{} Found a YANG file in {}: {}", YangToSourcesProcessor.LOG_PREFIX, file, - entryName); + LOG.debug("{} Found a YANG file in {}: {}", LOG_PREFIX, file, entryName); yangsFilesFromDependencies.add(file); break; } @@ -315,32 +265,16 @@ final class Util { return yangsFilesFromDependencies; } - static final class ContextHolder { - private static final Splitter SEP_SPLITTER = Splitter.on(File.separator); - - private final SchemaContext context; - private final Set yangModules; - - ContextHolder(final SchemaContext context, final Set yangModules) { - this.context = context; - this.yangModules = yangModules; - } - - SchemaContext getContext() { - return context; - } - - Set getYangModules() { - return yangModules; + static SourceIdentifier moduleToIdentifier(final Module module) { + final QNameModule mod = module.getQNameModule(); + final Date rev = mod.getRevision(); + final com.google.common.base.Optional optRev; + if (SimpleDateFormatUtil.DEFAULT_DATE_REV.equals(rev)) { + optRev = com.google.common.base.Optional.absent(); + } else { + optRev = com.google.common.base.Optional.of(mod.getFormattedRevision()); } - Optional moduleToResourcePath(final Module mod) { - if (!yangModules.contains(mod)) { - return Optional.empty(); - } - - return Optional.of("/" + YangToSourcesProcessor.META_INF_YANG_STRING_JAR + "/" - + Iterables.getLast(SEP_SPLITTER.split(mod.getModuleSourcePath()))); - } + return RevisionSourceIdentifier.create(module.getName(), optRev); } }