2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang2sources.plugin;
10 import java.io.Closeable;
12 import java.io.FileNotFoundException;
13 import java.io.FilenameFilter;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Enumeration;
21 import java.util.HashMap;
22 import java.util.List;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
28 import org.apache.commons.io.FileUtils;
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
32 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
33 import org.apache.maven.model.Dependency;
34 import org.apache.maven.model.Plugin;
35 import org.apache.maven.plugin.MojoFailureException;
36 import org.apache.maven.plugin.logging.Log;
37 import org.apache.maven.project.MavenProject;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
41 import org.apache.maven.repository.RepositorySystem;
43 import com.google.common.base.Preconditions;
44 import com.google.common.collect.Lists;
45 import com.google.common.collect.Maps;
50 * It isn't desirable to create instances of this class
55 static final String YANG_SUFFIX = "yang";
57 private static final int CACHE_SIZE = 10;
58 // Cache for listed directories and found yang files. Typically yang files
59 // are utilized twice. First: code is generated during generate-sources
60 // phase Second: yang files are copied as resources during
61 // generate-resources phase. This cache ensures that yang files are listed
63 private static Map<File, Collection<File>> cache = Maps.newHashMapWithExpectedSize(CACHE_SIZE);
66 * List files recursively and return as array of String paths. Use cache of
69 static Collection<File> listFiles(File root) throws FileNotFoundException {
70 if (cache.get(root) != null) {
71 return cache.get(root);
75 throw new FileNotFoundException(root.toString());
78 Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
80 toCache(root, yangFiles);
84 static Collection<File> listFiles(File root, File[] excludedFiles, Log log) throws FileNotFoundException {
87 log.warn(Util.message("YANG source directory %s not found. No code will be generated.", YangToSourcesProcessor.LOG_PREFIX, root.toString()));
89 return Collections.emptyList();
91 Collection<File> result = new ArrayList<>();
92 Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
93 for (File f : yangFiles) {
94 boolean excluded = false;
95 for (File ex : excludedFiles) {
103 log.info(Util.message("%s file excluded %s", YangToSourcesProcessor.LOG_PREFIX,
104 Util.YANG_SUFFIX.toUpperCase(), f));
114 private static void toCache(final File rootDir, final Collection<File> yangFiles) {
115 cache.put(rootDir, yangFiles);
119 * Instantiate object from fully qualified class name
121 static <T> T getInstance(String codeGeneratorClass, Class<T> baseType) throws ClassNotFoundException,
122 InstantiationException, IllegalAccessException {
123 return baseType.cast(resolveClass(codeGeneratorClass, baseType).newInstance());
126 private static Class<?> resolveClass(String codeGeneratorClass, Class<?> baseType) throws ClassNotFoundException {
127 Class<?> clazz = Class.forName(codeGeneratorClass);
129 if (!isImplemented(baseType, clazz)) {
130 throw new IllegalArgumentException("Code generator " + clazz + " has to implement " + baseType);
135 private static boolean isImplemented(Class<?> expectedIface, Class<?> byClazz) {
136 for (Class<?> iface : byClazz.getInterfaces()) {
137 if (iface.equals(expectedIface)) {
144 static String message(String message, String logPrefix, Object... args) {
145 String innerMessage = String.format(message, args);
146 return String.format("%s %s", logPrefix, innerMessage);
149 static List<File> getClassPath(MavenProject project) {
150 List<File> dependencies = Lists.newArrayList();
151 for (Artifact element : project.getArtifacts()) {
152 File asFile = element.getFile();
153 if (isJar(asFile) || asFile.isDirectory()) {
154 dependencies.add(asFile);
161 * Read current project dependencies and check if it don't grab incorrect
162 * artifacts versions which could be in conflict with plugin dependencies.
171 * remote repositories
175 static void checkClasspath(MavenProject project, RepositorySystem repoSystem, ArtifactRepository localRepo,
176 List<ArtifactRepository> remoteRepos, Log log) {
177 Plugin plugin = project.getPlugin(YangToSourcesMojo.PLUGIN_NAME);
178 if (plugin == null) {
179 log.warn(message("%s not found, dependencies version check skipped", YangToSourcesProcessor.LOG_PREFIX,
180 YangToSourcesMojo.PLUGIN_NAME));
182 Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
183 getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos, log);
185 Set<Artifact> projectDependencies = project.getDependencyArtifacts();
186 for (Map.Entry<Artifact, Collection<Artifact>> entry : pluginDependencies.entrySet()) {
187 checkArtifact(entry.getKey(), projectDependencies, log);
188 for (Artifact dependency : entry.getValue()) {
189 checkArtifact(dependency, projectDependencies, log);
196 * Read transitive dependencies of given plugin and store them in map.
201 * map, where founded transitive dependencies will be stored
204 * @param localRepository
207 * list of remote repositories
211 private static void getPluginTransitiveDependencies(Plugin plugin, Map<Artifact, Collection<Artifact>> map,
212 RepositorySystem repoSystem, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepos,
215 List<Dependency> pluginDependencies = plugin.getDependencies();
216 for (Dependency dep : pluginDependencies) {
217 Artifact artifact = repoSystem.createDependencyArtifact(dep);
219 ArtifactResolutionRequest request = new ArtifactResolutionRequest();
220 request.setArtifact(artifact);
221 request.setResolveTransitively(true);
222 request.setLocalRepository(localRepository);
223 request.setRemoteRepositories(remoteRepos);
225 ArtifactResolutionResult result = repoSystem.resolve(request);
226 Set<Artifact> pluginDependencyDependencies = result.getArtifacts();
227 map.put(artifact, pluginDependencyDependencies);
232 * Check artifact against collection of dependencies. If collection contains
233 * artifact with same groupId and artifactId, but different version, logs a
238 * @param dependencies
239 * collection of dependencies
243 private static void checkArtifact(Artifact artifact, Collection<Artifact> dependencies, Log log) {
244 for (org.apache.maven.artifact.Artifact d : dependencies) {
245 if (artifact.getGroupId().equals(d.getGroupId()) && artifact.getArtifactId().equals(d.getArtifactId())) {
246 if (!(artifact.getVersion().equals(d.getVersion()))) {
247 log.warn(message("Dependency resolution conflict:", YangToSourcesProcessor.LOG_PREFIX));
248 log.warn(message("'%s' dependency [%s] has different version than one "
249 + "declared in current project [%s]. It is recommended to fix this problem "
250 + "because it may cause compilation errors.", YangToSourcesProcessor.LOG_PREFIX,
251 YangToSourcesMojo.PLUGIN_NAME, artifact, d));
257 private static final String JAR_SUFFIX = ".jar";
259 private static boolean isJar(File element) {
260 return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true : false;
263 static <T> T checkNotNull(T obj, String paramName) {
264 return Preconditions.checkNotNull(obj, "Parameter " + paramName + " is null");
267 static final class YangsInZipsResult implements Closeable {
268 private final List<InputStream> yangStreams;
269 private final List<Closeable> zipInputStreams;
271 private YangsInZipsResult(List<InputStream> yangStreams, List<Closeable> zipInputStreams) {
272 this.yangStreams = yangStreams;
273 this.zipInputStreams = zipInputStreams;
277 public void close() throws IOException {
278 for (InputStream is : yangStreams) {
281 for (Closeable is : zipInputStreams) {
286 public List<InputStream> getYangStreams() {
287 return this.yangStreams;
291 static YangsInZipsResult findYangFilesInDependenciesAsStream(Log log, MavenProject project)
292 throws MojoFailureException {
293 List<InputStream> yangsFromDependencies = new ArrayList<>();
294 List<Closeable> zips = new ArrayList<>();
296 List<File> filesOnCp = Util.getClassPath(project);
297 log.info(Util.message("Searching for yang files in following dependencies: %s",
298 YangToSourcesProcessor.LOG_PREFIX, filesOnCp));
300 for (File file : filesOnCp) {
301 List<String> foundFilesForReporting = new ArrayList<>();
302 // is it jar file or directory?
303 if (file.isDirectory()) {
304 //FIXME: code duplicate
305 File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING);
306 if (yangDir.exists() && yangDir.isDirectory()) {
307 File[] yangFiles = yangDir.listFiles(new FilenameFilter() {
309 public boolean accept(File dir, String name) {
310 return name.endsWith(".yang") && new File(dir, name).isFile();
313 for (File yangFile : yangFiles) {
314 yangsFromDependencies.add(new NamedFileInputStream(yangFile, YangToSourcesProcessor.META_INF_YANG_STRING + File.separator + yangFile.getName()));
319 ZipFile zip = new ZipFile(file);
322 Enumeration<? extends ZipEntry> entries = zip.entries();
323 while (entries.hasMoreElements()) {
324 ZipEntry entry = entries.nextElement();
325 String entryName = entry.getName();
327 if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
328 && !entry.isDirectory() && entryName.endsWith(".yang")) {
329 foundFilesForReporting.add(entryName);
330 // This will be closed after all streams are
332 InputStream entryStream = zip.getInputStream(entry);
333 yangsFromDependencies.add(entryStream);
337 if (foundFilesForReporting.size() > 0) {
338 log.info(Util.message("Found %d yang files in %s: %s", YangToSourcesProcessor.LOG_PREFIX,
339 foundFilesForReporting.size(), file, foundFilesForReporting));
343 } catch (Exception e) {
344 throw new MojoFailureException(e.getMessage(), e);
346 return new YangsInZipsResult(yangsFromDependencies, zips);
349 static Collection<File> findYangFilesInDependencies(Log log, MavenProject project) throws MojoFailureException {
350 final List<File> yangsFilesFromDependencies = new ArrayList<>();
353 List<File> filesOnCp = Util.getClassPath(project);
354 log.info(Util.message("Searching for yang files in following dependencies: %s",
355 YangToSourcesProcessor.LOG_PREFIX, filesOnCp));
357 for (File file : filesOnCp) {
358 // is it jar file or directory?
359 if (file.isDirectory()) {
360 //FIXME: code duplicate
361 File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING);
362 if (yangDir.exists() && yangDir.isDirectory()) {
363 File[] yangFiles = yangDir.listFiles(new FilenameFilter() {
365 public boolean accept(File dir, String name) {
366 return name.endsWith(".yang") && new File(dir, name).isFile();
370 yangsFilesFromDependencies.addAll(Arrays.asList(yangFiles));
373 try (ZipFile zip = new ZipFile(file)) {
375 final Enumeration<? extends ZipEntry> entries = zip.entries();
376 while (entries.hasMoreElements()) {
377 ZipEntry entry = entries.nextElement();
378 String entryName = entry.getName();
380 if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
381 && !entry.isDirectory() && entryName.endsWith(".yang")) {
382 log.debug(Util.message("Found a YANG file in %s: %s", YangToSourcesProcessor.LOG_PREFIX,
384 yangsFilesFromDependencies.add(file);
391 } catch (Exception e) {
392 throw new MojoFailureException("Failed to scan for YANG files in depedencies", e);
394 return yangsFilesFromDependencies;
397 static final class ContextHolder {
398 private final SchemaContext context;
399 private final Set<Module> yangModules;
401 ContextHolder(SchemaContext context, Set<Module> yangModules) {
402 this.context = context;
403 this.yangModules = yangModules;
406 SchemaContext getContext() {
410 Set<Module> getYangModules() {