653c0bff6d5b6c1fca52c9ff891df60be1385ba2
[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.InputStream;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.apache.commons.io.FileUtils;
20 import org.apache.maven.artifact.Artifact;
21 import org.apache.maven.project.MavenProject;
22
23 import com.google.common.base.Function;
24 import com.google.common.collect.Collections2;
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Maps;
27
28 final class Util {
29     static final String YANG_SUFFIX = "yang";
30
31     // Cache for listed directories and found yang files. Typically yang files
32     // are utilized twice. First: code is generated during generate-sources
33     // phase Second: yang files are copied as resources during
34     // generate-resources phase. This cache ensures that yang files are listed
35     // only once.
36     private static Map<File, Collection<File>> cache = Maps
37             .newHashMapWithExpectedSize(10);
38
39     /**
40      * List files recursively and return as array of String paths. Use cache of
41      * size 1.
42      */
43     static Collection<File> listFiles(File root) throws FileNotFoundException {
44         if (cache.get(root) != null)
45             return cache.get(root);
46
47         if (!root.exists()) {
48             throw new FileNotFoundException(root.toString());
49         }
50
51         Collection<File> yangFiles = FileUtils.listFiles(root,
52                 new String[] { YANG_SUFFIX }, true);
53
54         toCache(root, yangFiles);
55         return yangFiles;
56     }
57
58     static List<InputStream> listFilesAsStream(File rootDir)
59             throws FileNotFoundException {
60         List<InputStream> is = new ArrayList<InputStream>();
61
62         Collection<File> files = listFiles(rootDir);
63         for (File f : files) {
64             is.add(new NamedFileInputStream(f));
65         }
66
67         return is;
68     }
69
70     static class NamedFileInputStream extends FileInputStream {
71         private final File file;
72
73         NamedFileInputStream(File file) throws FileNotFoundException {
74             super(file);
75             this.file = file;
76         }
77
78         @Override
79         public String toString() {
80             return getClass().getSimpleName() + "{" + file + "}";
81         }
82     }
83
84     static String[] listFilesAsArrayOfPaths(File rootDir)
85             throws FileNotFoundException {
86         String[] filesArray = new String[] {};
87         Collection<File> yangFiles = listFiles(rootDir);
88
89         // If collection is empty, return empty array [] rather then [null]
90         // array, that is created by default
91         return yangFiles.isEmpty() ? filesArray : Collections2.transform(
92                 yangFiles, new Function<File, String>() {
93
94                     @Override
95                     public String apply(File input) {
96                         return input.getPath();
97                     }
98                 }).toArray(filesArray);
99     }
100
101     private static void toCache(final File rootDir,
102             final Collection<File> yangFiles) {
103         cache.put(rootDir, yangFiles);
104     }
105
106     /**
107      * Instantiate object from fully qualified class name
108      */
109     static <T> T getInstance(String codeGeneratorClass, Class<T> baseType)
110             throws ClassNotFoundException, InstantiationException,
111             IllegalAccessException {
112         return baseType.cast(resolveClass(codeGeneratorClass, baseType)
113                 .newInstance());
114     }
115
116     private static Class<?> resolveClass(String codeGeneratorClass,
117             Class<?> baseType) throws ClassNotFoundException {
118         Class<?> clazz = Class.forName(codeGeneratorClass);
119
120         if (!isImplemented(baseType, clazz))
121             throw new IllegalArgumentException("Code generator " + clazz
122                     + " has to implement " + baseType);
123         return clazz;
124     }
125
126     private static boolean isImplemented(Class<?> expectedIface,
127             Class<?> byClazz) {
128         for (Class<?> iface : byClazz.getInterfaces()) {
129             if (iface.equals(expectedIface))
130                 return true;
131         }
132         return false;
133     }
134
135     static String message(String message, String logPrefix, Object... args) {
136         String innerMessage = String.format(message, args);
137         return String.format("%s %s", logPrefix, innerMessage);
138     }
139
140     public static List<File> getClassPath(MavenProject project) {
141         List<File> dependencies = Lists.newArrayList();
142         for (Artifact element : project.getArtifacts()) {
143             File asFile = element.getFile();
144             if (isJar(asFile) || asFile.isDirectory()) {
145                 dependencies.add(asFile);
146             }
147         }
148         return dependencies;
149     }
150
151     private static final String JAR_SUFFIX = ".jar";
152
153     private static boolean isJar(File element) {
154         return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true
155                 : false;
156     }
157
158     public static boolean acceptedFilter(String name, List<String> filter) {
159         for (String f : filter) {
160             if (name.endsWith(f)) {
161                 return true;
162             }
163         }
164         return false;
165     }
166
167 }