Merge "Checkstyle enforcer"
[controller.git] / opendaylight / sal / yang-prototype / code-generator / maven-yang-plugin / src / main / java / org / opendaylight / controller / yang2sources / plugin / YangToSourcesProcessor.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.Closeable;
11 import java.io.File;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.apache.maven.model.Resource;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.plugin.logging.Log;
26 import org.apache.maven.project.MavenProject;
27 import org.codehaus.plexus.util.FileUtils;
28 import org.opendaylight.controller.yang.model.api.Module;
29 import org.opendaylight.controller.yang.model.api.SchemaContext;
30 import org.opendaylight.controller.yang.parser.impl.YangParserImpl;
31 import org.opendaylight.controller.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
32 import org.opendaylight.controller.yang2sources.plugin.Util.ContextHolder;
33 import org.opendaylight.controller.yang2sources.plugin.Util.YangsInZipsResult;
34 import org.opendaylight.controller.yang2sources.spi.CodeGenerator;
35
36 import com.google.common.annotations.VisibleForTesting;
37 import com.google.common.collect.Maps;
38
39 class YangToSourcesProcessor {
40     static final String LOG_PREFIX = "yang-to-sources:";
41     static final String META_INF_YANG_STRING = "META-INF" + File.separator
42             + "yang";
43     static final File META_INF_YANG_DIR = new File(META_INF_YANG_STRING);
44
45     private final Log log;
46     private final File yangFilesRootDir;
47     private final List<CodeGeneratorArg> codeGenerators;
48     private final MavenProject project;
49     private final boolean inspectDependencies;
50     private YangProvider yangProvider;
51
52     @VisibleForTesting
53     YangToSourcesProcessor(Log log, File yangFilesRootDir,
54             List<CodeGeneratorArg> codeGenerators, MavenProject project,
55             boolean inspectDependencies, YangProvider yangProvider) {
56         this.log = Util.checkNotNull(log, "log");
57         this.yangFilesRootDir = Util.checkNotNull(yangFilesRootDir,
58                 "yangFilesRootDir");
59         this.codeGenerators = Collections.unmodifiableList(Util.checkNotNull(
60                 codeGenerators, "codeGenerators"));
61         this.project = Util.checkNotNull(project, "project");
62         this.inspectDependencies = inspectDependencies;
63         this.yangProvider = yangProvider;
64     }
65
66     YangToSourcesProcessor(Log log, File yangFilesRootDir,
67             List<CodeGeneratorArg> codeGenerators, MavenProject project,
68             boolean inspectDependencies) {
69         this(log, yangFilesRootDir, codeGenerators, project,
70                 inspectDependencies, new YangProvider());
71     }
72
73     public void execute() throws MojoExecutionException, MojoFailureException {
74         ContextHolder context = processYang();
75         generateSources(context);
76         yangProvider.addYangsToMETA_INF(log, project, yangFilesRootDir);
77     }
78
79     private ContextHolder processYang() throws MojoExecutionException {
80         YangParserImpl parser = new YangParserImpl();
81         List<Closeable> closeables = new ArrayList<>();
82         log.info(Util.message("Inspecting %s", LOG_PREFIX, yangFilesRootDir));
83         try {
84             List<InputStream> yangsInProject = Util
85                     .listFilesAsStream(yangFilesRootDir);
86             List<InputStream> all = new ArrayList<>(yangsInProject);
87             closeables.addAll(yangsInProject);
88             Map<InputStream, Module> allYangModules;
89             Set<Module> projectYangModules;
90             try {
91                 if (inspectDependencies) {
92                     YangsInZipsResult dependentYangResult = Util
93                             .findYangFilesInDependenciesAsStream(log, project);
94                     Closeable dependentYangResult1 = dependentYangResult;
95                     closeables.add(dependentYangResult1);
96                     all.addAll(dependentYangResult.yangStreams);
97                 }
98
99                 allYangModules = parser.parseYangModelsFromStreamsMapped(all);
100
101                 projectYangModules = new HashSet<>();
102                 for (InputStream inProject : yangsInProject) {
103                     projectYangModules.add(allYangModules.get(inProject));
104                 }
105
106             } finally {
107                 for (AutoCloseable closeable : closeables) {
108                     closeable.close();
109                 }
110             }
111
112             Set<Module> parsedAllYangModules = new HashSet<>(
113                     allYangModules.values());
114             SchemaContext resolveSchemaContext = parser
115                     .resolveSchemaContext(parsedAllYangModules);
116             log.info(Util.message("%s files parsed from %s", LOG_PREFIX,
117                     Util.YANG_SUFFIX.toUpperCase(), yangsInProject));
118             return new ContextHolder(resolveSchemaContext, projectYangModules);
119
120             // MojoExecutionException is thrown since execution cannot continue
121         } catch (Exception e) {
122             String message = Util.message("Unable to parse %s files from %s",
123                     LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir);
124             log.error(message, e);
125             throw new MojoExecutionException(message, e);
126         }
127     }
128
129     static class YangProvider {
130
131         private static final String yangResourceDir = "target" + File.separator
132                 + "yang";
133
134         void addYangsToMETA_INF(Log log, MavenProject project,
135                 File yangFilesRootDir) throws MojoFailureException {
136             File targetYangDir = new File(project.getBasedir(), yangResourceDir);
137
138             try {
139                 FileUtils.copyDirectory(yangFilesRootDir, targetYangDir);
140             } catch (IOException e) {
141                 String message = "Unable to copy yang files into resource folder";
142                 log.warn(message, e);
143                 throw new MojoFailureException(message, e);
144             }
145
146             setResource(targetYangDir, META_INF_YANG_DIR.getPath(), project);
147
148             log.debug(Util.message(
149                     "Yang files from: %s marked as resources: %s", LOG_PREFIX,
150                     yangFilesRootDir, META_INF_YANG_DIR.getPath()));
151         }
152
153         private static void setResource(File targetYangDir, String targetPath,
154                 MavenProject project) {
155             Resource res = new Resource();
156             res.setDirectory(targetYangDir.getPath());
157             if (targetPath != null)
158                 res.setTargetPath(targetPath);
159             project.addResource(res);
160         }
161     }
162
163     /**
164      * Call generate on every generator from plugin configuration
165      */
166     private void generateSources(ContextHolder context)
167             throws MojoFailureException {
168         if (codeGenerators.size() == 0) {
169             log.warn(Util.message("No code generators provided", LOG_PREFIX));
170             return;
171         }
172
173         Map<String, String> thrown = Maps.newHashMap();
174         for (CodeGeneratorArg codeGenerator : codeGenerators) {
175             try {
176                 generateSourcesWithOneGenerator(context, codeGenerator);
177             } catch (Exception e) {
178                 // try other generators, exception will be thrown after
179                 log.error(Util.message(
180                         "Unable to generate sources with %s generator",
181                         LOG_PREFIX, codeGenerator.getCodeGeneratorClass()), e);
182                 thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
183                         .getCanonicalName());
184             }
185         }
186
187         if (!thrown.isEmpty()) {
188             String message = Util
189                     .message(
190                             "One or more code generators failed, including failed list(generatorClass=exception) %s",
191                             LOG_PREFIX, thrown.toString());
192             log.error(message);
193             throw new MojoFailureException(message);
194         }
195     }
196
197     /**
198      * Instantiate generator from class and call required method
199      */
200     private void generateSourcesWithOneGenerator(ContextHolder context,
201             CodeGeneratorArg codeGeneratorCfg) throws ClassNotFoundException,
202             InstantiationException, IllegalAccessException, IOException {
203
204         codeGeneratorCfg.check();
205
206         CodeGenerator g = Util.getInstance(
207                 codeGeneratorCfg.getCodeGeneratorClass(), CodeGenerator.class);
208         log.info(Util.message("Code generator instantiated from %s",
209                 LOG_PREFIX, codeGeneratorCfg.getCodeGeneratorClass()));
210
211         File outputDir = codeGeneratorCfg.getOutputBaseDir(project);
212
213         log.info(Util.message("Sources will be generated to %s", LOG_PREFIX,
214                 outputDir));
215         log.debug(Util.message("Project root dir is %s", LOG_PREFIX,
216                 project.getBasedir()));
217         log.debug(Util.message(
218                 "Additional configuration picked up for : %s: %s", LOG_PREFIX,
219                 codeGeneratorCfg.getCodeGeneratorClass(),
220                 codeGeneratorCfg.getAdditionalConfiguration()));
221
222         project.addCompileSourceRoot(outputDir.getAbsolutePath());
223         g.setLog(log);
224         g.setMavenProject(project);
225         g.setAdditionalConfig(codeGeneratorCfg.getAdditionalConfiguration());
226         File resourceBaseDir = codeGeneratorCfg.getResourceBaseDir(project);
227
228         YangProvider.setResource(resourceBaseDir, null, project);
229         g.setResourceBaseDir(resourceBaseDir);
230         log.debug(Util.message(
231                 "Folder: %s marked as resources for generator: %s", LOG_PREFIX,
232                 resourceBaseDir, codeGeneratorCfg.getCodeGeneratorClass()));
233
234         Collection<File> generated = g.generateSources(context.getContext(),
235                 outputDir, context.getYangModules());
236
237         log.info(Util.message("Sources generated by %s: %s", LOG_PREFIX,
238                 codeGeneratorCfg.getCodeGeneratorClass(), generated));
239     }
240
241 }