*/
package org.opendaylight.yangtools.yang2sources.plugin;
-import java.io.Closeable;
+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.io.ByteSource;
+import com.google.common.io.ByteStreams;
import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
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.plugin.logging.Log;
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.parser.util.NamedFileInputStream;
-import org.sonatype.aether.RepositorySystem;
-import org.sonatype.aether.RepositorySystemSession;
-import org.sonatype.aether.collection.CollectRequest;
-import org.sonatype.aether.graph.DependencyFilter;
-import org.sonatype.aether.repository.RemoteRepository;
-import org.sonatype.aether.resolution.ArtifactResult;
-import org.sonatype.aether.resolution.DependencyRequest;
-import org.sonatype.aether.resolution.DependencyResolutionException;
-import org.sonatype.aether.util.artifact.JavaScopes;
-import org.sonatype.aether.util.filter.DependencyFilterUtils;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
+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 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<File, Collection<File>> cache = Maps.newHashMapWithExpectedSize(CACHE_SIZE);
-
- /**
- * List files recursively and return as array of String paths. Use cache of
- * size 1.
- */
- static Collection<File> listFiles(File root) throws FileNotFoundException {
- if (cache.get(root) != null) {
- return cache.get(root);
- }
-
- if (!root.exists()) {
- throw new FileNotFoundException(root.toString());
- }
-
- Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
-
- toCache(root, yangFiles);
- return yangFiles;
- }
-
- static Collection<File> listFiles(File root, File[] excludedFiles, Log log) throws FileNotFoundException {
- if (!root.exists()) {
- throw new FileNotFoundException(root.toString());
- }
- Collection<File> result = new ArrayList<>();
- Collection<File> 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) {
- if (log != null) {
- log.info(Util.message("%s file excluded %s", YangToSourcesProcessor.LOG_PREFIX,
- Util.YANG_SUFFIX.toUpperCase(), f));
- }
- } else {
- result.add(f);
- }
- }
+ private static final Logger LOG = LoggerFactory.getLogger(Util.class);
- return result;
- }
-
- private static void toCache(final File rootDir, final Collection<File> yangFiles) {
- cache.put(rootDir, yangFiles);
- }
-
- /**
- * Instantiate object from fully qualified class name
- */
- static <T> T getInstance(String codeGeneratorClass, Class<T> 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 (!isImplemented(baseType, clazz)) {
- throw new IllegalArgumentException("Code generator " + clazz + " has to implement " + baseType);
- }
- return clazz;
- }
-
- private static boolean isImplemented(Class<?> expectedIface, Class<?> byClazz) {
- for (Class<?> iface : byClazz.getInterfaces()) {
- if (iface.equals(expectedIface)) {
- return true;
- }
- }
- return false;
- }
-
- static String message(String message, String logPrefix, Object... args) {
- String innerMessage = String.format(message, args);
- return String.format("%s %s", logPrefix, innerMessage);
- }
-
- static List<File> getClassPath(MavenProject project) {
- List<File> dependencies = Lists.newArrayList();
+ static List<File> getClassPath(final MavenProject project) {
+ final List<File> dependencies = new ArrayList<>();
for (Artifact element : project.getArtifacts()) {
File asFile = element.getFile();
if (isJar(asFile) || asFile.isDirectory()) {
*
* @param project
* current project
- * @param log
- * logger
* @param repoSystem
* repository system
- * @param repoSession
- * repository system session
+ * @param localRepo
+ * local repository
* @param remoteRepos
* remote repositories
*/
- static void checkClasspath(MavenProject project, Log log, RepositorySystem repoSystem,
- RepositorySystemSession repoSession, List<RemoteRepository> remoteRepos) {
+ static void checkClasspath(final MavenProject project, final RepositorySystem repoSystem,
+ final ArtifactRepository localRepo, final List<ArtifactRepository> remoteRepos) {
Plugin plugin = project.getPlugin(YangToSourcesMojo.PLUGIN_NAME);
if (plugin == null) {
- log.warn(message("%s 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<org.sonatype.aether.artifact.Artifact, List<org.sonatype.aether.artifact.Artifact>> pluginDependencies = new HashMap<>();
- getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, repoSession, remoteRepos, log);
+ Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
+ getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos);
Set<Artifact> projectDependencies = project.getDependencyArtifacts();
- for (Map.Entry<org.sonatype.aether.artifact.Artifact, List<org.sonatype.aether.artifact.Artifact>> entry : pluginDependencies
- .entrySet()) {
- checkArtifact(entry.getKey(), projectDependencies, log);
- for (org.sonatype.aether.artifact.Artifact dependency : entry.getValue()) {
- checkArtifact(dependency, projectDependencies, log);
+ for (Map.Entry<Artifact, Collection<Artifact>> entry : pluginDependencies.entrySet()) {
+ checkArtifact(entry.getKey(), projectDependencies);
+ for (Artifact dependency : entry.getValue()) {
+ checkArtifact(dependency, projectDependencies);
}
}
}
* map, where founded transitive dependencies will be stored
* @param repoSystem
* repository system
- * @param repoSession
- * repository system session
+ * @param localRepository
+ * local repository
* @param remoteRepos
* list of remote repositories
- * @param log
- * logger
*/
- private static void getPluginTransitiveDependencies(Plugin plugin,
- Map<org.sonatype.aether.artifact.Artifact, List<org.sonatype.aether.artifact.Artifact>> map,
- RepositorySystem repoSystem, RepositorySystemSession repoSession, List<RemoteRepository> remoteRepos,
- Log log) {
- for (Dependency dep : plugin.getDependencies()) {
- String artifactCoords = dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion();
- org.sonatype.aether.artifact.Artifact artifact = new org.sonatype.aether.util.artifact.DefaultArtifact(
- artifactCoords);
-
- if (!(map.containsKey(artifact))) {
- DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE);
- CollectRequest collectRequest = new CollectRequest();
- collectRequest.setRoot(new org.sonatype.aether.graph.Dependency(artifact, JavaScopes.COMPILE));
- collectRequest.setRepositories(remoteRepos);
- DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFlter);
-
- List<ArtifactResult> artifactResults;
- try {
- artifactResults = repoSystem.resolveDependencies(repoSession, dependencyRequest)
- .getArtifactResults();
- List<org.sonatype.aether.artifact.Artifact> deps = new ArrayList<>();
- for (ArtifactResult ar : artifactResults) {
- deps.add(ar.getArtifact());
- }
- map.put(artifact, deps);
- } catch (DependencyResolutionException e) {
- log.warn(message("Failed to resolve plugin dependencies, version check skipped",
- YangToSourcesProcessor.LOG_PREFIX));
- }
- }
+ private static void getPluginTransitiveDependencies(final Plugin plugin,
+ final Map<Artifact, Collection<Artifact>> map, final RepositorySystem repoSystem,
+ final ArtifactRepository localRepository, final List<ArtifactRepository> remoteRepos) {
+
+ List<Dependency> pluginDependencies = plugin.getDependencies();
+ for (Dependency dep : pluginDependencies) {
+ Artifact artifact = repoSystem.createDependencyArtifact(dep);
+
+ ArtifactResolutionRequest request = new ArtifactResolutionRequest();
+ request.setArtifact(artifact);
+ request.setResolveTransitively(true);
+ request.setLocalRepository(localRepository);
+ request.setRemoteRepositories(remoteRepos);
+
+ ArtifactResolutionResult result = repoSystem.resolve(request);
+ Set<Artifact> pluginDependencyDependencies = result.getArtifacts();
+ map.put(artifact, pluginDependencyDependencies);
}
}
* artifact to check
* @param dependencies
* collection of dependencies
- * @param log
- * logger
*/
- private static void checkArtifact(org.sonatype.aether.artifact.Artifact artifact, Collection<Artifact> dependencies,
- Log log) {
- for (Artifact d : dependencies) {
+ private static void checkArtifact(final Artifact artifact, final Collection<Artifact> 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(message("Dependency resolution conflict:", YangToSourcesProcessor.LOG_PREFIX));
- log.warn(message("'%s' dependency [%s] has different version than one "
- + "declared in current project [%s]. 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)) ? true : false;
- }
-
- static <T> T checkNotNull(T obj, String paramName) {
- return Preconditions.checkNotNull(obj, "Parameter " + paramName + " is null");
- }
-
- static final class YangsInZipsResult implements Closeable {
- private final List<InputStream> yangStreams;
- private final List<Closeable> zipInputStreams;
-
- private YangsInZipsResult(List<InputStream> yangStreams, List<Closeable> zipInputStreams) {
- this.yangStreams = yangStreams;
- this.zipInputStreams = zipInputStreams;
- }
-
- @Override
- public void close() throws IOException {
- for (InputStream is : yangStreams) {
- is.close();
- }
- for (Closeable is : zipInputStreams) {
- is.close();
- }
- }
-
- public List<InputStream> getYangStreams() {
- return this.yangStreams;
- }
+ private static boolean isJar(final File element) {
+ return element.isFile() && element.getName().endsWith(".jar");
}
- static YangsInZipsResult findYangFilesInDependenciesAsStream(Log log, MavenProject project)
+ @SuppressWarnings("checkstyle:illegalCatch")
+ static List<YangTextSchemaSource> findYangFilesInDependenciesAsStream(final MavenProject project)
throws MojoFailureException {
- List<InputStream> yangsFromDependencies = new ArrayList<>();
- List<Closeable> zips = new ArrayList<>();
try {
- List<File> filesOnCp = Util.getClassPath(project);
- log.info(Util.message("Searching for yang files in following dependencies: %s",
- YangToSourcesProcessor.LOG_PREFIX, filesOnCp));
+ final List<File> filesOnCp = Util.getClassPath(project);
+ LOG.info("{} Searching for yang files in following dependencies: {}", LOG_PREFIX, filesOnCp);
+ final List<YangTextSchemaSource> yangsFromDependencies = new ArrayList<>();
for (File file : filesOnCp) {
- List<String> foundFilesForReporting = new ArrayList<>();
+ final List<String> foundFilesForReporting = new ArrayList<>();
// is it jar file or directory?
if (file.isDirectory()) {
- File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING);
+ //FIXME: code duplicate
+ File yangDir = new File(file, 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, YangToSourcesProcessor.META_INF_YANG_STRING + File.separator + yangFile.getName()));
+ File[] yangFiles = yangDir.listFiles(
+ (dir, name) -> name.endsWith(RFC6020_YANG_FILE_EXTENSION) && new File(dir, name).isFile());
+ for (final File yangFile : yangFiles) {
+ 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<? extends ZipEntry> entries = zip.entries();
+ while (entries.hasMoreElements()) {
+ final ZipEntry entry = entries.nextElement();
+ final String entryName = entry.getName();
- Enumeration<? extends ZipEntry> 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(".yang")) {
- foundFilesForReporting.add(entryName);
- // This will be closed after all streams are
- // parsed.
- InputStream entryStream = zip.getInputStream(entry);
- yangsFromDependencies.add(entryStream);
+ yangsFromDependencies.add(YangTextSchemaSource.delegateForByteSource(
+ entryName.substring(entryName.lastIndexOf('/') + 1),
+ ByteSource.wrap(ByteStreams.toByteArray(zip.getInputStream(entry)))));
+ }
}
}
}
if (foundFilesForReporting.size() > 0) {
- log.info(Util.message("Found %d yang files in %s: %s", 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);
}
- static Collection<File> findYangFilesInDependencies(Log log, MavenProject project) throws MojoFailureException {
+ /**
+ * 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.
+ @SuppressWarnings("checkstyle:illegalCatch")
+ static Collection<File> findYangFilesInDependencies(final MavenProject project) throws MojoFailureException {
final List<File> yangsFilesFromDependencies = new ArrayList<>();
+ final List<File> filesOnCp;
try {
- List<File> filesOnCp = Util.getClassPath(project);
- log.info(Util.message("Searching for yang files in following dependencies: %s",
- YangToSourcesProcessor.LOG_PREFIX, filesOnCp));
+ 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: {}", LOG_PREFIX, filesOnCp);
- for (File file : filesOnCp) {
+ for (File file : filesOnCp) {
+ try {
// is it jar file or directory?
if (file.isDirectory()) {
- File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING);
+ //FIXME: code duplicate
+ File yangDir = new File(file, 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();
- }
- });
+ File[] yangFiles = yangDir.listFiles(
+ (dir, name) -> name.endsWith(RFC6020_YANG_FILE_EXTENSION) && new File(dir, name).isFile());
yangsFilesFromDependencies.addAll(Arrays.asList(yangFiles));
}
ZipEntry entry = entries.nextElement();
String entryName = entry.getName();
- if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
- && !entry.isDirectory() && entryName.endsWith(".yang")) {
- log.debug(Util.message("Found a YANG file in %s: %s", YangToSourcesProcessor.LOG_PREFIX,
- file, entryName));
+ if (entryName.startsWith(META_INF_YANG_STRING_JAR)
+ && !entry.isDirectory() && entryName.endsWith(RFC6020_YANG_FILE_EXTENSION)) {
+ LOG.debug("{} Found a YANG file in {}: {}", 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);
}
- } catch (Exception e) {
- throw new MojoFailureException("Failed to scan for YANG files in depedencies", e);
}
return yangsFilesFromDependencies;
}
- static final class ContextHolder {
- private final SchemaContext context;
- private final Set<Module> yangModules;
-
- ContextHolder(SchemaContext context, Set<Module> yangModules) {
- this.context = context;
- this.yangModules = yangModules;
- }
-
- SchemaContext getContext() {
- return context;
- }
-
- Set<Module> getYangModules() {
- return yangModules;
- }
+ static SourceIdentifier moduleToIdentifier(final Module module) {
+ return RevisionSourceIdentifier.create(module.getName(), module.getRevision());
}
-
}