X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fmaven-yang-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang2sources%2Fplugin%2FUtil.java;h=e3b0b31215bef2a6deac7457d152d19ec81d1115;hb=ff1b4a79cca00743a00c3b0b1100bd0ab2b2fb31;hp=5676530afec2eaecedab1469608654abb3ebc675;hpb=a640c5c549376e5d72038e033d49ef6f0df96c92;p=controller.git diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java index 5676530afe..e3b0b31215 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java @@ -7,9 +7,11 @@ */ package org.opendaylight.controller.yang2sources.plugin; +import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -17,16 +19,19 @@ import java.util.Collection; import java.util.Enumeration; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.zip.ZipEntry; -import java.util.zip.ZipException; import java.util.zip.ZipFile; import org.apache.commons.io.FileUtils; -import org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; +import org.opendaylight.controller.yang.model.api.Module; +import org.opendaylight.controller.yang.model.api.SchemaContext; -import com.google.common.base.Function; -import com.google.common.collect.Collections2; +import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -38,57 +43,55 @@ final class Util { // 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 + private static Map> cache = Maps .newHashMapWithExpectedSize(10); /** * List files recursively and return as array of String paths. Use cache of * size 1. */ - static Collection listFiles(String rootDir) throws FileNotFoundException { - if (cache.get(rootDir) != null) - return cache.get(rootDir); + static Collection listFiles(File root) throws FileNotFoundException { + if (cache.get(root) != null) + return cache.get(root); - File file = new File(rootDir); - if(!file.exists()) { - throw new FileNotFoundException(); + if (!root.exists()) { + throw new FileNotFoundException(root.toString()); } - Collection yangFiles = FileUtils.listFiles(new File(rootDir), + Collection yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true); - toCache(rootDir, yangFiles); + toCache(root, yangFiles); return yangFiles; } - static Collection listFilesAsStream(String rootDir) throws FileNotFoundException { - Collection is = new ArrayList(); + static List listFilesAsStream(File rootDir) + throws FileNotFoundException { + List is = new ArrayList(); Collection files = listFiles(rootDir); - for(File f : files) { - is.add(new FileInputStream(f)); + for (File f : files) { + is.add(new NamedFileInputStream(f)); } return is; } - static String[] listFilesAsArrayOfPaths(String rootDir) throws FileNotFoundException { - String[] filesArray = new String[] {}; - Collection yangFiles = listFiles(rootDir); + static class NamedFileInputStream extends FileInputStream { + private final File file; - // If collection is empty, return empty array [] rather then [null] - // array, that is created by default - return yangFiles.isEmpty() ? filesArray : Collections2.transform( - yangFiles, new Function() { + NamedFileInputStream(File file) throws FileNotFoundException { + super(file); + this.file = file; + } - @Override - public String apply(File input) { - return input.getPath(); - } - }).toArray(filesArray); + @Override + public String toString() { + return getClass().getSimpleName() + "{" + file + "}"; + } } - private static void toCache(final String rootDir, + private static void toCache(final File rootDir, final Collection yangFiles) { cache.put(rootDir, yangFiles); } @@ -127,18 +130,13 @@ final class Util { return String.format("%s %s", logPrefix, innerMessage); } - public static List getClassPath(MavenProject project) - throws DependencyResolutionRequiredException { + static List getClassPath(MavenProject project) { List dependencies = Lists.newArrayList(); - try { - for (Object element : project.getCompileClasspathElements()) { - File asFile = new File((String) element); - if (isJar(asFile)) { - dependencies.add(asFile); - } + for (Artifact element : project.getArtifacts()) { + File asFile = element.getFile(); + if (isJar(asFile) || asFile.isDirectory()) { + dependencies.add(asFile); } - } catch (DependencyResolutionRequiredException e) { - throw e; } return dependencies; } @@ -150,32 +148,117 @@ final class Util { : false; } - public static Collection getFilesFromClasspath( - List jarsOnClasspath, List classPathFilter) - throws ZipException, IOException { - List yangs = Lists.newArrayList(); - - for (File file : jarsOnClasspath) { - ZipFile zip = new ZipFile(file); - Enumeration entries = zip.entries(); - while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); - if (entry.getName().endsWith(YANG_SUFFIX)) { - InputStream stream = zip.getInputStream(entry); - } - } + static T checkNotNull(T obj, String paramName) { + return Preconditions.checkNotNull(obj, "Parameter " + paramName + + " is null"); + } + + final static class YangsInZipsResult implements Closeable { + final List yangStreams; + private final List zipInputStreams; + + private YangsInZipsResult(List yangStreams, + List zipInputStreams) { + this.yangStreams = yangStreams; + this.zipInputStreams = zipInputStreams; } - return yangs; + @Override + public void close() throws IOException { + for (InputStream is : yangStreams) { + is.close(); + } + for (Closeable is : zipInputStreams) { + is.close(); + } + } } - public static boolean acceptedFilter(String name, List filter) { - for(String f : filter) { - if(name.endsWith(f)) { - return true; + static YangsInZipsResult findYangFilesInDependenciesAsStream(Log log, + MavenProject project) + throws MojoFailureException { + List yangsFromDependencies = new ArrayList<>(); + List zips = new ArrayList<>(); + try { + List filesOnCp = Util.getClassPath(project); + log.info(Util.message( + "Searching for yang files in following dependencies: %s", + YangToSourcesProcessor.LOG_PREFIX, filesOnCp)); + + for (File file : filesOnCp) { + List foundFilesForReporting = new ArrayList<>(); + // is it jar file or directory? + if (file.isDirectory()) { + 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 (File yangFile : yangFiles) { + yangsFromDependencies.add(new NamedFileInputStream( + 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)) { + if (entry.isDirectory() == false + && entryName.endsWith(".yang")) { + foundFilesForReporting.add(entryName); + // This will be closed after all strams are + // parsed. + InputStream entryStream = zip + .getInputStream(entry); + yangsFromDependencies.add(entryStream); + } + } + } + } + if (foundFilesForReporting.size() > 0) { + log.info(Util.message("Found %d yang files in %s: %s", + YangToSourcesProcessor.LOG_PREFIX, + foundFilesForReporting.size(), file, + foundFilesForReporting)); + } + } + } catch (Exception e) { + throw new MojoFailureException(e.getMessage(), e); + } + return new YangsInZipsResult(yangsFromDependencies, zips); + } + + final static 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; } - return false; } }