17ca037022d676dafc3a4ff05ac06102c8eaa9a1
[yangtools.git] / plugin / 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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.ImmutableList;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.File;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Set;
19 import org.apache.maven.artifact.repository.ArtifactRepository;
20 import org.apache.maven.plugin.AbstractMojo;
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.plugins.annotations.Component;
24 import org.apache.maven.plugins.annotations.LifecyclePhase;
25 import org.apache.maven.plugins.annotations.Mojo;
26 import org.apache.maven.plugins.annotations.Parameter;
27 import org.apache.maven.plugins.annotations.ResolutionScope;
28 import org.apache.maven.project.MavenProject;
29 import org.apache.maven.repository.RepositorySystem;
30 import org.opendaylight.yangtools.plugin.generator.api.FileGenerator;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
33 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
34 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
35 import org.sonatype.plexus.build.incremental.BuildContext;
36
37 /**
38  * Generate sources from yang files using user provided set of
39  * {@link BasicCodeGenerator}s. Steps of this process:
40  * <ol>
41  *   <li>List yang files from {@link #yangFilesRootDir}</li>
42  *   <li>Process yang files using Yang Parser</li>
43  *   <li>For each {@link BasicCodeGenerator} from {@link #codeGenerators}:
44  *     <ol>
45  *       <li>Instantiate using default constructor</li>
46  *       <li>Call {@link BasicCodeGenerator#generateSources(EffectiveModelContext, File, Set,
47  *           org.opendaylight.yangtools.yang2sources.spi.ModuleResourceResolver)}</li>
48  *     </ol>
49  *   </li>
50  * </ol>
51  */
52 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
53     requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true, threadSafe = true)
54 public final class YangToSourcesMojo extends AbstractMojo {
55     public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
56
57     /**
58      * Classes implementing {@link BasicCodeGenerator} interface. An instance will be
59      * created out of every class using default constructor. Method {@link
60      * BasicCodeGenerator#generateSources(EffectiveModelContext, File, Set)} will be called on every instance.
61      */
62     @Parameter(required = false)
63     private CodeGeneratorArg[] codeGenerators;
64
65     /**
66      * {@link FileGenerator} instances resolved via ServiceLoader can hold additional configuration, which details
67      * how they are executed.
68      */
69     @Parameter(required = false)
70     private FileGeneratorArg[] fileGenerators;
71
72     /**
73      * Source directory that will be recursively searched for yang files (ending
74      * with .yang suffix).
75      */
76     @Parameter(required = false)
77     // defaults to ${basedir}/src/main/yang
78     private String yangFilesRootDir;
79
80     @Parameter(required = false)
81     private String[] excludeFiles;
82
83     @Parameter(property = "project", required = true, readonly = true)
84     private MavenProject project;
85
86     @Parameter(property = "inspectDependencies")
87     private boolean inspectDependencies;
88
89     @Component
90     @VisibleForTesting
91     BuildContext buildContext;
92
93     private YangToSourcesProcessor yangToSourcesProcessor;
94
95     @Component
96     private RepositorySystem repoSystem;
97
98     @Parameter(readonly = true, defaultValue = "${localRepository}")
99     private ArtifactRepository localRepository;
100
101     @Parameter(readonly = true, defaultValue = "${project.remoteArtifactRepositories}")
102     private List<ArtifactRepository> remoteRepos;
103
104     // When set to "true", then the execution of the plugin is disabled
105     @Parameter(property = "yang.skip")
106     private String yangSkip;
107
108     @Parameter(defaultValue = "DEFAULT_MODE")
109     @Deprecated(forRemoval = true)
110     private StatementParserMode parserMode;
111
112     public YangToSourcesMojo() {
113
114     }
115
116     @VisibleForTesting
117     YangToSourcesMojo(final YangToSourcesProcessor processor) {
118         this.yangToSourcesProcessor = processor;
119     }
120
121     public void setProject(final MavenProject project) {
122         this.project = project;
123     }
124
125     @Override
126     @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "yangFilesRootDir")
127     public void execute() throws MojoExecutionException, MojoFailureException {
128         Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
129
130         if (yangToSourcesProcessor == null) {
131             // defaults to ${basedir}/src/main/yang
132             File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
133             Collection<File> excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
134
135             yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, yangFilesRootFile,
136                     excludedFiles, arrayToList(codeGenerators), arrayToList(fileGenerators), project,
137                     inspectDependencies);
138         }
139         yangToSourcesProcessor.conditionalExecute("true".equals(yangSkip));
140     }
141
142     private static <T> List<T> arrayToList(final T[] array) {
143         return array == null ? ImmutableList.of() : Arrays.asList(array);
144     }
145
146     private static File processYangFilesRootDir(final String yangFilesRootDir, final File baseDir) {
147         File yangFilesRootFile;
148         if (yangFilesRootDir == null) {
149             yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
150         } else {
151             File file = new File(yangFilesRootDir);
152             if (file.isAbsolute()) {
153                 yangFilesRootFile = file;
154             } else {
155                 yangFilesRootFile = new File(baseDir, file.getPath());
156             }
157         }
158         return yangFilesRootFile;
159     }
160
161     private static Collection<File> processExcludeFiles(final String[] excludeFiles, final File baseDir) {
162         if (excludeFiles == null) {
163             return ImmutableList.of();
164         }
165
166         return Collections2.transform(Arrays.asList(excludeFiles), f -> new File(baseDir, f));
167     }
168 }