Merge "Limit the references IdentityCodec retains"
[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 java.io.Closeable;
11 import java.io.File;
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;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
27
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;
42
43 import com.google.common.base.Preconditions;
44 import com.google.common.collect.Lists;
45 import com.google.common.collect.Maps;
46
47 final class Util {
48
49     /**
50      * It isn't desirable to create instances of this class
51      */
52     private Util() {
53     }
54
55     static final String YANG_SUFFIX = "yang";
56
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
62     // only once.
63     private static Map<File, Collection<File>> cache = Maps.newHashMapWithExpectedSize(CACHE_SIZE);
64
65     /**
66      * List files recursively and return as array of String paths. Use cache of
67      * size 1.
68      */
69     static Collection<File> listFiles(File root) throws FileNotFoundException {
70         if (cache.get(root) != null) {
71             return cache.get(root);
72         }
73
74         if (!root.exists()) {
75             throw new FileNotFoundException(root.toString());
76         }
77
78         Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
79
80         toCache(root, yangFiles);
81         return yangFiles;
82     }
83
84     static Collection<File> listFiles(File root, File[] excludedFiles, Log log) throws FileNotFoundException {
85         if (!root.exists()) {
86             if (log != null) {
87                 log.warn(Util.message("YANG source directory %s not found. No code will be generated.", YangToSourcesProcessor.LOG_PREFIX, root.toString()));
88             }
89             return Collections.emptyList();
90         }
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) {
96                 if (ex.equals(f)) {
97                     excluded = true;
98                     break;
99                 }
100             }
101             if (excluded) {
102                 if (log != null) {
103                     log.info(Util.message("%s file excluded %s", YangToSourcesProcessor.LOG_PREFIX,
104                             Util.YANG_SUFFIX.toUpperCase(), f));
105                 }
106             } else {
107                 result.add(f);
108             }
109         }
110
111         return result;
112     }
113
114     private static void toCache(final File rootDir, final Collection<File> yangFiles) {
115         cache.put(rootDir, yangFiles);
116     }
117
118     /**
119      * Instantiate object from fully qualified class name
120      */
121     static <T> T getInstance(String codeGeneratorClass, Class<T> baseType) throws ClassNotFoundException,
122             InstantiationException, IllegalAccessException {
123         return baseType.cast(resolveClass(codeGeneratorClass, baseType).newInstance());
124     }
125
126     private static Class<?> resolveClass(String codeGeneratorClass, Class<?> baseType) throws ClassNotFoundException {
127         Class<?> clazz = Class.forName(codeGeneratorClass);
128
129         if (!isImplemented(baseType, clazz)) {
130             throw new IllegalArgumentException("Code generator " + clazz + " has to implement " + baseType);
131         }
132         return clazz;
133     }
134
135     private static boolean isImplemented(Class<?> expectedIface, Class<?> byClazz) {
136         for (Class<?> iface : byClazz.getInterfaces()) {
137             if (iface.equals(expectedIface)) {
138                 return true;
139             }
140         }
141         return false;
142     }
143
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);
147     }
148
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);
155             }
156         }
157         return dependencies;
158     }
159
160     /**
161      * Read current project dependencies and check if it don't grab incorrect
162      * artifacts versions which could be in conflict with plugin dependencies.
163      *
164      * @param project
165      *            current project
166      * @param repoSystem
167      *            repository system
168      * @param localRepo
169      *            local repository
170      * @param remoteRepos
171      *            remote repositories
172      * @param log
173      *            logger
174      */
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));
181         } else {
182             Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
183             getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos, log);
184
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);
190                 }
191             }
192         }
193     }
194
195     /**
196      * Read transitive dependencies of given plugin and store them in map.
197      *
198      * @param plugin
199      *            plugin to read
200      * @param map
201      *            map, where founded transitive dependencies will be stored
202      * @param repoSystem
203      *            repository system
204      * @param localRepository
205      *            local repository
206      * @param remoteRepos
207      *            list of remote repositories
208      * @param log
209      *            logger
210      */
211     private static void getPluginTransitiveDependencies(Plugin plugin, Map<Artifact, Collection<Artifact>> map,
212             RepositorySystem repoSystem, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepos,
213             Log log) {
214
215         List<Dependency> pluginDependencies = plugin.getDependencies();
216         for (Dependency dep : pluginDependencies) {
217             Artifact artifact = repoSystem.createDependencyArtifact(dep);
218
219             ArtifactResolutionRequest request = new ArtifactResolutionRequest();
220             request.setArtifact(artifact);
221             request.setResolveTransitively(true);
222             request.setLocalRepository(localRepository);
223             request.setRemoteRepositories(remoteRepos);
224
225             ArtifactResolutionResult result = repoSystem.resolve(request);
226             Set<Artifact> pluginDependencyDependencies = result.getArtifacts();
227             map.put(artifact, pluginDependencyDependencies);
228         }
229     }
230
231     /**
232      * Check artifact against collection of dependencies. If collection contains
233      * artifact with same groupId and artifactId, but different version, logs a
234      * warning.
235      *
236      * @param artifact
237      *            artifact to check
238      * @param dependencies
239      *            collection of dependencies
240      * @param log
241      *            logger
242      */
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));
252                 }
253             }
254         }
255     }
256
257     private static final String JAR_SUFFIX = ".jar";
258
259     private static boolean isJar(File element) {
260         return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true : false;
261     }
262
263     static <T> T checkNotNull(T obj, String paramName) {
264         return Preconditions.checkNotNull(obj, "Parameter " + paramName + " is null");
265     }
266
267     static final class YangsInZipsResult implements Closeable {
268         private final List<InputStream> yangStreams;
269         private final List<Closeable> zipInputStreams;
270
271         private YangsInZipsResult(List<InputStream> yangStreams, List<Closeable> zipInputStreams) {
272             this.yangStreams = yangStreams;
273             this.zipInputStreams = zipInputStreams;
274         }
275
276         @Override
277         public void close() throws IOException {
278             for (InputStream is : yangStreams) {
279                 is.close();
280             }
281             for (Closeable is : zipInputStreams) {
282                 is.close();
283             }
284         }
285
286         public List<InputStream> getYangStreams() {
287             return this.yangStreams;
288         }
289     }
290
291     static YangsInZipsResult findYangFilesInDependenciesAsStream(Log log, MavenProject project)
292             throws MojoFailureException {
293         List<InputStream> yangsFromDependencies = new ArrayList<>();
294         List<Closeable> zips = new ArrayList<>();
295         try {
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));
299
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() {
308                             @Override
309                             public boolean accept(File dir, String name) {
310                                 return name.endsWith(".yang") && new File(dir, name).isFile();
311                             }
312                         });
313                         for (File yangFile : yangFiles) {
314                             yangsFromDependencies.add(new NamedFileInputStream(yangFile, YangToSourcesProcessor.META_INF_YANG_STRING + File.separator + yangFile.getName()));
315                         }
316                     }
317
318                 } else {
319                     ZipFile zip = new ZipFile(file);
320                     zips.add(zip);
321
322                     Enumeration<? extends ZipEntry> entries = zip.entries();
323                     while (entries.hasMoreElements()) {
324                         ZipEntry entry = entries.nextElement();
325                         String entryName = entry.getName();
326
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
331                             // parsed.
332                             InputStream entryStream = zip.getInputStream(entry);
333                             yangsFromDependencies.add(entryStream);
334                         }
335                     }
336                 }
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));
340                 }
341
342             }
343         } catch (Exception e) {
344             throw new MojoFailureException(e.getMessage(), e);
345         }
346         return new YangsInZipsResult(yangsFromDependencies, zips);
347     }
348
349     static Collection<File> findYangFilesInDependencies(Log log, MavenProject project) throws MojoFailureException {
350         final List<File> yangsFilesFromDependencies = new ArrayList<>();
351
352         try {
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));
356
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() {
364                             @Override
365                             public boolean accept(File dir, String name) {
366                                 return name.endsWith(".yang") && new File(dir, name).isFile();
367                             }
368                         });
369
370                         yangsFilesFromDependencies.addAll(Arrays.asList(yangFiles));
371                     }
372                 } else {
373                     try (ZipFile zip = new ZipFile(file)) {
374
375                         final Enumeration<? extends ZipEntry> entries = zip.entries();
376                         while (entries.hasMoreElements()) {
377                             ZipEntry entry = entries.nextElement();
378                             String entryName = entry.getName();
379
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,
383                                         file, entryName));
384                                 yangsFilesFromDependencies.add(file);
385                                 break;
386                             }
387                         }
388                     }
389                 }
390             }
391         } catch (Exception e) {
392             throw new MojoFailureException("Failed to scan for YANG files in depedencies", e);
393         }
394         return yangsFilesFromDependencies;
395     }
396
397     static final class ContextHolder {
398         private final SchemaContext context;
399         private final Set<Module> yangModules;
400
401         ContextHolder(SchemaContext context, Set<Module> yangModules) {
402             this.context = context;
403             this.yangModules = yangModules;
404         }
405
406         SchemaContext getContext() {
407             return context;
408         }
409
410         Set<Module> getYangModules() {
411             return yangModules;
412         }
413     }
414
415 }