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.yangtools.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.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
31 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
32 import org.opendaylight.yangtools.yang2sources.plugin.Util.ContextHolder;
33 import org.opendaylight.yangtools.yang2sources.plugin.Util.YangsInZipsResult;
34 import org.opendaylight.yangtools.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 String META_INF_YANG_STRING_JAR = "META-INF" + "/" + "yang";
44 static final File META_INF_YANG_DIR = new File(META_INF_YANG_STRING);
46 private final Log log;
47 private final File yangFilesRootDir;
48 private final List<CodeGeneratorArg> codeGenerators;
49 private final MavenProject project;
50 private final boolean inspectDependencies;
51 private YangProvider yangProvider;
54 YangToSourcesProcessor(Log log, File yangFilesRootDir,
55 List<CodeGeneratorArg> codeGenerators, MavenProject project,
56 boolean inspectDependencies, YangProvider yangProvider) {
57 this.log = Util.checkNotNull(log, "log");
58 this.yangFilesRootDir = Util.checkNotNull(yangFilesRootDir,
60 this.codeGenerators = Collections.unmodifiableList(Util.checkNotNull(
61 codeGenerators, "codeGenerators"));
62 this.project = Util.checkNotNull(project, "project");
63 this.inspectDependencies = inspectDependencies;
64 this.yangProvider = yangProvider;
67 YangToSourcesProcessor(Log log, File yangFilesRootDir,
68 List<CodeGeneratorArg> codeGenerators, MavenProject project,
69 boolean inspectDependencies) {
70 this(log, yangFilesRootDir, codeGenerators, project,
71 inspectDependencies, new YangProvider());
74 public void execute() throws MojoExecutionException, MojoFailureException {
75 ContextHolder context = processYang();
76 generateSources(context);
77 yangProvider.addYangsToMETA_INF(log, project, yangFilesRootDir);
80 private ContextHolder processYang() throws MojoExecutionException {
81 YangParserImpl parser = new YangParserImpl();
82 List<Closeable> closeables = new ArrayList<>();
83 log.info(Util.message("Inspecting %s", LOG_PREFIX, yangFilesRootDir));
85 List<InputStream> yangsInProject = Util
86 .listFilesAsStream(yangFilesRootDir);
87 List<InputStream> all = new ArrayList<>(yangsInProject);
88 closeables.addAll(yangsInProject);
89 Map<InputStream, Module> allYangModules;
90 Set<Module> projectYangModules;
92 if (inspectDependencies) {
93 YangsInZipsResult dependentYangResult = Util
94 .findYangFilesInDependenciesAsStream(log, project);
95 Closeable dependentYangResult1 = dependentYangResult;
96 closeables.add(dependentYangResult1);
97 all.addAll(dependentYangResult.yangStreams);
100 allYangModules = parser.parseYangModelsFromStreamsMapped(all);
102 projectYangModules = new HashSet<>();
103 for (InputStream inProject : yangsInProject) {
104 projectYangModules.add(allYangModules.get(inProject));
108 for (AutoCloseable closeable : closeables) {
113 Set<Module> parsedAllYangModules = new HashSet<>(
114 allYangModules.values());
115 SchemaContext resolveSchemaContext = parser
116 .resolveSchemaContext(parsedAllYangModules);
117 log.info(Util.message("%s files parsed from %s", LOG_PREFIX,
118 Util.YANG_SUFFIX.toUpperCase(), yangsInProject));
119 return new ContextHolder(resolveSchemaContext, projectYangModules);
121 // MojoExecutionException is thrown since execution cannot continue
122 } catch (Exception e) {
123 String message = Util.message("Unable to parse %s files from %s",
124 LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir);
125 log.error(message, e);
126 throw new MojoExecutionException(message, e);
130 static class YangProvider {
132 private static final String yangResourceDir = "target" + File.separator
135 void addYangsToMETA_INF(Log log, MavenProject project,
136 File yangFilesRootDir) throws MojoFailureException {
137 File targetYangDir = new File(project.getBasedir(), yangResourceDir);
140 FileUtils.copyDirectory(yangFilesRootDir, targetYangDir);
141 } catch (IOException e) {
142 String message = "Unable to copy yang files into resource folder";
143 log.warn(message, e);
144 throw new MojoFailureException(message, e);
147 setResource(targetYangDir, META_INF_YANG_STRING_JAR, project);
149 log.debug(Util.message(
150 "Yang files from: %s marked as resources: %s", LOG_PREFIX,
151 yangFilesRootDir, META_INF_YANG_STRING_JAR));
154 private static void setResource(File targetYangDir, String targetPath,
155 MavenProject project) {
156 Resource res = new Resource();
157 res.setDirectory(targetYangDir.getPath());
158 if (targetPath != null)
159 res.setTargetPath(targetPath);
160 project.addResource(res);
165 * Call generate on every generator from plugin configuration
167 private void generateSources(ContextHolder context)
168 throws MojoFailureException {
169 if (codeGenerators.size() == 0) {
170 log.warn(Util.message("No code generators provided", LOG_PREFIX));
174 Map<String, String> thrown = Maps.newHashMap();
175 for (CodeGeneratorArg codeGenerator : codeGenerators) {
177 generateSourcesWithOneGenerator(context, codeGenerator);
178 } catch (Exception e) {
179 // try other generators, exception will be thrown after
180 log.error(Util.message(
181 "Unable to generate sources with %s generator",
182 LOG_PREFIX, codeGenerator.getCodeGeneratorClass()), e);
183 thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
184 .getCanonicalName());
188 if (!thrown.isEmpty()) {
189 String message = Util
191 "One or more code generators failed, including failed list(generatorClass=exception) %s",
192 LOG_PREFIX, thrown.toString());
194 throw new MojoFailureException(message);
199 * Instantiate generator from class and call required method
201 private void generateSourcesWithOneGenerator(ContextHolder context,
202 CodeGeneratorArg codeGeneratorCfg) throws ClassNotFoundException,
203 InstantiationException, IllegalAccessException, IOException {
205 codeGeneratorCfg.check();
207 CodeGenerator g = Util.getInstance(
208 codeGeneratorCfg.getCodeGeneratorClass(), CodeGenerator.class);
209 log.info(Util.message("Code generator instantiated from %s",
210 LOG_PREFIX, codeGeneratorCfg.getCodeGeneratorClass()));
212 File outputDir = codeGeneratorCfg.getOutputBaseDir(project);
214 log.info(Util.message("Sources will be generated to %s", LOG_PREFIX,
216 log.debug(Util.message("Project root dir is %s", LOG_PREFIX,
217 project.getBasedir()));
218 log.debug(Util.message(
219 "Additional configuration picked up for : %s: %s", LOG_PREFIX,
220 codeGeneratorCfg.getCodeGeneratorClass(),
221 codeGeneratorCfg.getAdditionalConfiguration()));
223 if(outputDir != null) {
224 project.addCompileSourceRoot(outputDir.getAbsolutePath());
227 g.setMavenProject(project);
228 g.setAdditionalConfig(codeGeneratorCfg.getAdditionalConfiguration());
229 File resourceBaseDir = codeGeneratorCfg.getResourceBaseDir(project);
231 YangProvider.setResource(resourceBaseDir, null, project);
232 g.setResourceBaseDir(resourceBaseDir);
233 log.debug(Util.message(
234 "Folder: %s marked as resources for generator: %s", LOG_PREFIX,
235 resourceBaseDir, codeGeneratorCfg.getCodeGeneratorClass()));
237 Collection<File> generated = g.generateSources(context.getContext(),
238 outputDir, context.getYangModules());
240 log.info(Util.message("Sources generated by %s: %s", LOG_PREFIX,
241 codeGeneratorCfg.getCodeGeneratorClass(), generated));