Fixed NPE in maven-yang-plugin
[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.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Map;
14 import java.util.Set;
15
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;
30
31 import com.google.common.annotations.VisibleForTesting;
32 import com.google.common.collect.Maps;
33
34 /**
35  * Generate sources from yang files using user provided set of
36  * {@link CodeGenerator}s. Steps of this process:
37  * <ol>
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>
41  * <ol>
42  * <li>Instantiate using default constructor</li>
43  * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File)}</li>
44  * </ol>
45  * </ol>
46  */
47 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
48 public final class YangToSourcesMojo extends AbstractMojo {
49
50     private static final String LOG_PREFIX = "yang-to-sources:";
51
52     /**
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
56      * on every instance.
57      */
58     @Parameter(required = true)
59     private CodeGeneratorArg[] codeGenerators;
60
61     /**
62      * Source directory that will be recursively searched for yang files (ending
63      * with .yang suffix).
64      */
65     @Parameter(required = true)
66     private String yangFilesRootDir;
67
68     @Parameter(property = "project", required = true, readonly = true)
69     protected MavenProject project;
70
71     private transient final YangModelParser parser;
72
73     @VisibleForTesting
74     YangToSourcesMojo(CodeGeneratorArg[] codeGeneratorArgs,
75             YangModelParser parser, String yangFilesRootDir) {
76         super();
77         this.codeGenerators = codeGeneratorArgs;
78         this.yangFilesRootDir = yangFilesRootDir;
79         this.parser = parser;
80     }
81
82     public YangToSourcesMojo() {
83         super();
84         parser = new YangModelParserImpl();
85     }
86
87     @Override
88     public void execute() throws MojoExecutionException, MojoFailureException {
89         SchemaContext context = processYang();
90         generateSources(context);
91     }
92
93     /**
94      * Generate {@link SchemaContext} with {@link YangModelParserImpl}
95      */
96     private SchemaContext processYang() throws MojoExecutionException {
97         try {
98             Collection<File> yangFiles = Util.listFiles(yangFilesRootDir);
99
100             if (yangFiles.isEmpty()) {
101                 getLog().warn(
102                         Util.message("No %s file found in %s", LOG_PREFIX,
103                                 Util.YANG_SUFFIX, yangFilesRootDir));
104                 return null;
105             }
106
107             Set<Module> parsedYang = parser
108                     .parseYangModels(new ArrayList<File>(yangFiles));
109             SchemaContext resolveSchemaContext = parser
110                     .resolveSchemaContext(parsedYang);
111             getLog().info(
112                     Util.message("%s files parsed from %s", LOG_PREFIX,
113                             Util.YANG_SUFFIX, yangFiles));
114             return resolveSchemaContext;
115
116             // MojoExecutionException is thrown since execution cannot continue
117         } catch (Exception e) {
118             String message = Util.message("Unable to parse %s files from %s",
119                     LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir);
120             getLog().error(message, e);
121             throw new MojoExecutionException(message, e);
122         }
123     }
124
125     /**
126      * Call generate on every generator from plugin configuration
127      */
128     private void generateSources(SchemaContext context)
129             throws MojoFailureException {
130         if (codeGenerators.length == 0) {
131             getLog().warn(
132                     Util.message("No code generators provided", LOG_PREFIX));
133             return;
134         }
135
136         Map<String, String> thrown = Maps.newHashMap();
137
138         for (CodeGeneratorArg codeGenerator : codeGenerators) {
139             try {
140
141                 generateSourcesWithOneGenerator(context, codeGenerator);
142
143             } catch (Exception e) {
144                 // try other generators, exception will be thrown after
145                 getLog().error(
146                         Util.message(
147                                 "Unable to generate sources with %s generator",
148                                 LOG_PREFIX,
149                                 codeGenerator.getCodeGeneratorClass()), e);
150                 thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
151                         .getCanonicalName());
152             }
153         }
154
155         if (!thrown.isEmpty()) {
156             String message = Util
157                     .message(
158                             "One or more code generators failed, including failed list(generatorClass=exception) %s",
159                             LOG_PREFIX, thrown.toString());
160             getLog().error(message);
161             throw new MojoFailureException(message);
162         }
163     }
164
165     /**
166      * Instantiate generator from class and call required method
167      */
168     private void generateSourcesWithOneGenerator(SchemaContext context,
169             CodeGeneratorArg codeGeneratorCfg) throws ClassNotFoundException,
170             InstantiationException, IllegalAccessException {
171
172         codeGeneratorCfg.check();
173
174         CodeGenerator g = Util.getInstance(
175                 codeGeneratorCfg.getCodeGeneratorClass(), CodeGenerator.class);
176         getLog().info(
177                 Util.message("Code generator instantiated from %s", LOG_PREFIX,
178                         codeGeneratorCfg.getCodeGeneratorClass()));
179
180         File outputDir = codeGeneratorCfg.getOutputBaseDir();
181         if (project != null && outputDir != null) {
182             project.addCompileSourceRoot(outputDir.getPath());
183         }
184         Collection<File> generated = g.generateSources(context, outputDir);
185         getLog().info(
186                 Util.message("Sources generated by %s: %s", LOG_PREFIX,
187                         codeGeneratorCfg.getCodeGeneratorClass(), generated));
188     }
189
190 }