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