X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fmaven-yang-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang2sources%2Fplugin%2FUtil.java;fp=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fmaven-yang-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang2sources%2Fplugin%2FUtil.java;h=98a5d8531b79e0d8d78da9ffb2ee7461928d6773;hb=78718ca2980d6289703f239abb6b7928ea08c8e4;hp=0000000000000000000000000000000000000000;hpb=8e7e1c62838a9bf7057590e08226220044326e86;p=controller.git diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java new file mode 100644 index 0000000000..98a5d8531b --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.yang2sources.plugin; + +import java.io.File; +import java.util.Collection; +import java.util.Map; + +import org.apache.commons.io.FileUtils; + +import com.google.common.base.Function; +import com.google.common.collect.Collections2; +import com.google.common.collect.Maps; + +final class Util { + + static final String YANG_SUFFIX = "yang"; + + // Cache for listed directories and found yang files. Typically yang files + // are utilized twice. First: code is generated during generate-sources + // phase Second: yang files are copied as resources during + // generate-resources phase. This cache ensures that yang files are listed + // only once. + private static Map> cache = Maps + .newHashMapWithExpectedSize(10); + + /** + * List files recursively and return as array of String paths. Use cache of + * size 1. + */ + static Collection listFiles(String rootDir) { + + if (cache.get(rootDir) != null) + return cache.get(rootDir); + + Collection yangFiles = FileUtils.listFiles(new File(rootDir), + new String[] { YANG_SUFFIX }, true); + + toCache(rootDir, yangFiles); + return yangFiles; + } + + static String[] listFilesAsArrayOfPaths(String rootDir) { + String[] filesArray = new String[] {}; + Collection yangFiles = listFiles(rootDir); + + // If collection is empty, return empty array [] rather then [null] + // array, that is created by default + return yangFiles.isEmpty() ? filesArray : Collections2.transform( + yangFiles, new Function() { + + @Override + public String apply(File input) { + return input.getPath(); + } + }).toArray(filesArray); + } + + private static void toCache(final String rootDir, + final Collection yangFiles) { + cache.put(rootDir, yangFiles); + } + + /** + * Instantiate object from fully qualified class name + */ + static T getInstance(String codeGeneratorClass, Class baseType) + throws ClassNotFoundException, InstantiationException, + IllegalAccessException { + return baseType.cast(resolveClass(codeGeneratorClass, baseType) + .newInstance()); + } + + private static Class resolveClass(String codeGeneratorClass, + Class baseType) throws ClassNotFoundException { + Class clazz = Class.forName(codeGeneratorClass); + + if (!isImplemented(baseType, clazz)) + throw new IllegalArgumentException("Code generator " + clazz + + " has to implement " + baseType); + return clazz; + } + + private static boolean isImplemented(Class expectedIface, + Class byClazz) { + for (Class iface : byClazz.getInterfaces()) { + if (iface.equals(expectedIface)) + return true; + } + return false; + } + + static String message(String message, String logPrefix, Object... args) { + String innerMessage = String.format(message, args); + return String.format("%s %s", logPrefix, innerMessage); + } +}