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