2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang2sources.plugin;
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.List;
15 import org.apache.maven.plugin.AbstractMojo;
16 import org.apache.maven.plugin.MojoExecutionException;
17 import org.apache.maven.plugin.MojoFailureException;
18 import org.apache.maven.plugins.annotations.LifecyclePhase;
19 import org.apache.maven.plugins.annotations.Mojo;
20 import org.apache.maven.plugins.annotations.Parameter;
21 import org.apache.maven.plugins.annotations.ResolutionScope;
22 import org.apache.maven.project.MavenProject;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
25 import org.slf4j.impl.StaticLoggerBinder;
27 import com.google.common.annotations.VisibleForTesting;
30 * Generate sources from yang files using user provided set of
31 * {@link CodeGenerator}s. Steps of this process:
33 * <li>List yang files from {@link #yangFilesRootDir}</li>
34 * <li>Process yang files using {@link YangModelParserImpl}</li>
35 * <li>For each {@link CodeGenerator} from {@link #codeGenerators}:</li>
37 * <li>Instantiate using default constructor</li>
38 * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File)}</li>
42 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
43 public final class YangToSourcesMojo extends AbstractMojo {
46 * Classes implementing {@link CodeGenerator} interface. An instance will be
47 * created out of every class using default constructor. Method {@link
48 * CodeGenerator#generateSources(SchemaContext, File, Set<String>
49 * yangModulesNames)} will be called on every instance.
51 @Parameter(required = false)
52 private CodeGeneratorArg[] codeGenerators;
55 * Source directory that will be recursively searched for yang files (ending
58 @Parameter(required = false)
59 private String yangFilesRootDir; // defaults to ${basedir}/src/main/yang
61 @Parameter(required = false)
62 private String[] excludeFiles;
64 @Parameter(property = "project", required = true, readonly = true)
65 protected MavenProject project;
67 @Parameter(property = "inspectDependencies", required = true, readonly = true)
68 private boolean inspectDependencies;
70 private YangToSourcesProcessor yangToSourcesProcessor;
72 public YangToSourcesMojo() {
77 YangToSourcesMojo(YangToSourcesProcessor processor) {
78 this.yangToSourcesProcessor = processor;
82 public void execute() throws MojoExecutionException, MojoFailureException {
83 StaticLoggerBinder.SINGLETON.setLog(getLog());
84 if (yangToSourcesProcessor == null) {
85 List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
87 // defaults to ${basedir}/src/main/yang
88 File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
89 File[] excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
91 yangToSourcesProcessor = new YangToSourcesProcessor(getLog(), yangFilesRootFile, excludedFiles,
92 codeGeneratorArgs, project, inspectDependencies);
94 yangToSourcesProcessor.execute();
97 private static List<CodeGeneratorArg> processCodeGenerators(CodeGeneratorArg[] codeGenerators) {
98 List<CodeGeneratorArg> codeGeneratorArgs;
99 if (codeGenerators == null) {
100 codeGeneratorArgs = Collections.emptyList();
102 codeGeneratorArgs = Arrays.asList(codeGenerators);
104 return codeGeneratorArgs;
107 private static File processYangFilesRootDir(String yangFilesRootDir, File baseDir) {
108 File yangFilesRootFile;
109 if (yangFilesRootDir == null) {
110 yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
112 File file = new File(yangFilesRootDir);
113 if (file.isAbsolute()) {
114 yangFilesRootFile = file;
116 yangFilesRootFile = new File(baseDir, file.getPath());
119 return yangFilesRootFile;
122 private static File[] processExcludeFiles(String[] excludeFiles, File baseDir) {
123 if (excludeFiles == null) {
124 return new File[] {};
126 File[] result = new File[excludeFiles.length];
127 for (int i = 0; i < excludeFiles.length; i++) {
128 result[i] = new File(baseDir, excludeFiles[i]);