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.controller.yang2sources.plugin;
11 import java.io.FileInputStream;
12 import java.io.FileNotFoundException;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Enumeration;
18 import java.util.List;
20 import java.util.zip.ZipEntry;
21 import java.util.zip.ZipException;
22 import java.util.zip.ZipFile;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.maven.artifact.DependencyResolutionRequiredException;
26 import org.apache.maven.project.MavenProject;
28 import com.google.common.base.Function;
29 import com.google.common.collect.Collections2;
30 import com.google.common.collect.Lists;
31 import com.google.common.collect.Maps;
34 static final String YANG_SUFFIX = "yang";
36 // Cache for listed directories and found yang files. Typically yang files
37 // are utilized twice. First: code is generated during generate-sources
38 // phase Second: yang files are copied as resources during
39 // generate-resources phase. This cache ensures that yang files are listed
41 private static Map<String, Collection<File>> cache = Maps
42 .newHashMapWithExpectedSize(10);
45 * List files recursively and return as array of String paths. Use cache of
48 static Collection<File> listFiles(String rootDir) throws FileNotFoundException {
49 if (cache.get(rootDir) != null)
50 return cache.get(rootDir);
52 File file = new File(rootDir);
54 throw new FileNotFoundException();
57 Collection<File> yangFiles = FileUtils.listFiles(new File(rootDir),
58 new String[] { YANG_SUFFIX }, true);
60 toCache(rootDir, yangFiles);
64 static Collection<InputStream> listFilesAsStream(String rootDir) throws FileNotFoundException {
65 Collection<InputStream> is = new ArrayList<InputStream>();
67 Collection<File> files = listFiles(rootDir);
69 is.add(new FileInputStream(f));
75 static String[] listFilesAsArrayOfPaths(String rootDir) throws FileNotFoundException {
76 String[] filesArray = new String[] {};
77 Collection<File> yangFiles = listFiles(rootDir);
79 // If collection is empty, return empty array [] rather then [null]
80 // array, that is created by default
81 return yangFiles.isEmpty() ? filesArray : Collections2.transform(
82 yangFiles, new Function<File, String>() {
85 public String apply(File input) {
86 return input.getPath();
88 }).toArray(filesArray);
91 private static void toCache(final String rootDir,
92 final Collection<File> yangFiles) {
93 cache.put(rootDir, yangFiles);
97 * Instantiate object from fully qualified class name
99 static <T> T getInstance(String codeGeneratorClass, Class<T> baseType)
100 throws ClassNotFoundException, InstantiationException,
101 IllegalAccessException {
102 return baseType.cast(resolveClass(codeGeneratorClass, baseType)
106 private static Class<?> resolveClass(String codeGeneratorClass,
107 Class<?> baseType) throws ClassNotFoundException {
108 Class<?> clazz = Class.forName(codeGeneratorClass);
110 if (!isImplemented(baseType, clazz))
111 throw new IllegalArgumentException("Code generator " + clazz
112 + " has to implement " + baseType);
116 private static boolean isImplemented(Class<?> expectedIface,
118 for (Class<?> iface : byClazz.getInterfaces()) {
119 if (iface.equals(expectedIface))
125 static String message(String message, String logPrefix, Object... args) {
126 String innerMessage = String.format(message, args);
127 return String.format("%s %s", logPrefix, innerMessage);
130 public static List<File> getClassPath(MavenProject project)
131 throws DependencyResolutionRequiredException {
132 List<File> dependencies = Lists.newArrayList();
134 for (Object element : project.getCompileClasspathElements()) {
135 File asFile = new File((String) element);
137 dependencies.add(asFile);
140 } catch (DependencyResolutionRequiredException e) {
146 private static final String JAR_SUFFIX = ".jar";
148 private static boolean isJar(File element) {
149 return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true
153 public static Collection<File> getFilesFromClasspath(
154 List<File> jarsOnClasspath, List<String> classPathFilter)
155 throws ZipException, IOException {
156 List<File> yangs = Lists.newArrayList();
158 for (File file : jarsOnClasspath) {
159 ZipFile zip = new ZipFile(file);
160 Enumeration<? extends ZipEntry> entries = zip.entries();
161 while (entries.hasMoreElements()) {
162 ZipEntry entry = entries.nextElement();
163 if (entry.getName().endsWith(YANG_SUFFIX)) {
164 InputStream stream = zip.getInputStream(entry);
172 public static boolean acceptedFilter(String name, List<String> filter) {
173 for(String f : filter) {
174 if(name.endsWith(f)) {