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;
32 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
33 public final class YangToSourcesMojo extends AbstractMojo {
35 private static final String LOG_PREFIX = "yang-to-sources:";
37 @Parameter(required = true)
38 private CodeGeneratorArg[] codeGenerators;
40 @Parameter(required = true)
41 private String yangFilesRootDir;
43 private final YangModelParser parser;
46 YangToSourcesMojo(CodeGeneratorArg[] codeGeneratorArgs,
47 YangModelParser parser, String yangFilesRootDir) {
49 this.codeGenerators = codeGeneratorArgs;
50 this.yangFilesRootDir = yangFilesRootDir;
54 public YangToSourcesMojo() {
56 parser = new YangModelParserImpl();
60 public void execute() throws MojoExecutionException, MojoFailureException {
61 SchemaContext context = processYang();
62 generateSources(context);
66 * Generate {@link SchemaContext} with {@link YangModelParserImpl}
68 private SchemaContext processYang() throws MojoExecutionException {
70 String[] yangFiles = Util.listFilesAsArrayOfPaths(yangFilesRootDir);
72 if (yangFiles.length == 0)
74 Util.message("No %s file found in %s", LOG_PREFIX,
75 Util.YANG_SUFFIX, yangFilesRootDir));
76 // TODO only warning or throw exception ?
78 Set<Module> parsedYang = parser.parseYangModels(yangFiles);
79 SchemaContext resolveSchemaContext = parser
80 .resolveSchemaContext(parsedYang);
82 Util.message("%s files parsed from %s", LOG_PREFIX,
83 Util.YANG_SUFFIX, Arrays.toString(yangFiles)));
84 return resolveSchemaContext;
86 // MojoExecutionException is thrown since execution cannot continue
87 } catch (Exception e) {
88 String message = Util.message("Unable to parse %s files from %s",
89 LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir);
90 getLog().error(message, e);
91 throw new MojoExecutionException(message, e);
96 * Call generate on every generator from plugin configuration
98 private void generateSources(SchemaContext context)
99 throws MojoFailureException {
100 if (codeGenerators.length == 0) {
102 Util.message("No code generators provided", LOG_PREFIX));
106 Map<String, String> thrown = Maps.newHashMap();
108 for (CodeGeneratorArg codeGenerator : codeGenerators) {
111 generateSourcesWithOneGenerator(context, codeGenerator);
113 } catch (Exception e) {
114 // try other generators, exception will be thrown after
117 "Unable to generate sources with %s generator",
119 codeGenerator.getCodeGeneratorClass()), e);
120 thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
121 .getCanonicalName());
125 if (!thrown.isEmpty()) {
126 String message = Util
128 "One or more code generators failed, including failed list(generatorClass=exception) %s",
129 LOG_PREFIX, thrown.toString());
130 getLog().error(message);
131 throw new MojoFailureException(message);
136 * Instantiate generator from class and call required method
138 private void generateSourcesWithOneGenerator(SchemaContext context,
139 CodeGeneratorArg codeGenerator) throws ClassNotFoundException,
140 InstantiationException, IllegalAccessException {
142 codeGenerator.check();
144 CodeGenerator g = Util.getInstance(
145 codeGenerator.getCodeGeneratorClass(), CodeGenerator.class);
147 Util.message("Code generator instantiated from %s", LOG_PREFIX,
148 codeGenerator.getCodeGeneratorClass()));
150 Collection<File> generated = g.generateSources(context,
151 codeGenerator.getOutputBaseDir());
153 Util.message("Sources generated by %s: %s", LOG_PREFIX,
154 codeGenerator.getCodeGeneratorClass(), generated));