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