2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang2sources.plugin;
11 import java.util.ArrayList;
12 import java.util.Collection;
16 import org.apache.maven.plugin.AbstractMojo;
17 import org.apache.maven.plugin.MojoExecutionException;
18 import org.apache.maven.plugin.MojoFailureException;
19 import org.apache.maven.plugins.annotations.LifecyclePhase;
20 import org.apache.maven.plugins.annotations.Mojo;
21 import org.apache.maven.plugins.annotations.Parameter;
22 import org.apache.maven.plugins.annotations.ResolutionScope;
23 import org.apache.maven.project.MavenProject;
24 import org.opendaylight.controller.yang.model.api.Module;
25 import org.opendaylight.controller.yang.model.api.SchemaContext;
26 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
27 import org.opendaylight.controller.yang.model.parser.impl.YangModelParserImpl;
28 import org.opendaylight.controller.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
29 import org.opendaylight.controller.yang2sources.spi.CodeGenerator;
31 import com.google.common.annotations.VisibleForTesting;
32 import com.google.common.collect.Maps;
35 * Generate sources from yang files using user provided set of
36 * {@link CodeGenerator}s. Steps of this process:
38 * <li>List yang files from {@link #yangFilesRootDir}</li>
39 * <li>Process yang files using {@link YangModelParserImpl}</li>
40 * <li>For each {@link CodeGenerator} from {@link #codeGenerators}:</li>
42 * <li>Instantiate using default constructor</li>
43 * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File)}</li>
47 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES,requiresDependencyResolution=ResolutionScope.COMPILE,requiresProject=true)
48 public final class YangToSourcesMojo extends AbstractMojo {
50 private static final String LOG_PREFIX = "yang-to-sources:";
53 * Classes implementing {@link CodeGenerator} interface. An instance will be
54 * created out of every class using default constructor. Method
55 * {@link CodeGenerator#generateSources(SchemaContext, File)} will be called
58 @Parameter(required = true)
59 private CodeGeneratorArg[] codeGenerators;
62 * Source directory that will be recursively searched for yang files (ending
65 @Parameter(required = true)
66 private String yangFilesRootDir;
69 @Parameter(property = "project", required = true, readonly = true)
70 protected MavenProject project;
73 private transient final YangModelParser parser;
76 YangToSourcesMojo(CodeGeneratorArg[] codeGeneratorArgs,
77 YangModelParser parser, String yangFilesRootDir) {
79 this.codeGenerators = codeGeneratorArgs;
80 this.yangFilesRootDir = yangFilesRootDir;
84 public YangToSourcesMojo() {
86 parser = new YangModelParserImpl();
90 public void execute() throws MojoExecutionException, MojoFailureException {
91 SchemaContext context = processYang();
92 generateSources(context);
96 * Generate {@link SchemaContext} with {@link YangModelParserImpl}
98 private SchemaContext processYang() throws MojoExecutionException {
100 Collection<File> yangFiles = Util.listFiles(yangFilesRootDir);
102 if (yangFiles.isEmpty()) {
104 Util.message("No %s file found in %s", LOG_PREFIX,
105 Util.YANG_SUFFIX, yangFilesRootDir));
109 Set<Module> parsedYang = parser.parseYangModels(new ArrayList<File>(yangFiles));
110 SchemaContext resolveSchemaContext = parser
111 .resolveSchemaContext(parsedYang);
113 Util.message("%s files parsed from %s", LOG_PREFIX,
114 Util.YANG_SUFFIX, yangFiles));
115 return resolveSchemaContext;
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);
127 * Call generate on every generator from plugin configuration
129 private void generateSources(SchemaContext context)
130 throws MojoFailureException {
131 if (codeGenerators.length == 0) {
133 Util.message("No code generators provided", LOG_PREFIX));
137 Map<String, String> thrown = Maps.newHashMap();
139 for (CodeGeneratorArg codeGenerator : codeGenerators) {
142 generateSourcesWithOneGenerator(context, codeGenerator);
144 } catch (Exception e) {
145 // try other generators, exception will be thrown after
148 "Unable to generate sources with %s generator",
150 codeGenerator.getCodeGeneratorClass()), e);
151 thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
152 .getCanonicalName());
156 if (!thrown.isEmpty()) {
157 String message = Util
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);
167 * Instantiate generator from class and call required method
169 private void generateSourcesWithOneGenerator(SchemaContext context,
170 CodeGeneratorArg codeGeneratorCfg) throws ClassNotFoundException,
171 InstantiationException, IllegalAccessException {
173 codeGeneratorCfg.check();
175 CodeGenerator g = Util.getInstance(
176 codeGeneratorCfg.getCodeGeneratorClass(), CodeGenerator.class);
178 Util.message("Code generator instantiated from %s", LOG_PREFIX,
179 codeGeneratorCfg.getCodeGeneratorClass()));
181 File outputDir = codeGeneratorCfg.getOutputBaseDir();
182 project.addCompileSourceRoot(outputDir.getPath());
183 Collection<File> generated = g.generateSources(context,
186 Util.message("Sources generated by %s: %s", LOG_PREFIX,
187 codeGeneratorCfg.getCodeGeneratorClass(), generated));