Merge branch 'master' of ../controller
[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 java.util.function.Function;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.plugin.AbstractMojo;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.plugins.annotations.Component;
26 import org.apache.maven.plugins.annotations.LifecyclePhase;
27 import org.apache.maven.plugins.annotations.Mojo;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.apache.maven.plugins.annotations.ResolutionScope;
30 import org.apache.maven.project.MavenProject;
31 import org.apache.maven.repository.RepositorySystem;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
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, Function)}</li>
47  * </ol></li>
48  * </ol>
49  */
50 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
51     requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
52 public final class YangToSourcesMojo extends AbstractMojo {
53     public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
54
55     /**
56      * Classes implementing {@link BasicCodeGenerator} interface. An instance will be
57      * created out of every class using default constructor. Method {@link
58      * BasicCodeGenerator#generateSources(SchemaContext, File, Set)} 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")
78     private boolean inspectDependencies;
79
80     @Component
81     @VisibleForTesting
82     BuildContext buildContext;
83
84     private YangToSourcesProcessor yangToSourcesProcessor;
85
86     @Component
87     private RepositorySystem repoSystem;
88
89     @Parameter(readonly = true, defaultValue = "${localRepository}")
90     private ArtifactRepository localRepository;
91
92     @Parameter(readonly = true, defaultValue = "${project.remoteArtifactRepositories}")
93     private List<ArtifactRepository> remoteRepos;
94
95     // When set to "true", then the execution of the plugin is disabled
96     @Parameter(property = "yang.skip")
97     private String yangSkip;
98
99     public YangToSourcesMojo() {
100
101     }
102
103     @VisibleForTesting
104     YangToSourcesMojo(final YangToSourcesProcessor processor) {
105         this.yangToSourcesProcessor = processor;
106     }
107
108     public void setProject(final MavenProject project) {
109         this.project = project;
110     }
111
112     @Override
113     @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "yangFilesRootDir")
114     public void execute() throws MojoExecutionException, MojoFailureException {
115         Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
116
117         if (yangToSourcesProcessor == null) {
118             List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
119
120             // defaults to ${basedir}/src/main/yang
121             File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
122             Collection<File> excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
123
124             yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, yangFilesRootFile,
125                     excludedFiles, codeGeneratorArgs, project, inspectDependencies);
126         }
127         yangToSourcesProcessor.conditionalExecute("true".equals(yangSkip));
128     }
129
130     private static List<CodeGeneratorArg> processCodeGenerators(final CodeGeneratorArg[] codeGenerators) {
131         List<CodeGeneratorArg> codeGeneratorArgs;
132         if (codeGenerators == null) {
133             codeGeneratorArgs = Collections.emptyList();
134         } else {
135             codeGeneratorArgs = Arrays.asList(codeGenerators);
136         }
137         return codeGeneratorArgs;
138     }
139
140     private static File processYangFilesRootDir(final String yangFilesRootDir, final File baseDir) {
141         File yangFilesRootFile;
142         if (yangFilesRootDir == null) {
143             yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
144         } else {
145             File file = new File(yangFilesRootDir);
146             if (file.isAbsolute()) {
147                 yangFilesRootFile = file;
148             } else {
149                 yangFilesRootFile = new File(baseDir, file.getPath());
150             }
151         }
152         return yangFilesRootFile;
153     }
154
155     private static Collection<File> processExcludeFiles(final String[] excludeFiles, final File baseDir) {
156         if (excludeFiles == null) {
157             return ImmutableList.of();
158         }
159
160         return Collections2.transform(Arrays.asList(excludeFiles), f -> new File(baseDir, f));
161     }
162
163 }