Fixed relative/absolute yang files directory resolving.
[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) throws FileNotFoundException {
49         if (cache.get(rootDir) != null)
50             return cache.get(rootDir);
51
52         File file = new File(rootDir);
53         if(!file.exists()) {
54             throw new FileNotFoundException();
55         }
56
57         Collection<File> yangFiles = FileUtils.listFiles(new File(rootDir),
58                 new String[] { YANG_SUFFIX }, true);
59
60         toCache(rootDir, yangFiles);
61         return yangFiles;
62     }
63
64     static Collection<InputStream> listFilesAsStream(String rootDir) throws FileNotFoundException {
65         Collection<InputStream> is = new ArrayList<InputStream>();
66
67         Collection<File> files = listFiles(rootDir);
68         for(File f : files) {
69             is.add(new FileInputStream(f));
70         }
71
72         return is;
73     }
74
75     static String[] listFilesAsArrayOfPaths(String rootDir) throws FileNotFoundException {
76         String[] filesArray = new String[] {};
77         Collection<File> yangFiles = listFiles(rootDir);
78
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>() {
83
84                     @Override
85                     public String apply(File input) {
86                         return input.getPath();
87                     }
88                 }).toArray(filesArray);
89     }
90
91     private static void toCache(final String rootDir,
92             final Collection<File> yangFiles) {
93         cache.put(rootDir, yangFiles);
94     }
95
96     /**
97      * Instantiate object from fully qualified class name
98      */
99     static <T> T getInstance(String codeGeneratorClass, Class<T> baseType)
100             throws ClassNotFoundException, InstantiationException,
101             IllegalAccessException {
102         return baseType.cast(resolveClass(codeGeneratorClass, baseType)
103                 .newInstance());
104     }
105
106     private static Class<?> resolveClass(String codeGeneratorClass,
107             Class<?> baseType) throws ClassNotFoundException {
108         Class<?> clazz = Class.forName(codeGeneratorClass);
109
110         if (!isImplemented(baseType, clazz))
111             throw new IllegalArgumentException("Code generator " + clazz
112                     + " has to implement " + baseType);
113         return clazz;
114     }
115
116     private static boolean isImplemented(Class<?> expectedIface,
117             Class<?> byClazz) {
118         for (Class<?> iface : byClazz.getInterfaces()) {
119             if (iface.equals(expectedIface))
120                 return true;
121         }
122         return false;
123     }
124
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);
128     }
129
130     public static List<File> getClassPath(MavenProject project)
131             throws DependencyResolutionRequiredException {
132         List<File> dependencies = Lists.newArrayList();
133         try {
134             for (Object element : project.getCompileClasspathElements()) {
135                 File asFile = new File((String) element);
136                 if (isJar(asFile)) {
137                     dependencies.add(asFile);
138                 }
139             }
140         } catch (DependencyResolutionRequiredException e) {
141             throw e;
142         }
143         return dependencies;
144     }
145
146     private static final String JAR_SUFFIX = ".jar";
147
148     private static boolean isJar(File element) {
149         return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true
150                 : false;
151     }
152
153     public static Collection<File> getFilesFromClasspath(
154             List<File> jarsOnClasspath, List<String> classPathFilter)
155             throws ZipException, IOException {
156         List<File> yangs = Lists.newArrayList();
157
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);
165                 }
166             }
167         }
168
169         return yangs;
170     }
171
172     public static boolean acceptedFilter(String name, List<String> filter) {
173         for(String f : filter) {
174             if(name.endsWith(f)) {
175                 return true;
176             }
177         }
178         return false;
179     }
180
181 }