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