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.opendaylight.controller.yang.model.api.Module;
23 import org.opendaylight.controller.yang.model.api.SchemaContext;
24 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
25 import org.opendaylight.controller.yang.model.parser.impl.YangModelParserImpl;
26 import org.opendaylight.controller.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
27 import org.opendaylight.controller.yang2sources.spi.CodeGenerator;
29 import com.google.common.annotations.VisibleForTesting;
30 import com.google.common.collect.Maps;
33 * Generate sources from yang files using user provided set of
34 * {@link CodeGenerator}s. Steps of this process:
36 * <li>List yang files from {@link #yangFilesRootDir}</li>
37 * <li>Process yang files using {@link YangModelParserImpl}</li>
38 * <li>For each {@link CodeGenerator} from {@link #codeGenerators}:</li>
40 * <li>Instantiate using default constructor</li>
41 * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File)}</li>
45 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
46 public final class YangToSourcesMojo extends AbstractMojo {
48 private static final String LOG_PREFIX = "yang-to-sources:";
51 * Classes implementing {@link CodeGenerator} interface. An instance will be
52 * created out of every class using default constructor. Method
53 * {@link CodeGenerator#generateSources(SchemaContext, File)} will be called
56 @Parameter(required = true)
57 private CodeGeneratorArg[] codeGenerators;
60 * Source directory that will be recursively searched for yang files (ending
63 @Parameter(required = true)
64 private String yangFilesRootDir;
66 private final YangModelParser parser;
69 YangToSourcesMojo(CodeGeneratorArg[] codeGeneratorArgs,
70 YangModelParser parser, String yangFilesRootDir) {
72 this.codeGenerators = codeGeneratorArgs;
73 this.yangFilesRootDir = yangFilesRootDir;
77 public YangToSourcesMojo() {
79 parser = new YangModelParserImpl();
83 public void execute() throws MojoExecutionException, MojoFailureException {
84 SchemaContext context = processYang();
85 generateSources(context);
89 * Generate {@link SchemaContext} with {@link YangModelParserImpl}
91 private SchemaContext processYang() throws MojoExecutionException {
93 Collection<File> yangFiles = Util.listFiles(yangFilesRootDir);
95 if (yangFiles.isEmpty()) {
97 Util.message("No %s file found in %s", LOG_PREFIX,
98 Util.YANG_SUFFIX, yangFilesRootDir));
102 Set<Module> parsedYang = parser.parseYangModels(new ArrayList<File>(yangFiles));
103 SchemaContext resolveSchemaContext = parser
104 .resolveSchemaContext(parsedYang);
106 Util.message("%s files parsed from %s", LOG_PREFIX,
107 Util.YANG_SUFFIX, yangFiles));
108 return resolveSchemaContext;
110 // MojoExecutionException is thrown since execution cannot continue
111 } catch (Exception e) {
112 String message = Util.message("Unable to parse %s files from %s",
113 LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir);
114 getLog().error(message, e);
115 throw new MojoExecutionException(message, e);
120 * Call generate on every generator from plugin configuration
122 private void generateSources(SchemaContext context)
123 throws MojoFailureException {
124 if (codeGenerators.length == 0) {
126 Util.message("No code generators provided", LOG_PREFIX));
130 Map<String, String> thrown = Maps.newHashMap();
132 for (CodeGeneratorArg codeGenerator : codeGenerators) {
135 generateSourcesWithOneGenerator(context, codeGenerator);
137 } catch (Exception e) {
138 // try other generators, exception will be thrown after
141 "Unable to generate sources with %s generator",
143 codeGenerator.getCodeGeneratorClass()), e);
144 thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
145 .getCanonicalName());
149 if (!thrown.isEmpty()) {
150 String message = Util
152 "One or more code generators failed, including failed list(generatorClass=exception) %s",
153 LOG_PREFIX, thrown.toString());
154 getLog().error(message);
155 throw new MojoFailureException(message);
160 * Instantiate generator from class and call required method
162 private void generateSourcesWithOneGenerator(SchemaContext context,
163 CodeGeneratorArg codeGenerator) throws ClassNotFoundException,
164 InstantiationException, IllegalAccessException {
166 codeGenerator.check();
168 CodeGenerator g = Util.getInstance(
169 codeGenerator.getCodeGeneratorClass(), CodeGenerator.class);
171 Util.message("Code generator instantiated from %s", LOG_PREFIX,
172 codeGenerator.getCodeGeneratorClass()));
174 Collection<File> generated = g.generateSources(context,
175 codeGenerator.getOutputBaseDir());
177 Util.message("Sources generated by %s: %s", LOG_PREFIX,
178 codeGenerator.getCodeGeneratorClass(), generated));