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