Added 'code-generator' and 'model' projects from controller project.
[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.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
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     private String yangFilesRootDir; // defaults to ${basedir}/src/main/yang
59
60     @Parameter(property = "project", required = true, readonly = true)
61     protected MavenProject project;
62
63     @Parameter(property = "inspectDependencies", required = true, readonly = true)
64     private boolean inspectDependencies;
65
66     private YangToSourcesProcessor yangToSourcesProcessor;
67
68     public YangToSourcesMojo() {
69
70     }
71
72     @VisibleForTesting
73     YangToSourcesMojo(YangToSourcesProcessor processor) {
74         this.yangToSourcesProcessor = processor;
75     }
76
77     @Override
78     public void execute() throws MojoExecutionException, MojoFailureException {
79         if (yangToSourcesProcessor == null) {
80             List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
81
82             // defaults to ${basedir}/src/main/yang
83             File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir,
84                     project.getBasedir());
85
86             yangToSourcesProcessor = new YangToSourcesProcessor(getLog(),
87                     yangFilesRootFile, codeGeneratorArgs, project,
88                     inspectDependencies);
89         }
90         yangToSourcesProcessor.execute();
91     }
92
93     private static List<CodeGeneratorArg> processCodeGenerators(
94             CodeGeneratorArg[] codeGenerators) {
95         List<CodeGeneratorArg> codeGeneratorArgs;
96         if (codeGenerators == null) {
97             codeGeneratorArgs = Collections.emptyList();
98         } else {
99             codeGeneratorArgs = Arrays.asList(codeGenerators);
100         }
101         return codeGeneratorArgs;
102     }
103
104     private static File processYangFilesRootDir(String yangFilesRootDir,
105             File baseDir) {
106         File yangFilesRootFile;
107         if (yangFilesRootDir == null) {
108             yangFilesRootFile = new File(baseDir, "src" + File.separator
109                     + "main" + File.separator + "yang");
110         } else {
111             File file = new File(yangFilesRootDir);
112             if (file.isAbsolute()) {
113                 yangFilesRootFile = file;
114             } else {
115                 yangFilesRootFile = new File(baseDir, file.getPath());
116             }
117         }
118         return yangFilesRootFile;
119     }
120 }