Bug 7182: YangToSourcesProcessor deletes output directory
[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 static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_FILE_EXTENSION;
11
12 import com.google.common.base.Splitter;
13 import com.google.common.collect.Iterables;
14 import java.io.Closeable;
15 import java.io.File;
16 import java.io.FileNotFoundException;
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.Optional;
27 import java.util.Set;
28 import java.util.zip.ZipEntry;
29 import java.util.zip.ZipFile;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.artifact.repository.ArtifactRepository;
33 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
34 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
35 import org.apache.maven.model.Dependency;
36 import org.apache.maven.model.Plugin;
37 import org.apache.maven.plugin.MojoFailureException;
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 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 final class Util {
46
47     /**
48      * It isn't desirable to create instances of this class
49      */
50     private Util() {
51     }
52
53     static final String YANG_SUFFIX = "yang";
54
55     private static final Logger LOG = LoggerFactory.getLogger(Util.class);
56
57     static Collection<File> listFiles(final File root, final Collection<File> excludedFiles) throws FileNotFoundException {
58         if (!root.exists()) {
59             LOG.warn("{} YANG source directory {} not found. No code will be generated.", YangToSourcesProcessor
60                     .LOG_PREFIX, root.toString());
61
62             return Collections.emptyList();
63         }
64         Collection<File> result = new ArrayList<>();
65         Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
66         for (File f : yangFiles) {
67             if (excludedFiles.contains(f)) {
68                 LOG.info("{} {} file excluded {}", YangToSourcesProcessor.LOG_PREFIX, Util.YANG_SUFFIX.toUpperCase(),
69                         f);
70             } else {
71                 result.add(f);
72             }
73         }
74
75         return result;
76     }
77
78     static List<File> getClassPath(final MavenProject project) {
79         List<File> dependencies = new ArrayList<>();
80         for (Artifact element : project.getArtifacts()) {
81             File asFile = element.getFile();
82             if (isJar(asFile) || asFile.isDirectory()) {
83                 dependencies.add(asFile);
84             }
85         }
86         return dependencies;
87     }
88
89     /**
90      * Read current project dependencies and check if it don't grab incorrect
91      * artifacts versions which could be in conflict with plugin dependencies.
92      *
93      * @param project
94      *            current project
95      * @param repoSystem
96      *            repository system
97      * @param localRepo
98      *            local repository
99      * @param remoteRepos
100      *            remote repositories
101      */
102     static void checkClasspath(final MavenProject project, final RepositorySystem repoSystem,
103             final ArtifactRepository localRepo, final List<ArtifactRepository> remoteRepos) {
104         Plugin plugin = project.getPlugin(YangToSourcesMojo.PLUGIN_NAME);
105         if (plugin == null) {
106             LOG.warn("{} {} not found, dependencies version check skipped", YangToSourcesProcessor.LOG_PREFIX,
107                     YangToSourcesMojo.PLUGIN_NAME);
108         } else {
109             Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
110             getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos);
111
112             Set<Artifact> projectDependencies = project.getDependencyArtifacts();
113             for (Map.Entry<Artifact, Collection<Artifact>> entry : pluginDependencies.entrySet()) {
114                 checkArtifact(entry.getKey(), projectDependencies);
115                 for (Artifact dependency : entry.getValue()) {
116                     checkArtifact(dependency, projectDependencies);
117                 }
118             }
119         }
120     }
121
122     /**
123      * Read transitive dependencies of given plugin and store them in map.
124      *
125      * @param plugin
126      *            plugin to read
127      * @param map
128      *            map, where founded transitive dependencies will be stored
129      * @param repoSystem
130      *            repository system
131      * @param localRepository
132      *            local repository
133      * @param remoteRepos
134      *            list of remote repositories
135      */
136     private static void getPluginTransitiveDependencies(final Plugin plugin,
137             final Map<Artifact, Collection<Artifact>> map, final RepositorySystem repoSystem,
138             final ArtifactRepository localRepository, final List<ArtifactRepository> remoteRepos) {
139
140         List<Dependency> pluginDependencies = plugin.getDependencies();
141         for (Dependency dep : pluginDependencies) {
142             Artifact artifact = repoSystem.createDependencyArtifact(dep);
143
144             ArtifactResolutionRequest request = new ArtifactResolutionRequest();
145             request.setArtifact(artifact);
146             request.setResolveTransitively(true);
147             request.setLocalRepository(localRepository);
148             request.setRemoteRepositories(remoteRepos);
149
150             ArtifactResolutionResult result = repoSystem.resolve(request);
151             Set<Artifact> pluginDependencyDependencies = result.getArtifacts();
152             map.put(artifact, pluginDependencyDependencies);
153         }
154     }
155
156     /**
157      * Check artifact against collection of dependencies. If collection contains
158      * artifact with same groupId and artifactId, but different version, logs a
159      * warning.
160      *
161      * @param artifact
162      *            artifact to check
163      * @param dependencies
164      *            collection of dependencies
165      */
166     private static void checkArtifact(final Artifact artifact, final Collection<Artifact> dependencies) {
167         for (org.apache.maven.artifact.Artifact d : dependencies) {
168             if (artifact.getGroupId().equals(d.getGroupId()) && artifact.getArtifactId().equals(d.getArtifactId())) {
169                 if (!(artifact.getVersion().equals(d.getVersion()))) {
170                     LOG.warn("{} Dependency resolution conflict:", YangToSourcesProcessor.LOG_PREFIX);
171                     LOG.warn("{} '{}' dependency [{}] has different version than one declared in current project [{}]" +
172                                     ". It is recommended to fix this problem because it may cause compilation errors.",
173                             YangToSourcesProcessor.LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME, artifact, d);
174                 }
175             }
176         }
177     }
178
179     private static final String JAR_SUFFIX = ".jar";
180
181     private static boolean isJar(final File element) {
182         return (element.isFile() && element.getName().endsWith(JAR_SUFFIX));
183     }
184
185     static final class YangsInZipsResult implements Closeable {
186         private final List<YangSourceFromDependency> yangStreams;
187         private final List<Closeable> zipInputStreams;
188
189         private YangsInZipsResult(final List<YangSourceFromDependency> yangStreams,
190                 final List<Closeable> zipInputStreams) {
191             this.yangStreams = yangStreams;
192             this.zipInputStreams = zipInputStreams;
193         }
194
195         @Override
196         public void close() throws IOException {
197             for (Closeable is : zipInputStreams) {
198                 is.close();
199             }
200         }
201
202         public List<YangSourceFromDependency> getYangStreams() {
203             return this.yangStreams;
204         }
205     }
206
207     static YangsInZipsResult findYangFilesInDependenciesAsStream(final MavenProject project)
208             throws MojoFailureException {
209         List<YangSourceFromDependency> yangsFromDependencies = new ArrayList<>();
210         List<Closeable> zips = new ArrayList<>();
211         try {
212             List<File> filesOnCp = Util.getClassPath(project);
213             LOG.info("{} Searching for yang files in following dependencies: {}", YangToSourcesProcessor.LOG_PREFIX,
214                     filesOnCp);
215
216             for (File file : filesOnCp) {
217                 List<String> foundFilesForReporting = new ArrayList<>();
218                 // is it jar file or directory?
219                 if (file.isDirectory()) {
220                     //FIXME: code duplicate
221                     File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING);
222                     if (yangDir.exists() && yangDir.isDirectory()) {
223                         File[] yangFiles = yangDir.listFiles(
224                             (dir, name) -> name.endsWith(RFC6020_YANG_FILE_EXTENSION) && new File(dir, name).isFile());
225                         for (final File yangFile : yangFiles) {
226                             yangsFromDependencies.add(new YangSourceFromFile(yangFile));
227                         }
228                     }
229
230                 } else {
231                     ZipFile zip = new ZipFile(file);
232                     zips.add(zip);
233
234                     Enumeration<? extends ZipEntry> entries = zip.entries();
235                     while (entries.hasMoreElements()) {
236                         ZipEntry entry = entries.nextElement();
237                         String entryName = entry.getName();
238
239                         if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
240                                 && !entry.isDirectory() && entryName.endsWith(RFC6020_YANG_FILE_EXTENSION)) {
241                             foundFilesForReporting.add(entryName);
242                             yangsFromDependencies.add(new YangSourceInZipFile(zip, entry));
243                         }
244                     }
245                 }
246                 if (foundFilesForReporting.size() > 0) {
247                     LOG.info("{} Found {} yang files in {}: {}", YangToSourcesProcessor.LOG_PREFIX,
248                             foundFilesForReporting.size(), file, foundFilesForReporting);
249                 }
250
251             }
252         } catch (Exception e) {
253             throw new MojoFailureException(e.getMessage(), e);
254         }
255         return new YangsInZipsResult(yangsFromDependencies, zips);
256     }
257
258     /**
259      * Find all dependencies which contains yang sources
260      *
261      * Returns collection of YANG files and Zip files which contains YANG files.
262      *
263      * FIXME: Rename to what class is actually doing.
264      *
265      * @param project
266      * @return
267      * @throws MojoFailureException
268      */
269     static Collection<File> findYangFilesInDependencies(final MavenProject project) throws MojoFailureException {
270         final List<File> yangsFilesFromDependencies = new ArrayList<>();
271
272         List<File> filesOnCp;
273         try {
274             filesOnCp = Util.getClassPath(project);
275         } catch (Exception e) {
276             throw new MojoFailureException("Failed to scan for YANG files in dependencies", e);
277         }
278         LOG.info("{} Searching for yang files in following dependencies: {}", YangToSourcesProcessor.LOG_PREFIX,
279             filesOnCp);
280
281         for (File file : filesOnCp) {
282             try {
283                 // is it jar file or directory?
284                 if (file.isDirectory()) {
285                     //FIXME: code duplicate
286                     File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING);
287                     if (yangDir.exists() && yangDir.isDirectory()) {
288                         File[] yangFiles = yangDir.listFiles(
289                             (dir, name) -> name.endsWith(RFC6020_YANG_FILE_EXTENSION) && new File(dir, name).isFile());
290
291                         yangsFilesFromDependencies.addAll(Arrays.asList(yangFiles));
292                     }
293                 } else {
294                     try (ZipFile zip = new ZipFile(file)) {
295
296                         final Enumeration<? extends ZipEntry> entries = zip.entries();
297                         while (entries.hasMoreElements()) {
298                             ZipEntry entry = entries.nextElement();
299                             String entryName = entry.getName();
300
301                             if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
302                                     && !entry.isDirectory() && entryName.endsWith(RFC6020_YANG_FILE_EXTENSION)) {
303                                 LOG.debug("{} Found a YANG file in {}: {}", YangToSourcesProcessor.LOG_PREFIX, file,
304                                         entryName);
305                                 yangsFilesFromDependencies.add(file);
306                                 break;
307                             }
308                         }
309                     }
310                 }
311             } catch (Exception e) {
312                 throw new MojoFailureException("Failed to scan for YANG files in dependency: " + file.toString(), e);
313             }
314         }
315         return yangsFilesFromDependencies;
316     }
317
318     static final class ContextHolder {
319         private static final Splitter SEP_SPLITTER = Splitter.on(File.separator);
320
321         private final SchemaContext context;
322         private final Set<Module> yangModules;
323         private final Set<Module> yangFiles;
324
325         ContextHolder(final SchemaContext context, final Set<Module> yangModules, final Set<Module> yangFiles) {
326             this.context = context;
327             this.yangModules = yangModules;
328             this.yangFiles = yangFiles;
329         }
330
331         SchemaContext getContext() {
332             return context;
333         }
334
335         Set<Module> getYangModules() {
336             return yangModules;
337         }
338
339         Set<Module> getYangFiles() {
340             return yangFiles;
341         }
342
343         Optional<String> moduleToResourcePath(final Module mod) {
344             if (!yangFiles.contains(mod)) {
345                 return Optional.empty();
346             }
347
348             return Optional.of("/" + YangToSourcesProcessor.META_INF_YANG_STRING_JAR + "/"
349                     + Iterables.getLast(SEP_SPLITTER.split(mod.getModuleSourcePath())));
350         }
351     }
352 }