3739a1d6599184898a6b64548e86208c634f572d
[controller.git] / opendaylight / sal / yang-prototype / code-generator / maven-yang-plugin / src / main / java / org / opendaylight / controller / 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.controller.yang2sources.plugin;
9
10 import java.io.File;
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;
19 import java.util.Map;
20 import java.util.zip.ZipEntry;
21 import java.util.zip.ZipException;
22 import java.util.zip.ZipFile;
23
24 import org.apache.commons.io.FileUtils;
25 import org.apache.maven.artifact.DependencyResolutionRequiredException;
26 import org.apache.maven.project.MavenProject;
27
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;
32
33 final class Util {
34     static final String YANG_SUFFIX = "yang";
35
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
40     // only once.
41     private static Map<String, Collection<File>> cache = Maps
42             .newHashMapWithExpectedSize(10);
43
44     /**
45      * List files recursively and return as array of String paths. Use cache of
46      * size 1.
47      */
48     static Collection<File> listFiles(String rootDir) {
49         if (cache.get(rootDir) != null)
50             return cache.get(rootDir);
51
52         Collection<File> yangFiles = FileUtils.listFiles(new File(rootDir),
53                 new String[] { YANG_SUFFIX }, true);
54
55         toCache(rootDir, yangFiles);
56         return yangFiles;
57     }
58
59     static Collection<InputStream> listFilesAsStream(String rootDir) throws FileNotFoundException {
60         Collection<InputStream> is = new ArrayList<InputStream>();
61
62         Collection<File> files = listFiles(rootDir);
63         for(File f : files) {
64             is.add(new FileInputStream(f));
65         }
66
67         return is;
68     }
69
70     static String[] listFilesAsArrayOfPaths(String rootDir) {
71         String[] filesArray = new String[] {};
72         Collection<File> yangFiles = listFiles(rootDir);
73
74         // If collection is empty, return empty array [] rather then [null]
75         // array, that is created by default
76         return yangFiles.isEmpty() ? filesArray : Collections2.transform(
77                 yangFiles, new Function<File, String>() {
78
79                     @Override
80                     public String apply(File input) {
81                         return input.getPath();
82                     }
83                 }).toArray(filesArray);
84     }
85
86     private static void toCache(final String rootDir,
87             final Collection<File> yangFiles) {
88         cache.put(rootDir, yangFiles);
89     }
90
91     /**
92      * Instantiate object from fully qualified class name
93      */
94     static <T> T getInstance(String codeGeneratorClass, Class<T> baseType)
95             throws ClassNotFoundException, InstantiationException,
96             IllegalAccessException {
97         return baseType.cast(resolveClass(codeGeneratorClass, baseType)
98                 .newInstance());
99     }
100
101     private static Class<?> resolveClass(String codeGeneratorClass,
102             Class<?> baseType) throws ClassNotFoundException {
103         Class<?> clazz = Class.forName(codeGeneratorClass);
104
105         if (!isImplemented(baseType, clazz))
106             throw new IllegalArgumentException("Code generator " + clazz
107                     + " has to implement " + baseType);
108         return clazz;
109     }
110
111     private static boolean isImplemented(Class<?> expectedIface,
112             Class<?> byClazz) {
113         for (Class<?> iface : byClazz.getInterfaces()) {
114             if (iface.equals(expectedIface))
115                 return true;
116         }
117         return false;
118     }
119
120     static String message(String message, String logPrefix, Object... args) {
121         String innerMessage = String.format(message, args);
122         return String.format("%s %s", logPrefix, innerMessage);
123     }
124
125     public static List<File> getClassPath(MavenProject project)
126             throws DependencyResolutionRequiredException {
127         List<File> dependencies = Lists.newArrayList();
128         try {
129             for (Object element : project.getCompileClasspathElements()) {
130                 File asFile = new File((String) element);
131                 if (isJar(asFile)) {
132                     dependencies.add(asFile);
133                 }
134             }
135         } catch (DependencyResolutionRequiredException e) {
136             throw e;
137         }
138         return dependencies;
139     }
140
141     private static final String JAR_SUFFIX = ".jar";
142
143     private static boolean isJar(File element) {
144         return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true
145                 : false;
146     }
147
148     public static Collection<File> getFilesFromClasspath(
149             List<File> jarsOnClasspath, List<String> classPathFilter)
150             throws ZipException, IOException {
151         List<File> yangs = Lists.newArrayList();
152
153         for (File file : jarsOnClasspath) {
154             ZipFile zip = new ZipFile(file);
155             Enumeration<? extends ZipEntry> entries = zip.entries();
156             while (entries.hasMoreElements()) {
157                 ZipEntry entry = entries.nextElement();
158                 if (entry.getName().endsWith(YANG_SUFFIX)) {
159                     InputStream stream = zip.getInputStream(entry);
160                 }
161             }
162         }
163
164         return yangs;
165     }
166
167     public static boolean acceptedFilter(String name, List<String> filter) {
168         for(String f : filter) {
169             if(name.endsWith(f)) {
170                 return true;
171             }
172         }
173         return false;
174     }
175
176 }