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