Renamed yang-to-sources-plugin to maven-yang-plugin.
[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.util.Collection;
12 import java.util.Map;
13
14 import org.apache.commons.io.FileUtils;
15
16 import com.google.common.base.Function;
17 import com.google.common.collect.Collections2;
18 import com.google.common.collect.Maps;
19
20 final class Util {
21
22     static final String YANG_SUFFIX = "yang";
23
24     // Cache for listed directories and found yang files. Typically yang files
25     // are utilized twice. First: code is generated during generate-sources
26     // phase Second: yang files are copied as resources during
27     // generate-resources phase. This cache ensures that yang files are listed
28     // only once.
29     private static Map<String, Collection<File>> cache = Maps
30             .newHashMapWithExpectedSize(10);
31
32     /**
33      * List files recursively and return as array of String paths. Use cache of
34      * size 1.
35      */
36     static Collection<File> listFiles(String rootDir) {
37
38         if (cache.get(rootDir) != null)
39             return cache.get(rootDir);
40
41         Collection<File> yangFiles = FileUtils.listFiles(new File(rootDir),
42                 new String[] { YANG_SUFFIX }, true);
43
44         toCache(rootDir, yangFiles);
45         return yangFiles;
46     }
47
48     static String[] listFilesAsArrayOfPaths(String rootDir) {
49         String[] filesArray = new String[] {};
50         Collection<File> yangFiles = listFiles(rootDir);
51
52         // If collection is empty, return empty array [] rather then [null]
53         // array, that is created by default
54         return yangFiles.isEmpty() ? filesArray : Collections2.transform(
55                 yangFiles, new Function<File, String>() {
56
57                     @Override
58                     public String apply(File input) {
59                         return input.getPath();
60                     }
61                 }).toArray(filesArray);
62     }
63
64     private static void toCache(final String rootDir,
65             final Collection<File> yangFiles) {
66         cache.put(rootDir, yangFiles);
67     }
68
69     /**
70      * Instantiate object from fully qualified class name
71      */
72     static <T> T getInstance(String codeGeneratorClass, Class<T> baseType)
73             throws ClassNotFoundException, InstantiationException,
74             IllegalAccessException {
75         return baseType.cast(resolveClass(codeGeneratorClass, baseType)
76                 .newInstance());
77     }
78
79     private static Class<?> resolveClass(String codeGeneratorClass,
80             Class<?> baseType) throws ClassNotFoundException {
81         Class<?> clazz = Class.forName(codeGeneratorClass);
82
83         if (!isImplemented(baseType, clazz))
84             throw new IllegalArgumentException("Code generator " + clazz
85                     + " has to implement " + baseType);
86         return clazz;
87     }
88
89     private static boolean isImplemented(Class<?> expectedIface,
90             Class<?> byClazz) {
91         for (Class<?> iface : byClazz.getInterfaces()) {
92             if (iface.equals(expectedIface))
93                 return true;
94         }
95         return false;
96     }
97
98     static String message(String message, String logPrefix, Object... args) {
99         String innerMessage = String.format(message, args);
100         return String.format("%s %s", logPrefix, innerMessage);
101     }
102 }