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;
10 import java.io.Closeable;
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;
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;
36 import com.google.common.annotations.VisibleForTesting;
37 import com.google.common.collect.Maps;
39 class YangToSourcesProcessor {
40 static final String LOG_PREFIX = "yang-to-sources:";
41 static final String META_INF_YANG_STRING = "META-INF" + File.separator
43 static final File META_INF_YANG_DIR = new File(META_INF_YANG_STRING);
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;
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,
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;
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());
73 public void execute() throws MojoExecutionException, MojoFailureException {
74 ContextHolder context = processYang();
75 generateSources(context);
76 yangProvider.addYangsToMETA_INF(log, project, yangFilesRootDir);
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));
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;
91 if (inspectDependencies) {
92 YangsInZipsResult dependentYangResult = Util
93 .findYangFilesInDependenciesAsStream(log, project);
94 Closeable dependentYangResult1 = dependentYangResult;
95 closeables.add(dependentYangResult1);
96 all.addAll(dependentYangResult.yangStreams);
99 allYangModules = parser.parseYangModelsFromStreamsMapped(all);
101 projectYangModules = new HashSet<>();
102 for (InputStream inProject : yangsInProject) {
103 projectYangModules.add(allYangModules.get(inProject));
107 for (AutoCloseable closeable : closeables) {
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);
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);
129 static class YangProvider {
131 private static final String yangResourceDir = "target" + File.separator
134 void addYangsToMETA_INF(Log log, MavenProject project,
135 File yangFilesRootDir) throws MojoFailureException {
136 File targetYangDir = new File(project.getBasedir(), yangResourceDir);
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);
146 setResource(targetYangDir, META_INF_YANG_DIR.getPath(), project);
148 log.debug(Util.message(
149 "Yang files from: %s marked as resources: %s", LOG_PREFIX,
150 yangFilesRootDir, META_INF_YANG_DIR.getPath()));
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);
164 * Call generate on every generator from plugin configuration
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));
173 Map<String, String> thrown = Maps.newHashMap();
174 for (CodeGeneratorArg codeGenerator : codeGenerators) {
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());
187 if (!thrown.isEmpty()) {
188 String message = Util
190 "One or more code generators failed, including failed list(generatorClass=exception) %s",
191 LOG_PREFIX, thrown.toString());
193 throw new MojoFailureException(message);
198 * Instantiate generator from class and call required method
200 private void generateSourcesWithOneGenerator(ContextHolder context,
201 CodeGeneratorArg codeGeneratorCfg) throws ClassNotFoundException,
202 InstantiationException, IllegalAccessException, IOException {
204 codeGeneratorCfg.check();
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()));
211 File outputDir = codeGeneratorCfg.getOutputBaseDir(project);
213 log.info(Util.message("Sources will be generated to %s", LOG_PREFIX,
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()));
222 project.addCompileSourceRoot(outputDir.getAbsolutePath());
224 g.setMavenProject(project);
225 g.setAdditionalConfig(codeGeneratorCfg.getAdditionalConfiguration());
226 File resourceBaseDir = codeGeneratorCfg.getResourceBaseDir(project);
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()));
234 Collection<File> generated = g.generateSources(context.getContext(),
235 outputDir, context.getYangModules());
237 log.info(Util.message("Sources generated by %s: %s", LOG_PREFIX,
238 codeGeneratorCfg.getCodeGeneratorClass(), generated));