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.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
31 import org.opendaylight.yangtools.yang2sources.plugin.Util.ContextHolder;
32 import org.opendaylight.yangtools.yang2sources.plugin.Util.YangsInZipsResult;
33 import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
35 import com.google.common.annotations.VisibleForTesting;
36 import com.google.common.collect.Maps;
38 class YangToSourcesProcessor {
39 static final String LOG_PREFIX = "yang-to-sources:";
40 static final String META_INF_YANG_STRING = "META-INF" + File.separator + "yang";
41 static final String META_INF_YANG_STRING_JAR = "META-INF" + "/" + "yang";
42 static final File META_INF_YANG_DIR = new File(META_INF_YANG_STRING);
44 private final Log log;
45 private final File yangFilesRootDir;
46 private final File[] excludedFiles;
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, File[] excludedFiles, List<CodeGeneratorArg> codeGenerators,
54 MavenProject project, boolean inspectDependencies, YangProvider yangProvider) {
55 this.log = Util.checkNotNull(log, "log");
56 this.yangFilesRootDir = Util.checkNotNull(yangFilesRootDir, "yangFilesRootDir");
57 this.excludedFiles = new File[excludedFiles.length];
59 for (File file : excludedFiles) {
60 this.excludedFiles[i++] = new File(file.getPath());
62 this.codeGenerators = Collections.unmodifiableList(Util.checkNotNull(codeGenerators, "codeGenerators"));
63 this.project = Util.checkNotNull(project, "project");
64 this.inspectDependencies = inspectDependencies;
65 this.yangProvider = yangProvider;
68 YangToSourcesProcessor(Log log, File yangFilesRootDir, File[] excludedFiles, List<CodeGeneratorArg> codeGenerators,
69 MavenProject project, boolean inspectDependencies) {
70 this(log, yangFilesRootDir, excludedFiles, codeGenerators, project, inspectDependencies, new YangProvider());
73 public void execute() throws MojoExecutionException, MojoFailureException {
74 ContextHolder context = processYang();
75 generateSources(context);
76 yangProvider.addYangsToMetaInf(log, project, yangFilesRootDir, excludedFiles);
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.listFilesAsStream(yangFilesRootDir, excludedFiles, log);
85 List<InputStream> all = new ArrayList<>(yangsInProject);
86 closeables.addAll(yangsInProject);
87 Map<InputStream, Module> allYangModules;
88 Set<Module> projectYangModules;
90 if (inspectDependencies) {
91 YangsInZipsResult dependentYangResult = Util.findYangFilesInDependenciesAsStream(log, project);
92 Closeable dependentYangResult1 = dependentYangResult;
93 closeables.add(dependentYangResult1);
94 all.addAll(dependentYangResult.getYangStreams());
97 allYangModules = parser.parseYangModelsFromStreamsMapped(all);
99 projectYangModules = new HashSet<>();
100 for (InputStream inProject : yangsInProject) {
101 projectYangModules.add(allYangModules.get(inProject));
105 for (AutoCloseable closeable : closeables) {
110 Set<Module> parsedAllYangModules = new HashSet<>(allYangModules.values());
111 SchemaContext resolveSchemaContext = parser.resolveSchemaContext(parsedAllYangModules);
112 log.info(Util.message("%s files parsed from %s", LOG_PREFIX, Util.YANG_SUFFIX.toUpperCase(), yangsInProject));
113 return new ContextHolder(resolveSchemaContext, projectYangModules);
115 // MojoExecutionException is thrown since execution cannot continue
116 } catch (Exception e) {
117 String message = Util.message("Unable to parse %s files from %s", LOG_PREFIX, Util.YANG_SUFFIX,
119 log.error(message, e);
120 throw new MojoExecutionException(message, e);
124 static class YangProvider {
126 private static final String YANG_RESOURCE_DIR = "target" + File.separator + "yang";
128 void addYangsToMetaInf(Log log, MavenProject project, File yangFilesRootDir, File[] excludedFiles)
129 throws MojoFailureException {
130 File targetYangDir = new File(project.getBasedir(), YANG_RESOURCE_DIR);
133 Collection<File> files = Util.listFiles(yangFilesRootDir, excludedFiles, null);
134 for (File file : files) {
135 org.apache.commons.io.FileUtils.copyFile(file, new File(targetYangDir, file.getName()));
137 } catch (IOException e) {
138 String message = "Unable to list yang files into resource folder";
139 log.warn(message, e);
140 throw new MojoFailureException(message, e);
143 setResource(targetYangDir, META_INF_YANG_STRING_JAR, project);
145 log.debug(Util.message("Yang files from: %s marked as resources: %s", LOG_PREFIX, yangFilesRootDir,
146 META_INF_YANG_STRING_JAR));
149 private static void setResource(File targetYangDir, String targetPath, MavenProject project) {
150 Resource res = new Resource();
151 res.setDirectory(targetYangDir.getPath());
152 if (targetPath != null) {
153 res.setTargetPath(targetPath);
155 project.addResource(res);
160 * Call generate on every generator from plugin configuration
162 private void generateSources(ContextHolder context) throws MojoFailureException {
163 if (codeGenerators.size() == 0) {
164 log.warn(Util.message("No code generators provided", LOG_PREFIX));
168 Map<String, String> thrown = Maps.newHashMap();
169 for (CodeGeneratorArg codeGenerator : codeGenerators) {
171 generateSourcesWithOneGenerator(context, codeGenerator);
172 } catch (Exception e) {
173 // try other generators, exception will be thrown after
175 Util.message("Unable to generate sources with %s generator", LOG_PREFIX,
176 codeGenerator.getCodeGeneratorClass()), e);
177 thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass().getCanonicalName());
181 if (!thrown.isEmpty()) {
182 String message = Util.message(
183 "One or more code generators failed, including failed list(generatorClass=exception) %s",
184 LOG_PREFIX, thrown.toString());
186 throw new MojoFailureException(message);
191 * Instantiate generator from class and call required method
193 private void generateSourcesWithOneGenerator(ContextHolder context, CodeGeneratorArg codeGeneratorCfg)
194 throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
196 codeGeneratorCfg.check();
198 CodeGenerator g = Util.getInstance(codeGeneratorCfg.getCodeGeneratorClass(), CodeGenerator.class);
199 log.info(Util.message("Code generator instantiated from %s", LOG_PREFIX,
200 codeGeneratorCfg.getCodeGeneratorClass()));
202 File outputDir = codeGeneratorCfg.getOutputBaseDir(project);
204 log.info(Util.message("Sources will be generated to %s", LOG_PREFIX, outputDir));
205 log.debug(Util.message("Project root dir is %s", LOG_PREFIX, project.getBasedir()));
206 log.debug(Util.message("Additional configuration picked up for : %s: %s", LOG_PREFIX,
207 codeGeneratorCfg.getCodeGeneratorClass(), codeGeneratorCfg.getAdditionalConfiguration()));
209 if (outputDir != null) {
210 project.addCompileSourceRoot(outputDir.getAbsolutePath());
213 g.setMavenProject(project);
214 g.setAdditionalConfig(codeGeneratorCfg.getAdditionalConfiguration());
215 File resourceBaseDir = codeGeneratorCfg.getResourceBaseDir(project);
217 YangProvider.setResource(resourceBaseDir, null, project);
218 g.setResourceBaseDir(resourceBaseDir);
219 log.debug(Util.message("Folder: %s marked as resources for generator: %s", LOG_PREFIX, resourceBaseDir,
220 codeGeneratorCfg.getCodeGeneratorClass()));
222 Collection<File> generated = g.generateSources(context.getContext(), outputDir, context.getYangModules());
224 log.info(Util.message("Sources generated by %s: %s", LOG_PREFIX, codeGeneratorCfg.getCodeGeneratorClass(),