Fix for Bug 114.
[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 import java.util.Set;
15
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.aether.RepositorySystem;
31 import org.sonatype.aether.RepositorySystemSession;
32 import org.sonatype.aether.repository.RemoteRepository;
33 import org.sonatype.plexus.build.incremental.BuildContext;
34
35 import com.google.common.annotations.VisibleForTesting;
36
37 /**
38  * Generate sources from yang files using user provided set of
39  * {@link CodeGenerator}s. Steps of this process:
40  * <ol>
41  * <li>List yang files from {@link #yangFilesRootDir}</li>
42  * <li>Process yang files using {@link YangParserImpl}</li>
43  * <li>For each {@link CodeGenerator} from {@link #codeGenerators}:</li>
44  * <ol>
45  * <li>Instantiate using default constructor</li>
46  * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File, Set)}</li>
47  * </ol>
48  * </ol>
49  */
50 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
51 public final class YangToSourcesMojo extends AbstractMojo {
52     public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
53
54     /**
55      * Classes implementing {@link CodeGenerator} interface. An instance will be
56      * created out of every class using default constructor. Method {@link
57      * CodeGenerator#generateSources(SchemaContext, File, Set<String>
58      * yangModulesNames)} will be called on every instance.
59      */
60     @Parameter(required = false)
61     private CodeGeneratorArg[] codeGenerators;
62
63     /**
64      * Source directory that will be recursively searched for yang files (ending
65      * with .yang suffix).
66      */
67     @Parameter(required = false)
68     // defaults to ${basedir}/src/main/yang
69     private String yangFilesRootDir;
70
71     @Parameter(required = false)
72     private String[] excludeFiles;
73
74     @Parameter(property = "project", required = true, readonly = true)
75     private MavenProject project;
76
77     @Parameter(property = "inspectDependencies", required = true, readonly = true)
78     private boolean inspectDependencies;
79
80     @Component
81     private BuildContext buildContext;
82
83     private YangToSourcesProcessor yangToSourcesProcessor;
84
85     @Component
86     private RepositorySystem repoSystem;
87
88     @Parameter( readonly = true, defaultValue = "${repositorySystemSession}" )
89     private RepositorySystemSession repoSession;
90
91     @Parameter( readonly = true, defaultValue = "${project.remoteProjectRepositories}" )
92     private List<RemoteRepository> remoteRepos;
93
94
95     public YangToSourcesMojo() {
96
97     }
98
99     public void setProject(MavenProject project) {
100         this.project = project;
101     }
102
103     @VisibleForTesting
104     YangToSourcesMojo(YangToSourcesProcessor processor) {
105         this.yangToSourcesProcessor = processor;
106     }
107
108     @Override
109     public void execute() throws MojoExecutionException, MojoFailureException {
110         StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
111
112         Util.checkClasspath(project, getLog(), repoSystem, repoSession, remoteRepos);
113
114         if (yangToSourcesProcessor == null) {
115             List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
116
117             // defaults to ${basedir}/src/main/yang
118             File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
119             File[] excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
120
121             yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, getLog(), yangFilesRootFile, excludedFiles,
122                     codeGeneratorArgs, project, inspectDependencies);
123         }
124         yangToSourcesProcessor.execute();
125     }
126
127     private static List<CodeGeneratorArg> processCodeGenerators(CodeGeneratorArg[] codeGenerators) {
128         List<CodeGeneratorArg> codeGeneratorArgs;
129         if (codeGenerators == null) {
130             codeGeneratorArgs = Collections.emptyList();
131         } else {
132             codeGeneratorArgs = Arrays.asList(codeGenerators);
133         }
134         return codeGeneratorArgs;
135     }
136
137     private static File processYangFilesRootDir(String yangFilesRootDir, File baseDir) {
138         File yangFilesRootFile;
139         if (yangFilesRootDir == null) {
140             yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
141         } else {
142             File file = new File(yangFilesRootDir);
143             if (file.isAbsolute()) {
144                 yangFilesRootFile = file;
145             } else {
146                 yangFilesRootFile = new File(baseDir, file.getPath());
147             }
148         }
149         return yangFilesRootFile;
150     }
151
152     private static File[] processExcludeFiles(String[] excludeFiles, File baseDir) {
153         if (excludeFiles == null) {
154             return new File[] {};
155         }
156         File[] result = new File[excludeFiles.length];
157         for (int i = 0; i < excludeFiles.length; i++) {
158             result[i] = new File(baseDir, excludeFiles[i]);
159         }
160
161         return result;
162     }
163
164 }