2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang2sources.plugin;
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;
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;
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;
46 * It isn't desirable to create instances of this class
51 static final String YANG_SUFFIX = "yang";
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
60 private static Map<File, Collection<File>> cache = Maps.newHashMapWithExpectedSize(CACHE_SIZE);
63 * List files recursively and return as array of String paths. Use cache of
66 static Collection<File> listFiles(File root) throws FileNotFoundException {
67 if (cache.get(root) != null) {
68 return cache.get(root);
72 throw new FileNotFoundException(root.toString());
75 Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
77 toCache(root, yangFiles);
81 static Collection<File> listFiles(File root, File[] excludedFiles) throws FileNotFoundException {
83 LOG.warn("{} YANG source directory {} not found. No code will be generated.", YangToSourcesProcessor
84 .LOG_PREFIX, root.toString());
86 return Collections.emptyList();
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) {
99 LOG.info("{} {} file excluded {}", YangToSourcesProcessor.LOG_PREFIX, Util.YANG_SUFFIX.toUpperCase(),
109 private static void toCache(final File rootDir, final Collection<File> yangFiles) {
110 cache.put(rootDir, yangFiles);
114 * Instantiate object from fully qualified class name
116 static <T> T getInstance(String codeGeneratorClass, Class<T> baseType) throws ClassNotFoundException,
117 InstantiationException, IllegalAccessException {
118 return baseType.cast(resolveClass(codeGeneratorClass, baseType).newInstance());
121 private static Class<?> resolveClass(String codeGeneratorClass, Class<?> baseType) throws ClassNotFoundException {
122 Class<?> clazz = Class.forName(codeGeneratorClass);
124 if (!baseType.isAssignableFrom(clazz)) {
125 throw new IllegalArgumentException("Code generator " + clazz + " has to implement " + baseType);
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);
142 * Read current project dependencies and check if it don't grab incorrect
143 * artifacts versions which could be in conflict with plugin dependencies.
152 * remote repositories
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);
161 Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
162 getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos);
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);
175 * Read transitive dependencies of given plugin and store them in map.
180 * map, where founded transitive dependencies will be stored
183 * @param localRepository
186 * list of remote repositories
188 private static void getPluginTransitiveDependencies(Plugin plugin, Map<Artifact, Collection<Artifact>> map,
189 RepositorySystem repoSystem, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepos) {
191 List<Dependency> pluginDependencies = plugin.getDependencies();
192 for (Dependency dep : pluginDependencies) {
193 Artifact artifact = repoSystem.createDependencyArtifact(dep);
195 ArtifactResolutionRequest request = new ArtifactResolutionRequest();
196 request.setArtifact(artifact);
197 request.setResolveTransitively(true);
198 request.setLocalRepository(localRepository);
199 request.setRemoteRepositories(remoteRepos);
201 ArtifactResolutionResult result = repoSystem.resolve(request);
202 Set<Artifact> pluginDependencyDependencies = result.getArtifacts();
203 map.put(artifact, pluginDependencyDependencies);
208 * Check artifact against collection of dependencies. If collection contains
209 * artifact with same groupId and artifactId, but different version, logs a
214 * @param dependencies
215 * collection of dependencies
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);
230 private static final String JAR_SUFFIX = ".jar";
232 private static boolean isJar(File element) {
233 return (element.isFile() && element.getName().endsWith(JAR_SUFFIX));
236 static <T> T checkNotNull(T obj, String paramName) {
237 return Preconditions.checkNotNull(obj, "Parameter " + paramName + " is null");
240 static final class YangsInZipsResult implements Closeable {
241 private final List<YangSourceFromDependency> yangStreams;
242 private final List<Closeable> zipInputStreams;
244 private YangsInZipsResult(List<YangSourceFromDependency> yangStreams, List<Closeable> zipInputStreams) {
245 this.yangStreams = yangStreams;
246 this.zipInputStreams = zipInputStreams;
250 public void close() throws IOException {
251 for (Closeable is : zipInputStreams) {
256 public List<YangSourceFromDependency> getYangStreams() {
257 return this.yangStreams;
261 static YangsInZipsResult findYangFilesInDependenciesAsStream(MavenProject project)
262 throws MojoFailureException {
263 List<YangSourceFromDependency> yangsFromDependencies = new ArrayList<>();
264 List<Closeable> zips = new ArrayList<>();
266 List<File> filesOnCp = Util.getClassPath(project);
267 LOG.info("{} Searching for yang files in following dependencies: {}", YangToSourcesProcessor.LOG_PREFIX,
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));
285 ZipFile zip = new ZipFile(file);
288 Enumeration<? extends ZipEntry> entries = zip.entries();
289 while (entries.hasMoreElements()) {
290 ZipEntry entry = entries.nextElement();
291 String entryName = entry.getName();
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));
300 if (foundFilesForReporting.size() > 0) {
301 LOG.info("{} Found {} yang files in {}: {}", YangToSourcesProcessor.LOG_PREFIX,
302 foundFilesForReporting.size(), file, foundFilesForReporting);
306 } catch (Exception e) {
307 throw new MojoFailureException(e.getMessage(), e);
309 return new YangsInZipsResult(yangsFromDependencies, zips);
313 * Find all dependencies which contains yang sources
315 * Returns collection of YANG files and Zip files which contains YANG files.
317 * FIXME: Rename to what class is actually doing.
321 * @throws MojoFailureException
323 static Collection<File> findYangFilesInDependencies(MavenProject project) throws MojoFailureException {
324 final List<File> yangsFilesFromDependencies = new ArrayList<>();
326 List<File> filesOnCp;
328 filesOnCp = Util.getClassPath(project);
329 } catch (Exception e) {
330 throw new MojoFailureException("Failed to scan for YANG files in dependencies", e);
332 LOG.info("{} Searching for yang files in following dependencies: {}", YangToSourcesProcessor.LOG_PREFIX,
335 for (File file : filesOnCp) {
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());
345 yangsFilesFromDependencies.addAll(Arrays.asList(yangFiles));
348 try (ZipFile zip = new ZipFile(file)) {
350 final Enumeration<? extends ZipEntry> entries = zip.entries();
351 while (entries.hasMoreElements()) {
352 ZipEntry entry = entries.nextElement();
353 String entryName = entry.getName();
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,
359 yangsFilesFromDependencies.add(file);
365 } catch (Exception e) {
366 throw new MojoFailureException("Failed to scan for YANG files in dependency: " + file.toString(), e);
369 return yangsFilesFromDependencies;
372 static final class ContextHolder {
373 private final SchemaContext context;
374 private final Set<Module> yangModules;
376 ContextHolder(SchemaContext context, Set<Module> yangModules) {
377 this.context = context;
378 this.yangModules = yangModules;
381 SchemaContext getContext() {
385 Set<Module> getYangModules() {