// 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<File, Collection<File>> cache = Maps
- .newHashMapWithExpectedSize(10);
+ private static Map<File, Collection<File>> cache = Maps.newHashMapWithExpectedSize(10);
/**
* List files recursively and return as array of String paths. Use cache of
throw new FileNotFoundException(root.toString());
}
- Collection<File> yangFiles = FileUtils.listFiles(root,
- new String[] { YANG_SUFFIX }, true);
+ Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
toCache(root, yangFiles);
return yangFiles;
}
- static List<InputStream> listFilesAsStream(File rootDir)
+ static Collection<File> listFiles(File root, File[] excludedFiles, Log log) throws FileNotFoundException {
+ if (!root.exists()) {
+ throw new FileNotFoundException(root.toString());
+ }
+ Collection<File> result = new ArrayList<>();
+ Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
+ for (File f : yangFiles) {
+ boolean excluded = false;
+ for (File ex : excludedFiles) {
+ if (ex.equals(f)) {
+ excluded = true;
+ break;
+ }
+ }
+ if (excluded) {
+ if(log != null) {
+ log.info(Util.message("%s file excluded %s", YangToSourcesProcessor.LOG_PREFIX,
+ Util.YANG_SUFFIX.toUpperCase(), f));
+ }
+ } else {
+ result.add(f);
+ }
+ }
+
+ return result;
+ }
+
+ static List<InputStream> listFilesAsStream(File rootDir, File[] excludedFiles, Log log)
throws FileNotFoundException {
List<InputStream> is = new ArrayList<InputStream>();
- Collection<File> files = listFiles(rootDir);
+ Collection<File> files = listFiles(rootDir, excludedFiles, log);
for (File f : files) {
is.add(new NamedFileInputStream(f));
}
}
}
- private static void toCache(final File rootDir,
- final Collection<File> yangFiles) {
+ private static void toCache(final File rootDir, final Collection<File> yangFiles) {
cache.put(rootDir, yangFiles);
}
/**
* Instantiate object from fully qualified class name
*/
- static <T> T getInstance(String codeGeneratorClass, Class<T> baseType)
- throws ClassNotFoundException, InstantiationException,
- IllegalAccessException {
- return baseType.cast(resolveClass(codeGeneratorClass, baseType)
- .newInstance());
+ static <T> T getInstance(String codeGeneratorClass, Class<T> baseType) throws ClassNotFoundException,
+ InstantiationException, IllegalAccessException {
+ return baseType.cast(resolveClass(codeGeneratorClass, baseType).newInstance());
}
- private static Class<?> resolveClass(String codeGeneratorClass,
- Class<?> baseType) throws ClassNotFoundException {
+ 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);
+ throw new IllegalArgumentException("Code generator " + clazz + " has to implement " + baseType);
return clazz;
}
- private static boolean isImplemented(Class<?> expectedIface,
- Class<?> byClazz) {
+ private static boolean isImplemented(Class<?> expectedIface, Class<?> byClazz) {
for (Class<?> iface : byClazz.getInterfaces()) {
if (iface.equals(expectedIface))
return true;
private static final String JAR_SUFFIX = ".jar";
private static boolean isJar(File element) {
- return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true
- : false;
+ return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true : false;
}
static <T> T checkNotNull(T obj, String paramName) {
- return Preconditions.checkNotNull(obj, "Parameter " + paramName
- + " is null");
+ return Preconditions.checkNotNull(obj, "Parameter " + paramName + " is null");
}
final static class YangsInZipsResult implements Closeable {
final List<InputStream> yangStreams;
private final List<Closeable> zipInputStreams;
- private YangsInZipsResult(List<InputStream> yangStreams,
- List<Closeable> zipInputStreams) {
+ private YangsInZipsResult(List<InputStream> yangStreams, List<Closeable> zipInputStreams) {
this.yangStreams = yangStreams;
this.zipInputStreams = zipInputStreams;
}
}
}
- static YangsInZipsResult findYangFilesInDependenciesAsStream(Log log,
- MavenProject project)
+ static YangsInZipsResult findYangFilesInDependenciesAsStream(Log log, MavenProject project)
throws MojoFailureException {
List<InputStream> yangsFromDependencies = new ArrayList<>();
List<Closeable> zips = new ArrayList<>();
try {
List<File> filesOnCp = Util.getClassPath(project);
- log.info(Util.message(
- "Searching for yang files in following dependencies: %s",
+ log.info(Util.message("Searching for yang files in following dependencies: %s",
YangToSourcesProcessor.LOG_PREFIX, filesOnCp));
for (File file : filesOnCp) {
List<String> foundFilesForReporting = new ArrayList<>();
// is it jar file or directory?
if (file.isDirectory()) {
- File yangDir = new File(file,
- YangToSourcesProcessor.META_INF_YANG_STRING);
+ File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING);
if (yangDir.exists() && yangDir.isDirectory()) {
- File[] yangFiles = yangDir
- .listFiles(new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- return name.endsWith(".yang")
- && new File(dir, name).isFile();
- }
- });
+ File[] yangFiles = yangDir.listFiles(new FilenameFilter() {
+ @Override
+ public boolean accept(File dir, String name) {
+ return name.endsWith(".yang") && new File(dir, name).isFile();
+ }
+ });
for (File yangFile : yangFiles) {
- yangsFromDependencies.add(new NamedFileInputStream(
- yangFile));
+ yangsFromDependencies.add(new NamedFileInputStream(yangFile));
}
}
ZipEntry entry = entries.nextElement();
String entryName = entry.getName();
- if (entryName
- .startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)) {
- if (entry.isDirectory() == false
- && entryName.endsWith(".yang")) {
+ if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)) {
+ if (entry.isDirectory() == false && entryName.endsWith(".yang")) {
foundFilesForReporting.add(entryName);
// This will be closed after all strams are
// parsed.
- InputStream entryStream = zip
- .getInputStream(entry);
+ InputStream entryStream = zip.getInputStream(entry);
yangsFromDependencies.add(entryStream);
}
}
}
}
if (foundFilesForReporting.size() > 0) {
- log.info(Util.message("Found %d yang files in %s: %s",
- YangToSourcesProcessor.LOG_PREFIX,
- foundFilesForReporting.size(), file,
- foundFilesForReporting));
+ log.info(Util.message("Found %d yang files in %s: %s", YangToSourcesProcessor.LOG_PREFIX,
+ foundFilesForReporting.size(), file, foundFilesForReporting));
}
}
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.util.FileUtils;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
class YangToSourcesProcessor {
static final String LOG_PREFIX = "yang-to-sources:";
- static final String META_INF_YANG_STRING = "META-INF" + File.separator
- + "yang";
+ static final String META_INF_YANG_STRING = "META-INF" + File.separator + "yang";
static final String META_INF_YANG_STRING_JAR = "META-INF" + "/" + "yang";
static final File META_INF_YANG_DIR = new File(META_INF_YANG_STRING);
private final Log log;
private final File yangFilesRootDir;
+ private final File[] excludedFiles;
private final List<CodeGeneratorArg> codeGenerators;
private final MavenProject project;
private final boolean inspectDependencies;
private YangProvider yangProvider;
@VisibleForTesting
- YangToSourcesProcessor(Log log, File yangFilesRootDir,
- List<CodeGeneratorArg> codeGenerators, MavenProject project,
- boolean inspectDependencies, YangProvider yangProvider) {
+ YangToSourcesProcessor(Log log, File yangFilesRootDir, File[] excludedFiles, List<CodeGeneratorArg> codeGenerators,
+ MavenProject project, boolean inspectDependencies, YangProvider yangProvider) {
this.log = Util.checkNotNull(log, "log");
- this.yangFilesRootDir = Util.checkNotNull(yangFilesRootDir,
- "yangFilesRootDir");
- this.codeGenerators = Collections.unmodifiableList(Util.checkNotNull(
- codeGenerators, "codeGenerators"));
+ this.yangFilesRootDir = Util.checkNotNull(yangFilesRootDir, "yangFilesRootDir");
+ this.excludedFiles = excludedFiles;
+ this.codeGenerators = Collections.unmodifiableList(Util.checkNotNull(codeGenerators, "codeGenerators"));
this.project = Util.checkNotNull(project, "project");
this.inspectDependencies = inspectDependencies;
this.yangProvider = yangProvider;
}
- YangToSourcesProcessor(Log log, File yangFilesRootDir,
- List<CodeGeneratorArg> codeGenerators, MavenProject project,
- boolean inspectDependencies) {
- this(log, yangFilesRootDir, codeGenerators, project,
- inspectDependencies, new YangProvider());
+ YangToSourcesProcessor(Log log, File yangFilesRootDir, File[] excludedFiles, List<CodeGeneratorArg> codeGenerators,
+ MavenProject project, boolean inspectDependencies) {
+ this(log, yangFilesRootDir, excludedFiles, codeGenerators, project, inspectDependencies, new YangProvider());
}
public void execute() throws MojoExecutionException, MojoFailureException {
ContextHolder context = processYang();
generateSources(context);
- yangProvider.addYangsToMETA_INF(log, project, yangFilesRootDir);
+ yangProvider.addYangsToMETA_INF(log, project, yangFilesRootDir, excludedFiles);
}
private ContextHolder processYang() throws MojoExecutionException {
List<Closeable> closeables = new ArrayList<>();
log.info(Util.message("Inspecting %s", LOG_PREFIX, yangFilesRootDir));
try {
- List<InputStream> yangsInProject = Util
- .listFilesAsStream(yangFilesRootDir);
+ List<InputStream> yangsInProject = Util.listFilesAsStream(yangFilesRootDir, excludedFiles, log);
List<InputStream> all = new ArrayList<>(yangsInProject);
closeables.addAll(yangsInProject);
Map<InputStream, Module> allYangModules;
Set<Module> projectYangModules;
try {
if (inspectDependencies) {
- YangsInZipsResult dependentYangResult = Util
- .findYangFilesInDependenciesAsStream(log, project);
+ YangsInZipsResult dependentYangResult = Util.findYangFilesInDependenciesAsStream(log, project);
Closeable dependentYangResult1 = dependentYangResult;
closeables.add(dependentYangResult1);
all.addAll(dependentYangResult.yangStreams);
}
}
- Set<Module> parsedAllYangModules = new HashSet<>(
- allYangModules.values());
- SchemaContext resolveSchemaContext = parser
- .resolveSchemaContext(parsedAllYangModules);
- log.info(Util.message("%s files parsed from %s", LOG_PREFIX,
- Util.YANG_SUFFIX.toUpperCase(), yangsInProject));
+ Set<Module> parsedAllYangModules = new HashSet<>(allYangModules.values());
+ SchemaContext resolveSchemaContext = parser.resolveSchemaContext(parsedAllYangModules);
+ log.info(Util.message("%s files parsed from %s", LOG_PREFIX, Util.YANG_SUFFIX.toUpperCase(), yangsInProject));
return new ContextHolder(resolveSchemaContext, projectYangModules);
// MojoExecutionException is thrown since execution cannot continue
} catch (Exception e) {
- String message = Util.message("Unable to parse %s files from %s",
- LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir);
+ String message = Util.message("Unable to parse %s files from %s", LOG_PREFIX, Util.YANG_SUFFIX,
+ yangFilesRootDir);
log.error(message, e);
throw new MojoExecutionException(message, e);
}
static class YangProvider {
- private static final String yangResourceDir = "target" + File.separator
- + "yang";
+ private static final String yangResourceDir = "target" + File.separator + "yang";
- void addYangsToMETA_INF(Log log, MavenProject project,
- File yangFilesRootDir) throws MojoFailureException {
+ void addYangsToMETA_INF(Log log, MavenProject project, File yangFilesRootDir, File[] excludedFiles)
+ throws MojoFailureException {
File targetYangDir = new File(project.getBasedir(), yangResourceDir);
try {
- FileUtils.copyDirectory(yangFilesRootDir, targetYangDir);
+ Collection<File> files = Util.listFiles(yangFilesRootDir, excludedFiles, null);
+ for (File file : files) {
+ org.apache.commons.io.FileUtils.copyFile(file, new File(targetYangDir, file.getName()));
+ }
} catch (IOException e) {
- String message = "Unable to copy yang files into resource folder";
+ String message = "Unable to list yang files into resource folder";
log.warn(message, e);
throw new MojoFailureException(message, e);
}
setResource(targetYangDir, META_INF_YANG_STRING_JAR, project);
- log.debug(Util.message(
- "Yang files from: %s marked as resources: %s", LOG_PREFIX,
- yangFilesRootDir, META_INF_YANG_STRING_JAR));
+ log.debug(Util.message("Yang files from: %s marked as resources: %s", LOG_PREFIX, yangFilesRootDir,
+ META_INF_YANG_STRING_JAR));
}
- private static void setResource(File targetYangDir, String targetPath,
- MavenProject project) {
+ private static void setResource(File targetYangDir, String targetPath, MavenProject project) {
Resource res = new Resource();
res.setDirectory(targetYangDir.getPath());
if (targetPath != null)
/**
* Call generate on every generator from plugin configuration
*/
- private void generateSources(ContextHolder context)
- throws MojoFailureException {
+ private void generateSources(ContextHolder context) throws MojoFailureException {
if (codeGenerators.size() == 0) {
log.warn(Util.message("No code generators provided", LOG_PREFIX));
return;
generateSourcesWithOneGenerator(context, codeGenerator);
} catch (Exception e) {
// try other generators, exception will be thrown after
- log.error(Util.message(
- "Unable to generate sources with %s generator",
- LOG_PREFIX, codeGenerator.getCodeGeneratorClass()), e);
- thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
- .getCanonicalName());
+ log.error(
+ Util.message("Unable to generate sources with %s generator", LOG_PREFIX,
+ codeGenerator.getCodeGeneratorClass()), e);
+ thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass().getCanonicalName());
}
}
if (!thrown.isEmpty()) {
- String message = Util
- .message(
- "One or more code generators failed, including failed list(generatorClass=exception) %s",
- LOG_PREFIX, thrown.toString());
+ String message = Util.message(
+ "One or more code generators failed, including failed list(generatorClass=exception) %s",
+ LOG_PREFIX, thrown.toString());
log.error(message);
throw new MojoFailureException(message);
}
/**
* Instantiate generator from class and call required method
*/
- private void generateSourcesWithOneGenerator(ContextHolder context,
- CodeGeneratorArg codeGeneratorCfg) throws ClassNotFoundException,
- InstantiationException, IllegalAccessException, IOException {
+ private void generateSourcesWithOneGenerator(ContextHolder context, CodeGeneratorArg codeGeneratorCfg)
+ throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
codeGeneratorCfg.check();
- CodeGenerator g = Util.getInstance(
- codeGeneratorCfg.getCodeGeneratorClass(), CodeGenerator.class);
- log.info(Util.message("Code generator instantiated from %s",
- LOG_PREFIX, codeGeneratorCfg.getCodeGeneratorClass()));
+ CodeGenerator g = Util.getInstance(codeGeneratorCfg.getCodeGeneratorClass(), CodeGenerator.class);
+ log.info(Util.message("Code generator instantiated from %s", LOG_PREFIX,
+ codeGeneratorCfg.getCodeGeneratorClass()));
File outputDir = codeGeneratorCfg.getOutputBaseDir(project);
- log.info(Util.message("Sources will be generated to %s", LOG_PREFIX,
- outputDir));
- log.debug(Util.message("Project root dir is %s", LOG_PREFIX,
- project.getBasedir()));
- log.debug(Util.message(
- "Additional configuration picked up for : %s: %s", LOG_PREFIX,
- codeGeneratorCfg.getCodeGeneratorClass(),
- codeGeneratorCfg.getAdditionalConfiguration()));
+ log.info(Util.message("Sources will be generated to %s", LOG_PREFIX, outputDir));
+ log.debug(Util.message("Project root dir is %s", LOG_PREFIX, project.getBasedir()));
+ log.debug(Util.message("Additional configuration picked up for : %s: %s", LOG_PREFIX,
+ codeGeneratorCfg.getCodeGeneratorClass(), codeGeneratorCfg.getAdditionalConfiguration()));
- if(outputDir != null) {
+ if (outputDir != null) {
project.addCompileSourceRoot(outputDir.getAbsolutePath());
}
g.setLog(log);
YangProvider.setResource(resourceBaseDir, null, project);
g.setResourceBaseDir(resourceBaseDir);
- log.debug(Util.message(
- "Folder: %s marked as resources for generator: %s", LOG_PREFIX,
- resourceBaseDir, codeGeneratorCfg.getCodeGeneratorClass()));
+ log.debug(Util.message("Folder: %s marked as resources for generator: %s", LOG_PREFIX, resourceBaseDir,
+ codeGeneratorCfg.getCodeGeneratorClass()));
- Collection<File> generated = g.generateSources(context.getContext(),
- outputDir, context.getYangModules());
+ Collection<File> generated = g.generateSources(context.getContext(), outputDir, context.getYangModules());
- log.info(Util.message("Sources generated by %s: %s", LOG_PREFIX,
- codeGeneratorCfg.getCodeGeneratorClass(), generated));
+ log.info(Util.message("Sources generated by %s: %s", LOG_PREFIX, codeGeneratorCfg.getCodeGeneratorClass(),
+ generated));
}
}