Improve yang-maven-plugin error reporting
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / Util.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang2sources.plugin;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import com.google.common.collect.Maps;
13 import java.io.Closeable;
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.io.FilenameFilter;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Enumeration;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipFile;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.artifact.repository.ArtifactRepository;
32 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
33 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
34 import org.apache.maven.model.Dependency;
35 import org.apache.maven.model.Plugin;
36 import org.apache.maven.plugin.MojoFailureException;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.repository.RepositorySystem;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 final class Util {
45
46     /**
47      * It isn't desirable to create instances of this class
48      */
49     private Util() {
50     }
51
52     static final String YANG_SUFFIX = "yang";
53
54     private static final Logger LOG = LoggerFactory.getLogger(Util.class);
55     private static final int CACHE_SIZE = 10;
56     // Cache for listed directories and found yang files. Typically yang files
57     // are utilized twice. First: code is generated during generate-sources
58     // phase Second: yang files are copied as resources during
59     // generate-resources phase. This cache ensures that yang files are listed
60     // only once.
61     private static Map<File, Collection<File>> cache = Maps.newHashMapWithExpectedSize(CACHE_SIZE);
62
63     /**
64      * List files recursively and return as array of String paths. Use cache of
65      * size 1.
66      */
67     static Collection<File> listFiles(File root) throws FileNotFoundException {
68         if (cache.get(root) != null) {
69             return cache.get(root);
70         }
71
72         if (!root.exists()) {
73             throw new FileNotFoundException(root.toString());
74         }
75
76         Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
77
78         toCache(root, yangFiles);
79         return yangFiles;
80     }
81
82     static Collection<File> listFiles(File root, File[] excludedFiles) throws FileNotFoundException {
83         if (!root.exists()) {
84             LOG.warn("{} YANG source directory {} not found. No code will be generated.", YangToSourcesProcessor
85                     .LOG_PREFIX, root.toString());
86
87             return Collections.emptyList();
88         }
89         Collection<File> result = new ArrayList<>();
90         Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
91         for (File f : yangFiles) {
92             boolean excluded = false;
93             for (File ex : excludedFiles) {
94                 if (ex.equals(f)) {
95                     excluded = true;
96                     break;
97                 }
98             }
99             if (excluded) {
100                 LOG.info("{} {} file excluded {}", YangToSourcesProcessor.LOG_PREFIX, Util.YANG_SUFFIX.toUpperCase(),
101                         f);
102             } else {
103                 result.add(f);
104             }
105         }
106
107         return result;
108     }
109
110     private static void toCache(final File rootDir, final Collection<File> yangFiles) {
111         cache.put(rootDir, yangFiles);
112     }
113
114     /**
115      * Instantiate object from fully qualified class name
116      */
117     static <T> T getInstance(String codeGeneratorClass, Class<T> baseType) throws ClassNotFoundException,
118             InstantiationException, IllegalAccessException {
119         return baseType.cast(resolveClass(codeGeneratorClass, baseType).newInstance());
120     }
121
122     private static Class<?> resolveClass(String codeGeneratorClass, Class<?> baseType) throws ClassNotFoundException {
123         Class<?> clazz = Class.forName(codeGeneratorClass);
124
125         if (!baseType.isAssignableFrom(clazz)) {
126             throw new IllegalArgumentException("Code generator " + clazz + " has to implement " + baseType);
127         }
128         return clazz;
129     }
130
131     static List<File> getClassPath(MavenProject project) {
132         List<File> dependencies = Lists.newArrayList();
133         for (Artifact element : project.getArtifacts()) {
134             File asFile = element.getFile();
135             if (isJar(asFile) || asFile.isDirectory()) {
136                 dependencies.add(asFile);
137             }
138         }
139         return dependencies;
140     }
141
142     /**
143      * Read current project dependencies and check if it don't grab incorrect
144      * artifacts versions which could be in conflict with plugin dependencies.
145      *
146      * @param project
147      *            current project
148      * @param repoSystem
149      *            repository system
150      * @param localRepo
151      *            local repository
152      * @param remoteRepos
153      *            remote repositories
154      */
155     static void checkClasspath(MavenProject project, RepositorySystem repoSystem, ArtifactRepository localRepo,
156             List<ArtifactRepository> remoteRepos) {
157         Plugin plugin = project.getPlugin(YangToSourcesMojo.PLUGIN_NAME);
158         if (plugin == null) {
159             LOG.warn("{} {} not found, dependencies version check skipped", YangToSourcesProcessor.LOG_PREFIX,
160                     YangToSourcesMojo.PLUGIN_NAME);
161         } else {
162             Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
163             getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos);
164
165             Set<Artifact> projectDependencies = project.getDependencyArtifacts();
166             for (Map.Entry<Artifact, Collection<Artifact>> entry : pluginDependencies.entrySet()) {
167                 checkArtifact(entry.getKey(), projectDependencies);
168                 for (Artifact dependency : entry.getValue()) {
169                     checkArtifact(dependency, projectDependencies);
170                 }
171             }
172         }
173     }
174
175     /**
176      * Read transitive dependencies of given plugin and store them in map.
177      *
178      * @param plugin
179      *            plugin to read
180      * @param map
181      *            map, where founded transitive dependencies will be stored
182      * @param repoSystem
183      *            repository system
184      * @param localRepository
185      *            local repository
186      * @param remoteRepos
187      *            list of remote repositories
188      */
189     private static void getPluginTransitiveDependencies(Plugin plugin, Map<Artifact, Collection<Artifact>> map,
190             RepositorySystem repoSystem, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepos) {
191
192         List<Dependency> pluginDependencies = plugin.getDependencies();
193         for (Dependency dep : pluginDependencies) {
194             Artifact artifact = repoSystem.createDependencyArtifact(dep);
195
196             ArtifactResolutionRequest request = new ArtifactResolutionRequest();
197             request.setArtifact(artifact);
198             request.setResolveTransitively(true);
199             request.setLocalRepository(localRepository);
200             request.setRemoteRepositories(remoteRepos);
201
202             ArtifactResolutionResult result = repoSystem.resolve(request);
203             Set<Artifact> pluginDependencyDependencies = result.getArtifacts();
204             map.put(artifact, pluginDependencyDependencies);
205         }
206     }
207
208     /**
209      * Check artifact against collection of dependencies. If collection contains
210      * artifact with same groupId and artifactId, but different version, logs a
211      * warning.
212      *
213      * @param artifact
214      *            artifact to check
215      * @param dependencies
216      *            collection of dependencies
217      */
218     private static void checkArtifact(Artifact artifact, Collection<Artifact> dependencies) {
219         for (org.apache.maven.artifact.Artifact d : dependencies) {
220             if (artifact.getGroupId().equals(d.getGroupId()) && artifact.getArtifactId().equals(d.getArtifactId())) {
221                 if (!(artifact.getVersion().equals(d.getVersion()))) {
222                     LOG.warn("{} Dependency resolution conflict:", YangToSourcesProcessor.LOG_PREFIX);
223                     LOG.warn("{} '{}' dependency [{}] has different version than one declared in current project [{}]" +
224                                     ". It is recommended to fix this problem because it may cause compilation errors.",
225                             YangToSourcesProcessor.LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME, artifact, d);
226                 }
227             }
228         }
229     }
230
231     private static final String JAR_SUFFIX = ".jar";
232
233     private static boolean isJar(File element) {
234         return (element.isFile() && element.getName().endsWith(JAR_SUFFIX));
235     }
236
237     static <T> T checkNotNull(T obj, String paramName) {
238         return Preconditions.checkNotNull(obj, "Parameter " + paramName + " is null");
239     }
240
241     static final class YangsInZipsResult implements Closeable {
242         private final List<YangSourceFromDependency> yangStreams;
243         private final List<Closeable> zipInputStreams;
244
245         private YangsInZipsResult(List<YangSourceFromDependency> yangStreams, List<Closeable> zipInputStreams) {
246             this.yangStreams = yangStreams;
247             this.zipInputStreams = zipInputStreams;
248         }
249
250         @Override
251         public void close() throws IOException {
252             for (Closeable is : zipInputStreams) {
253                 is.close();
254             }
255         }
256
257         public List<YangSourceFromDependency> getYangStreams() {
258             return this.yangStreams;
259         }
260     }
261
262     static YangsInZipsResult findYangFilesInDependenciesAsStream(MavenProject project)
263             throws MojoFailureException {
264         List<YangSourceFromDependency> yangsFromDependencies = new ArrayList<>();
265         List<Closeable> zips = new ArrayList<>();
266         try {
267             List<File> filesOnCp = Util.getClassPath(project);
268             LOG.info("{} Searching for yang files in following dependencies: {}", YangToSourcesProcessor.LOG_PREFIX,
269                     filesOnCp);
270
271             for (File file : filesOnCp) {
272                 List<String> foundFilesForReporting = new ArrayList<>();
273                 // is it jar file or directory?
274                 if (file.isDirectory()) {
275                     //FIXME: code duplicate
276                     File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING);
277                     if (yangDir.exists() && yangDir.isDirectory()) {
278                         File[] yangFiles = yangDir.listFiles(new FilenameFilter() {
279                             @Override
280                             public boolean accept(File dir, String name) {
281                                 return name.endsWith(".yang") && new File(dir, name).isFile();
282                             }
283                         });
284                         for (final File yangFile : yangFiles) {
285                             yangsFromDependencies.add(new YangSourceFromFile(yangFile));
286                         }
287                     }
288
289                 } else {
290                     ZipFile zip = new ZipFile(file);
291                     zips.add(zip);
292
293                     Enumeration<? extends ZipEntry> entries = zip.entries();
294                     while (entries.hasMoreElements()) {
295                         ZipEntry entry = entries.nextElement();
296                         String entryName = entry.getName();
297
298                         if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
299                                 && !entry.isDirectory() && entryName.endsWith(".yang")) {
300                             foundFilesForReporting.add(entryName);
301                             yangsFromDependencies.add(new YangSourceInZipFile(zip, entry));
302                         }
303                     }
304                 }
305                 if (foundFilesForReporting.size() > 0) {
306                     LOG.info("{} Found {} yang files in {}: {}", YangToSourcesProcessor.LOG_PREFIX,
307                             foundFilesForReporting.size(), file, foundFilesForReporting);
308                 }
309
310             }
311         } catch (Exception e) {
312             throw new MojoFailureException(e.getMessage(), e);
313         }
314         return new YangsInZipsResult(yangsFromDependencies, zips);
315     }
316
317     /**
318      * Find all dependencies which contains yang sources
319      *
320      * Returns collection of YANG files and Zip files which contains YANG files.
321      *
322      * FIXME: Rename to what class is actually doing.
323      *
324      * @param project
325      * @return
326      * @throws MojoFailureException
327      */
328     static Collection<File> findYangFilesInDependencies(MavenProject project) throws MojoFailureException {
329         final List<File> yangsFilesFromDependencies = new ArrayList<>();
330
331         List<File> filesOnCp;
332         try {
333             filesOnCp = Util.getClassPath(project);
334         } catch (Exception e) {
335             throw new MojoFailureException("Failed to scan for YANG files in dependencies", e);
336         }
337         LOG.info("{} Searching for yang files in following dependencies: {}", YangToSourcesProcessor.LOG_PREFIX,
338             filesOnCp);
339
340         for (File file : filesOnCp) {
341             try {
342                 // is it jar file or directory?
343                 if (file.isDirectory()) {
344                     //FIXME: code duplicate
345                     File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING);
346                     if (yangDir.exists() && yangDir.isDirectory()) {
347                         File[] yangFiles = yangDir.listFiles(new FilenameFilter() {
348                             @Override
349                             public boolean accept(File dir, String name) {
350                                 return name.endsWith(".yang") && new File(dir, name).isFile();
351                             }
352                         });
353
354                         yangsFilesFromDependencies.addAll(Arrays.asList(yangFiles));
355                     }
356                 } else {
357                     try (ZipFile zip = new ZipFile(file)) {
358
359                         final Enumeration<? extends ZipEntry> entries = zip.entries();
360                         while (entries.hasMoreElements()) {
361                             ZipEntry entry = entries.nextElement();
362                             String entryName = entry.getName();
363
364                             if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
365                                     && !entry.isDirectory() && entryName.endsWith(".yang")) {
366                                 LOG.debug("{} Found a YANG file in {}: {}", YangToSourcesProcessor.LOG_PREFIX, file,
367                                         entryName);
368                                 yangsFilesFromDependencies.add(file);
369                                 break;
370                             }
371                         }
372                     }
373                 }
374             } catch (Exception e) {
375                 throw new MojoFailureException("Failed to scan for YANG files in dependency: " + file.toString(), e);
376             }
377         }
378         return yangsFilesFromDependencies;
379     }
380
381     static final class ContextHolder {
382         private final SchemaContext context;
383         private final Set<Module> yangModules;
384
385         ContextHolder(SchemaContext context, Set<Module> yangModules) {
386             this.context = context;
387             this.yangModules = yangModules;
388         }
389
390         SchemaContext getContext() {
391             return context;
392         }
393
394         Set<Module> getYangModules() {
395             return yangModules;
396         }
397     }
398
399 }