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;
16 import org.apache.maven.plugin.AbstractMojo;
17 import org.apache.maven.plugin.MojoExecutionException;
18 import org.apache.maven.plugin.MojoFailureException;
19 import org.apache.maven.plugins.annotations.Component;
20 import org.apache.maven.plugins.annotations.LifecyclePhase;
21 import org.apache.maven.plugins.annotations.Mojo;
22 import org.apache.maven.plugins.annotations.Parameter;
23 import org.apache.maven.plugins.annotations.ResolutionScope;
24 import org.apache.maven.project.MavenProject;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
27 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
28 import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
29 import org.slf4j.impl.StaticLoggerBinder;
30 import org.sonatype.plexus.build.incremental.BuildContext;
32 import com.google.common.annotations.VisibleForTesting;
35 * Generate sources from yang files using user provided set of
36 * {@link CodeGenerator}s. Steps of this process:
38 * <li>List yang files from {@link #yangFilesRootDir}</li>
39 * <li>Process yang files using {@link YangParserImpl}</li>
40 * <li>For each {@link CodeGenerator} from {@link #codeGenerators}:</li>
42 * <li>Instantiate using default constructor</li>
43 * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File, Set)}</li>
47 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
48 public final class YangToSourcesMojo extends AbstractMojo {
51 * Classes implementing {@link CodeGenerator} interface. An instance will be
52 * created out of every class using default constructor. Method {@link
53 * CodeGenerator#generateSources(SchemaContext, File, Set<String>
54 * yangModulesNames)} will be called on every instance.
56 @Parameter(required = false)
57 private CodeGeneratorArg[] codeGenerators;
60 * Source directory that will be recursively searched for yang files (ending
63 @Parameter(required = false)
64 // defaults to ${basedir}/src/main/yang
65 private String yangFilesRootDir;
67 @Parameter(required = false)
68 private String[] excludeFiles;
70 @Parameter(property = "project", required = true, readonly = true)
71 private MavenProject project;
73 @Parameter(property = "inspectDependencies", required = true, readonly = true)
74 private boolean inspectDependencies;
77 private BuildContext buildContext;
79 private YangToSourcesProcessor yangToSourcesProcessor;
81 public YangToSourcesMojo() {
85 public void setProject(MavenProject project) {
86 this.project = project;
90 YangToSourcesMojo(YangToSourcesProcessor processor) {
91 this.yangToSourcesProcessor = processor;
95 public void execute() throws MojoExecutionException, MojoFailureException {
96 StaticLoggerBinder.SINGLETON.setLog(getLog());
97 if (yangToSourcesProcessor == null) {
98 List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
100 // defaults to ${basedir}/src/main/yang
101 File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
102 File[] excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
104 yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, getLog(), yangFilesRootFile, excludedFiles,
105 codeGeneratorArgs, project, inspectDependencies);
107 yangToSourcesProcessor.execute();
110 private static List<CodeGeneratorArg> processCodeGenerators(CodeGeneratorArg[] codeGenerators) {
111 List<CodeGeneratorArg> codeGeneratorArgs;
112 if (codeGenerators == null) {
113 codeGeneratorArgs = Collections.emptyList();
115 codeGeneratorArgs = Arrays.asList(codeGenerators);
117 return codeGeneratorArgs;
120 private static File processYangFilesRootDir(String yangFilesRootDir, File baseDir) {
121 File yangFilesRootFile;
122 if (yangFilesRootDir == null) {
123 yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
125 File file = new File(yangFilesRootDir);
126 if (file.isAbsolute()) {
127 yangFilesRootFile = file;
129 yangFilesRootFile = new File(baseDir, file.getPath());
132 return yangFilesRootFile;
135 private static File[] processExcludeFiles(String[] excludeFiles, File baseDir) {
136 if (excludeFiles == null) {
137 return new File[] {};
139 File[] result = new File[excludeFiles.length];
140 for (int i = 0; i < excludeFiles.length; i++) {
141 result[i] = new File(baseDir, excludeFiles[i]);