07c3edcffe432b8348adb57e97adee8b0a407bdb
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / YangToSourcesMojo.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.yangtools.yang2sources.plugin;
9
10 import java.io.File;
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.List;
14
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;
26
27 import com.google.common.annotations.VisibleForTesting;
28
29 /**
30  * Generate sources from yang files using user provided set of
31  * {@link CodeGenerator}s. Steps of this process:
32  * <ol>
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>
36  * <ol>
37  * <li>Instantiate using default constructor</li>
38  * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File)}</li>
39  * </ol>
40  * </ol>
41  */
42 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
43 public final class YangToSourcesMojo extends AbstractMojo {
44
45     /**
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.
50      */
51     @Parameter(required = false)
52     private CodeGeneratorArg[] codeGenerators;
53
54     /**
55      * Source directory that will be recursively searched for yang files (ending
56      * with .yang suffix).
57      */
58     @Parameter(required = false)
59     private String yangFilesRootDir; // defaults to ${basedir}/src/main/yang
60
61     @Parameter(property = "project", required = true, readonly = true)
62     protected MavenProject project;
63
64     @Parameter(property = "inspectDependencies", required = true, readonly = true)
65     private boolean inspectDependencies;
66
67     private YangToSourcesProcessor yangToSourcesProcessor;
68
69     public YangToSourcesMojo() {
70
71     }
72
73     @VisibleForTesting
74     YangToSourcesMojo(YangToSourcesProcessor processor) {
75         this.yangToSourcesProcessor = processor;
76     }
77
78     @Override
79     public void execute() throws MojoExecutionException, MojoFailureException {
80         StaticLoggerBinder.SINGLETON.setLog(getLog());
81         if (yangToSourcesProcessor == null) {
82             List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
83
84             // defaults to ${basedir}/src/main/yang
85             File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir,
86                     project.getBasedir());
87
88             yangToSourcesProcessor = new YangToSourcesProcessor(getLog(),
89                     yangFilesRootFile, codeGeneratorArgs, project,
90                     inspectDependencies);
91         }
92         yangToSourcesProcessor.execute();
93     }
94
95     private static List<CodeGeneratorArg> processCodeGenerators(
96             CodeGeneratorArg[] codeGenerators) {
97         List<CodeGeneratorArg> codeGeneratorArgs;
98         if (codeGenerators == null) {
99             codeGeneratorArgs = Collections.emptyList();
100         } else {
101             codeGeneratorArgs = Arrays.asList(codeGenerators);
102         }
103         return codeGeneratorArgs;
104     }
105
106     private static File processYangFilesRootDir(String yangFilesRootDir,
107             File baseDir) {
108         File yangFilesRootFile;
109         if (yangFilesRootDir == null) {
110             yangFilesRootFile = new File(baseDir, "src" + File.separator
111                     + "main" + File.separator + "yang");
112         } else {
113             File file = new File(yangFilesRootDir);
114             if (file.isAbsolute()) {
115                 yangFilesRootFile = file;
116             } else {
117                 yangFilesRootFile = new File(baseDir, file.getPath());
118             }
119         }
120         return yangFilesRootFile;
121     }
122 }