05d9c0cd2c54488e5815b4701ec862c7744f89f3
[controller.git] / opendaylight / sal / yang-prototype / code-generator / maven-yang-plugin / src / main / java / org / opendaylight / controller / 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.controller.yang2sources.plugin;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Map;
15 import java.util.Set;
16
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.LifecyclePhase;
21 import org.apache.maven.plugins.annotations.Mojo;
22 import org.apache.maven.plugins.annotations.Parameter;
23 import org.apache.maven.plugins.annotations.ResolutionScope;
24 import org.apache.maven.project.MavenProject;
25 import org.opendaylight.controller.yang.model.api.Module;
26 import org.opendaylight.controller.yang.model.api.SchemaContext;
27 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
28 import org.opendaylight.controller.yang.model.parser.impl.YangParserImpl;
29 import org.opendaylight.controller.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
30 import org.opendaylight.controller.yang2sources.spi.CodeGenerator;
31
32 import com.google.common.annotations.VisibleForTesting;
33 import com.google.common.collect.Maps;
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 YangModelParserImpl}</li>
41  * <li>For each {@link CodeGenerator} from {@link #codeGenerators}:</li>
42  * <ol>
43  * <li>Instantiate using default constructor</li>
44  * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File)}</li>
45  * </ol>
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
51     private static final String LOG_PREFIX = "yang-to-sources:";
52
53     /**
54      * Classes implementing {@link CodeGenerator} interface. An instance will be
55      * created out of every class using default constructor. Method
56      * {@link CodeGenerator#generateSources(SchemaContext, File)} will be called
57      * on every instance.
58      */
59     @Parameter(required = true)
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 = true)
67     private String yangFilesRootDir;
68
69     @Parameter(property = "project", required = true, readonly = true)
70     protected MavenProject project;
71
72     private transient final YangModelParser parser;
73
74     @VisibleForTesting
75     YangToSourcesMojo(CodeGeneratorArg[] codeGeneratorArgs,
76             YangModelParser parser, String yangFilesRootDir) {
77         super();
78         this.codeGenerators = codeGeneratorArgs;
79         this.yangFilesRootDir = yangFilesRootDir;
80         this.parser = parser;
81     }
82
83     public YangToSourcesMojo() {
84         super();
85         parser = new YangParserImpl();
86     }
87
88     @Override
89     public void execute() throws MojoExecutionException, MojoFailureException {
90         SchemaContext context = processYang();
91         generateSources(context);
92     }
93
94     /**
95      * Generate {@link SchemaContext} with {@link YangModelParserImpl}
96      */
97     private SchemaContext processYang() throws MojoExecutionException {
98         try {
99             Collection<File> yangFiles = Util.listFiles(yangFilesRootDir);
100
101             if (yangFiles.isEmpty()) {
102                 getLog().warn(
103                         Util.message("No %s file found in %s", LOG_PREFIX,
104                                 Util.YANG_SUFFIX, yangFilesRootDir));
105                 return null;
106             }
107
108             Set<Module> parsedYang = parser
109                     .parseYangModels(new ArrayList<File>(yangFiles));
110             SchemaContext resolveSchemaContext = parser
111                     .resolveSchemaContext(parsedYang);
112             getLog().info(
113                     Util.message("%s files parsed from %s", LOG_PREFIX,
114                             Util.YANG_SUFFIX, yangFiles));
115             return resolveSchemaContext;
116
117             // MojoExecutionException is thrown since execution cannot continue
118         } catch (Exception e) {
119             String message = Util.message("Unable to parse %s files from %s",
120                     LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir);
121             getLog().error(message, e);
122             throw new MojoExecutionException(message, e);
123         }
124     }
125
126     /**
127      * Call generate on every generator from plugin configuration
128      */
129     private void generateSources(SchemaContext context)
130             throws MojoFailureException {
131         if (codeGenerators.length == 0) {
132             getLog().warn(
133                     Util.message("No code generators provided", LOG_PREFIX));
134             return;
135         }
136
137         Map<String, String> thrown = Maps.newHashMap();
138
139         for (CodeGeneratorArg codeGenerator : codeGenerators) {
140             try {
141
142                 generateSourcesWithOneGenerator(context, codeGenerator);
143
144             } catch (Exception e) {
145                 // try other generators, exception will be thrown after
146                 getLog().error(
147                         Util.message(
148                                 "Unable to generate sources with %s generator",
149                                 LOG_PREFIX,
150                                 codeGenerator.getCodeGeneratorClass()), e);
151                 thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
152                         .getCanonicalName());
153             }
154         }
155
156         if (!thrown.isEmpty()) {
157             String message = Util
158                     .message(
159                             "One or more code generators failed, including failed list(generatorClass=exception) %s",
160                             LOG_PREFIX, thrown.toString());
161             getLog().error(message);
162             throw new MojoFailureException(message);
163         }
164     }
165
166     /**
167      * Instantiate generator from class and call required method
168      */
169     private void generateSourcesWithOneGenerator(SchemaContext context,
170             CodeGeneratorArg codeGeneratorCfg) throws ClassNotFoundException,
171             InstantiationException, IllegalAccessException, IOException {
172
173         codeGeneratorCfg.check();
174
175         CodeGenerator g = Util.getInstance(
176                 codeGeneratorCfg.getCodeGeneratorClass(), CodeGenerator.class);
177         getLog().info(
178                 Util.message("Code generator instantiated from %s", LOG_PREFIX,
179                         codeGeneratorCfg.getCodeGeneratorClass()));
180
181         File outputDir = codeGeneratorCfg.getOutputBaseDir();
182         if (project != null && outputDir != null) {
183             project.addCompileSourceRoot(outputDir.getPath());
184         }
185         Collection<File> generated = g.generateSources(context, outputDir);
186         getLog().info(
187                 Util.message("Sources generated by %s: %s", LOG_PREFIX,
188                         codeGeneratorCfg.getCodeGeneratorClass(), generated));
189     }
190
191 }