Yang-maven-plugin refactored and config attributes and spi adjusted.
[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.Closeable;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.io.FilenameFilter;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Enumeration;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.zip.ZipEntry;
24 import java.util.zip.ZipFile;
25
26 import org.apache.commons.io.FileUtils;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.plugin.MojoFailureException;
29 import org.apache.maven.plugin.logging.Log;
30 import org.apache.maven.project.MavenProject;
31 import org.opendaylight.controller.yang.model.api.Module;
32 import org.opendaylight.controller.yang.model.api.SchemaContext;
33
34 import com.google.common.base.Preconditions;
35 import com.google.common.collect.Lists;
36 import com.google.common.collect.Maps;
37
38 final class Util {
39     static final String YANG_SUFFIX = "yang";
40
41     // Cache for listed directories and found yang files. Typically yang files
42     // are utilized twice. First: code is generated during generate-sources
43     // phase Second: yang files are copied as resources during
44     // generate-resources phase. This cache ensures that yang files are listed
45     // only once.
46     private static Map<File, Collection<File>> cache = Maps
47             .newHashMapWithExpectedSize(10);
48
49     /**
50      * List files recursively and return as array of String paths. Use cache of
51      * size 1.
52      */
53     static Collection<File> listFiles(File root) throws FileNotFoundException {
54         if (cache.get(root) != null)
55             return cache.get(root);
56
57         if (!root.exists()) {
58             throw new FileNotFoundException(root.toString());
59         }
60
61         Collection<File> yangFiles = FileUtils.listFiles(root,
62                 new String[] { YANG_SUFFIX }, true);
63
64         toCache(root, yangFiles);
65         return yangFiles;
66     }
67
68     static List<InputStream> listFilesAsStream(File rootDir)
69             throws FileNotFoundException {
70         List<InputStream> is = new ArrayList<InputStream>();
71
72         Collection<File> files = listFiles(rootDir);
73         for (File f : files) {
74             is.add(new NamedFileInputStream(f));
75         }
76
77         return is;
78     }
79
80     static class NamedFileInputStream extends FileInputStream {
81         private final File file;
82
83         NamedFileInputStream(File file) throws FileNotFoundException {
84             super(file);
85             this.file = file;
86         }
87
88         @Override
89         public String toString() {
90             return getClass().getSimpleName() + "{" + file + "}";
91         }
92     }
93
94     private static void toCache(final File rootDir,
95             final Collection<File> yangFiles) {
96         cache.put(rootDir, yangFiles);
97     }
98
99     /**
100      * Instantiate object from fully qualified class name
101      */
102     static <T> T getInstance(String codeGeneratorClass, Class<T> baseType)
103             throws ClassNotFoundException, InstantiationException,
104             IllegalAccessException {
105         return baseType.cast(resolveClass(codeGeneratorClass, baseType)
106                 .newInstance());
107     }
108
109     private static Class<?> resolveClass(String codeGeneratorClass,
110             Class<?> baseType) throws ClassNotFoundException {
111         Class<?> clazz = Class.forName(codeGeneratorClass);
112
113         if (!isImplemented(baseType, clazz))
114             throw new IllegalArgumentException("Code generator " + clazz
115                     + " has to implement " + baseType);
116         return clazz;
117     }
118
119     private static boolean isImplemented(Class<?> expectedIface,
120             Class<?> byClazz) {
121         for (Class<?> iface : byClazz.getInterfaces()) {
122             if (iface.equals(expectedIface))
123                 return true;
124         }
125         return false;
126     }
127
128     static String message(String message, String logPrefix, Object... args) {
129         String innerMessage = String.format(message, args);
130         return String.format("%s %s", logPrefix, innerMessage);
131     }
132
133     static List<File> getClassPath(MavenProject project) {
134         List<File> dependencies = Lists.newArrayList();
135         for (Artifact element : project.getArtifacts()) {
136             File asFile = element.getFile();
137             if (isJar(asFile) || asFile.isDirectory()) {
138                 dependencies.add(asFile);
139             }
140         }
141         return dependencies;
142     }
143
144     private static final String JAR_SUFFIX = ".jar";
145
146     private static boolean isJar(File element) {
147         return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true
148                 : false;
149     }
150
151     static <T> T checkNotNull(T obj, String paramName) {
152         return Preconditions.checkNotNull(obj, "Parameter " + paramName
153                 + " is null");
154     }
155
156     final static class YangsInZipsResult implements Closeable {
157         final List<InputStream> yangStreams;
158         private final List<Closeable> zipInputStreams;
159
160         private YangsInZipsResult(List<InputStream> yangStreams,
161                 List<Closeable> zipInputStreams) {
162             this.yangStreams = yangStreams;
163             this.zipInputStreams = zipInputStreams;
164         }
165
166         @Override
167         public void close() throws IOException {
168             for (InputStream is : yangStreams) {
169                 is.close();
170             }
171             for (Closeable is : zipInputStreams) {
172                 is.close();
173             }
174         }
175     }
176
177     static YangsInZipsResult findYangFilesInDependenciesAsStream(Log log,
178             MavenProject project)
179             throws MojoFailureException {
180         List<InputStream> yangsFromDependencies = new ArrayList<>();
181         List<Closeable> zips = new ArrayList<>();
182         try {
183             List<File> filesOnCp = Util.getClassPath(project);
184             log.info(Util.message(
185                     "Searching for yang files in following dependencies: %s",
186                     YangToSourcesProcessor.LOG_PREFIX, filesOnCp));
187
188             for (File file : filesOnCp) {
189                 List<String> foundFilesForReporting = new ArrayList<>();
190                 // is it jar file or directory?
191                 if (file.isDirectory()) {
192                     File yangDir = new File(file,
193                             YangToSourcesProcessor.META_INF_YANG_STRING);
194                     if (yangDir.exists() && yangDir.isDirectory()) {
195                         File[] yangFiles = yangDir
196                                 .listFiles(new FilenameFilter() {
197                                     @Override
198                                     public boolean accept(File dir, String name) {
199                                         return name.endsWith(".yang")
200                                                 && new File(dir, name).isFile();
201                                     }
202                                 });
203                         for (File yangFile : yangFiles) {
204                             yangsFromDependencies.add(new NamedFileInputStream(
205                                     yangFile));
206                         }
207                     }
208
209                 } else {
210                     ZipFile zip = new ZipFile(file);
211                     zips.add(zip);
212
213                     Enumeration<? extends ZipEntry> entries = zip.entries();
214                     while (entries.hasMoreElements()) {
215                         ZipEntry entry = entries.nextElement();
216                         String entryName = entry.getName();
217
218                         if (entryName
219                                 .startsWith(YangToSourcesProcessor.META_INF_YANG_STRING)) {
220                             if (entry.isDirectory() == false
221                                     && entryName.endsWith(".yang")) {
222                                 foundFilesForReporting.add(entryName);
223                                 // This will be closed after all strams are
224                                 // parsed.
225                                 InputStream entryStream = zip
226                                         .getInputStream(entry);
227                                 yangsFromDependencies.add(entryStream);
228                             }
229                         }
230                     }
231                 }
232                 if (foundFilesForReporting.size() > 0) {
233                     log.info(Util.message("Found %d yang files in %s: %s",
234                             YangToSourcesProcessor.LOG_PREFIX,
235                             foundFilesForReporting.size(), file,
236                             foundFilesForReporting));
237                 }
238
239             }
240         } catch (Exception e) {
241             throw new MojoFailureException(e.getMessage(), e);
242         }
243         return new YangsInZipsResult(yangsFromDependencies, zips);
244     }
245
246     final static class ContextHolder {
247         private final SchemaContext context;
248         private final Set<Module> yangModules;
249
250         ContextHolder(SchemaContext context, Set<Module> yangModules) {
251             this.context = context;
252             this.yangModules = yangModules;
253         }
254
255         SchemaContext getContext() {
256             return context;
257         }
258
259         Set<Module> getYangModules() {
260             return yangModules;
261         }
262     }
263
264 }