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