X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-maven-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang2sources%2Fplugin%2FUtil.java;h=07cf2e2ccdace69cd010296e4ecc1965ff67aedf;hb=refs%2Fchanges%2F93%2F92893%2F8;hp=a29ad42a4636d1961ad53d950a07d8882eea5dd8;hpb=eddc53acc6e439659970b15c13981551a5dcb0c4;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 a29ad42a46..07cf2e2ccd 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 @@ -7,129 +7,41 @@ */ package org.opendaylight.yangtools.yang2sources.plugin; -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import java.io.Closeable; +import static org.opendaylight.yangtools.yang2sources.plugin.YangToSourcesProcessor.LOG_PREFIX; + import java.io.File; -import java.io.FileNotFoundException; -import java.io.FilenameFilter; -import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; -import java.util.Collections; -import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; 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; import org.apache.maven.artifact.resolver.ArtifactResolutionResult; import org.apache.maven.model.Dependency; 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.model.api.Module; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.ModuleLike; +import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier; +import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; 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); - private static final int CACHE_SIZE = 10; - // Cache for listed directories and found yang files. Typically yang files - // are utilized twice. First: code is generated during generate-sources - // phase Second: yang files are copied as resources during - // generate-resources phase. This cache ensures that yang files are listed - // only once. - private static Map> cache = Maps.newHashMapWithExpectedSize(CACHE_SIZE); - - /** - * List files recursively and return as array of String paths. Use cache of - * size 1. - */ - static Collection listFiles(File root) throws FileNotFoundException { - if (cache.get(root) != null) { - return cache.get(root); - } - - if (!root.exists()) { - throw new FileNotFoundException(root.toString()); - } - - Collection yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true); - - toCache(root, yangFiles); - return yangFiles; - } - - static Collection listFiles(File root, File[] 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) { - boolean excluded = false; - for (File ex : excludedFiles) { - if (ex.equals(f)) { - excluded = true; - break; - } - } - if (excluded) { - LOG.info("{} {} file excluded {}", YangToSourcesProcessor.LOG_PREFIX, Util.YANG_SUFFIX.toUpperCase(), - f); - } else { - result.add(f); - } - } - - return result; - } - private static void toCache(final File rootDir, final Collection yangFiles) { - cache.put(rootDir, yangFiles); - } - - /** - * Instantiate object from fully qualified class name - */ - static T getInstance(String codeGeneratorClass, Class baseType) throws ClassNotFoundException, - InstantiationException, IllegalAccessException { - return baseType.cast(resolveClass(codeGeneratorClass, baseType).newInstance()); - } - - private static Class resolveClass(String codeGeneratorClass, Class baseType) throws ClassNotFoundException { - Class clazz = Class.forName(codeGeneratorClass); - - if (!baseType.isAssignableFrom(clazz)) { - throw new IllegalArgumentException("Code generator " + clazz + " has to implement " + baseType); - } - return clazz; - } - - static List getClassPath(MavenProject project) { - List dependencies = Lists.newArrayList(); + static List getClassPath(final MavenProject project) { + final List dependencies = new ArrayList<>(); for (Artifact element : project.getArtifacts()) { File asFile = element.getFile(); if (isJar(asFile) || asFile.isDirectory()) { @@ -152,12 +64,11 @@ final class Util { * @param remoteRepos * remote repositories */ - static void checkClasspath(MavenProject project, RepositorySystem repoSystem, ArtifactRepository localRepo, - List remoteRepos) { + static void checkClasspath(final MavenProject project, final RepositorySystem repoSystem, + 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); @@ -186,8 +97,9 @@ final class Util { * @param remoteRepos * list of remote repositories */ - private static void getPluginTransitiveDependencies(Plugin plugin, Map> map, - RepositorySystem repoSystem, ArtifactRepository localRepository, List remoteRepos) { + private static void getPluginTransitiveDependencies(final Plugin plugin, + final Map> map, final RepositorySystem repoSystem, + final ArtifactRepository localRepository, final List remoteRepos) { List pluginDependencies = plugin.getDependencies(); for (Dependency dep : pluginDependencies) { @@ -215,185 +127,24 @@ final class Util { * @param dependencies * collection of dependencies */ - private static void checkArtifact(Artifact artifact, Collection dependencies) { + private static void checkArtifact(final Artifact artifact, final Collection dependencies) { 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); + if (!artifact.getVersion().equals(d.getVersion())) { + 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(File element) { - return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)); + private static boolean isJar(final File element) { + return element.isFile() && element.getName().endsWith(".jar"); } - static T checkNotNull(T obj, String paramName) { - return Preconditions.checkNotNull(obj, "Parameter " + paramName + " is null"); + static SourceIdentifier moduleToIdentifier(final ModuleLike module) { + return RevisionSourceIdentifier.create(module.getName(), module.getRevision()); } - - static final class YangsInZipsResult implements Closeable { - private final List yangStreams; - private final List zipInputStreams; - - private YangsInZipsResult(List yangStreams, 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(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); - - for (File file : filesOnCp) { - 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); - if (yangDir.exists() && yangDir.isDirectory()) { - File[] yangFiles = yangDir.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.endsWith(".yang") && new File(dir, name).isFile(); - } - }); - for (final File yangFile : yangFiles) { - yangsFromDependencies.add(new YangSourceFromFile(yangFile)); - } - } - - } else { - ZipFile zip = new ZipFile(file); - zips.add(zip); - - Enumeration entries = zip.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - String entryName = entry.getName(); - - if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR) - && !entry.isDirectory() && entryName.endsWith(".yang")) { - foundFilesForReporting.add(entryName); - yangsFromDependencies.add(new YangSourceInZipFile(zip, entry)); - } - } - } - if (foundFilesForReporting.size() > 0) { - LOG.info("{} Found {} yang files in {}: {}", YangToSourcesProcessor.LOG_PREFIX, - foundFilesForReporting.size(), file, foundFilesForReporting); - } - - } - } catch (Exception e) { - throw new MojoFailureException(e.getMessage(), e); - } - return new YangsInZipsResult(yangsFromDependencies, zips); - } - - /** - * 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 - */ - static Collection findYangFilesInDependencies(MavenProject project) throws MojoFailureException { - final List yangsFilesFromDependencies = new ArrayList<>(); - - 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); - - 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); - if (yangDir.exists() && yangDir.isDirectory()) { - File[] yangFiles = yangDir.listFiles(new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.endsWith(".yang") && new File(dir, name).isFile(); - } - }); - - yangsFilesFromDependencies.addAll(Arrays.asList(yangFiles)); - } - } else { - try (ZipFile zip = new ZipFile(file)) { - - final Enumeration entries = zip.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - String entryName = entry.getName(); - - if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR) - && !entry.isDirectory() && entryName.endsWith(".yang")) { - LOG.debug("{} Found a YANG file in {}: {}", YangToSourcesProcessor.LOG_PREFIX, file, - entryName); - yangsFilesFromDependencies.add(file); - break; - } - } - } - } - } catch (Exception e) { - throw new MojoFailureException("Failed to scan for YANG files in dependency: " + file.toString(), e); - } - } - return yangsFilesFromDependencies; - } - - static final class ContextHolder { - private final SchemaContext context; - private final Set yangModules; - - ContextHolder(SchemaContext context, Set yangModules) { - this.context = context; - this.yangModules = yangModules; - } - - SchemaContext getContext() { - return context; - } - - Set getYangModules() { - return yangModules; - } - } - }