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.Arrays;
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 String[] yangFiles = Util.listFilesAsArrayOfPaths(yangFilesRootDir);
95 if (yangFiles.length == 0)
97 Util.message("No %s file found in %s", LOG_PREFIX,
98 Util.YANG_SUFFIX, yangFilesRootDir));
99 // TODO only warning or throw exception ?
101 Set<Module> parsedYang = parser.parseYangModels(yangFiles);
102 SchemaContext resolveSchemaContext = parser
103 .resolveSchemaContext(parsedYang);
105 Util.message("%s files parsed from %s", LOG_PREFIX,
106 Util.YANG_SUFFIX, Arrays.toString(yangFiles)));
107 return resolveSchemaContext;
109 // MojoExecutionException is thrown since execution cannot continue
110 } catch (Exception e) {
111 String message = Util.message("Unable to parse %s files from %s",
112 LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir);
113 getLog().error(message, e);
114 throw new MojoExecutionException(message, e);
119 * Call generate on every generator from plugin configuration
121 private void generateSources(SchemaContext context)
122 throws MojoFailureException {
123 if (codeGenerators.length == 0) {
125 Util.message("No code generators provided", LOG_PREFIX));
129 Map<String, String> thrown = Maps.newHashMap();
131 for (CodeGeneratorArg codeGenerator : codeGenerators) {
134 generateSourcesWithOneGenerator(context, codeGenerator);
136 } catch (Exception e) {
137 // try other generators, exception will be thrown after
140 "Unable to generate sources with %s generator",
142 codeGenerator.getCodeGeneratorClass()), e);
143 thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
144 .getCanonicalName());
148 if (!thrown.isEmpty()) {
149 String message = Util
151 "One or more code generators failed, including failed list(generatorClass=exception) %s",
152 LOG_PREFIX, thrown.toString());
153 getLog().error(message);
154 throw new MojoFailureException(message);
159 * Instantiate generator from class and call required method
161 private void generateSourcesWithOneGenerator(SchemaContext context,
162 CodeGeneratorArg codeGenerator) throws ClassNotFoundException,
163 InstantiationException, IllegalAccessException {
165 codeGenerator.check();
167 CodeGenerator g = Util.getInstance(
168 codeGenerator.getCodeGeneratorClass(), CodeGenerator.class);
170 Util.message("Code generator instantiated from %s", LOG_PREFIX,
171 codeGenerator.getCodeGeneratorClass()));
173 Collection<File> generated = g.generateSources(context,
174 codeGenerator.getOutputBaseDir());
176 Util.message("Sources generated by %s: %s", LOG_PREFIX,
177 codeGenerator.getCodeGeneratorClass(), generated));