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