29282988d6ef267b82f2e1383ca8eb1653d716be
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-to-sources-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.Arrays;
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 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
33 public final class YangToSourcesMojo extends AbstractMojo {
34
35     private static final String LOG_PREFIX = "yang-to-sources:";
36
37     @Parameter(required = true)
38     private CodeGeneratorArg[] codeGenerators;
39
40     @Parameter(required = true)
41     private String yangFilesRootDir;
42
43     private final YangModelParser parser;
44
45     @VisibleForTesting
46     YangToSourcesMojo(CodeGeneratorArg[] codeGeneratorArgs,
47             YangModelParser parser, String yangFilesRootDir) {
48         super();
49         this.codeGenerators = codeGeneratorArgs;
50         this.yangFilesRootDir = yangFilesRootDir;
51         this.parser = parser;
52     }
53
54     public YangToSourcesMojo() {
55         super();
56         parser = new YangModelParserImpl();
57     }
58
59     @Override
60     public void execute() throws MojoExecutionException, MojoFailureException {
61         SchemaContext context = processYang();
62         generateSources(context);
63     }
64
65     /**
66      * Generate {@link SchemaContext} with {@link YangModelParserImpl}
67      */
68     private SchemaContext processYang() throws MojoExecutionException {
69         try {
70             String[] yangFiles = Util.listFilesAsArrayOfPaths(yangFilesRootDir);
71
72             if (yangFiles.length == 0)
73                 getLog().warn(
74                         Util.message("No %s file found in %s", LOG_PREFIX,
75                                 Util.YANG_SUFFIX, yangFilesRootDir));
76             // TODO only warning or throw exception ?
77
78             Set<Module> parsedYang = parser.parseYangModels(yangFiles);
79             SchemaContext resolveSchemaContext = parser
80                     .resolveSchemaContext(parsedYang);
81             getLog().info(
82                     Util.message("%s files parsed from %s", LOG_PREFIX,
83                             Util.YANG_SUFFIX, Arrays.toString(yangFiles)));
84             return resolveSchemaContext;
85
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);
92         }
93     }
94
95     /**
96      * Call generate on every generator from plugin configuration
97      */
98     private void generateSources(SchemaContext context)
99             throws MojoFailureException {
100         if (codeGenerators.length == 0) {
101             getLog().warn(
102                     Util.message("No code generators provided", LOG_PREFIX));
103             return;
104         }
105
106         Map<String, String> thrown = Maps.newHashMap();
107
108         for (CodeGeneratorArg codeGenerator : codeGenerators) {
109             try {
110
111                 generateSourcesWithOneGenerator(context, codeGenerator);
112
113             } catch (Exception e) {
114                 // try other generators, exception will be thrown after
115                 getLog().error(
116                         Util.message(
117                                 "Unable to generate sources with %s generator",
118                                 LOG_PREFIX,
119                                 codeGenerator.getCodeGeneratorClass()), e);
120                 thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
121                         .getCanonicalName());
122             }
123         }
124
125         if (!thrown.isEmpty()) {
126             String message = Util
127                     .message(
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);
132         }
133     }
134
135     /**
136      * Instantiate generator from class and call required method
137      */
138     private void generateSourcesWithOneGenerator(SchemaContext context,
139             CodeGeneratorArg codeGenerator) throws ClassNotFoundException,
140             InstantiationException, IllegalAccessException {
141
142         codeGenerator.check();
143
144         CodeGenerator g = Util.getInstance(
145                 codeGenerator.getCodeGeneratorClass(), CodeGenerator.class);
146         getLog().info(
147                 Util.message("Code generator instantiated from %s", LOG_PREFIX,
148                         codeGenerator.getCodeGeneratorClass()));
149
150         Collection<File> generated = g.generateSources(context,
151                 codeGenerator.getOutputBaseDir());
152         getLog().info(
153                 Util.message("Sources generated by %s: %s", LOG_PREFIX,
154                         codeGenerator.getCodeGeneratorClass(), generated));
155     }
156
157 }