blob: 3373c6573437a2c2642a023a9435d55fba02afb2 [file] [log] [blame]
Vinod Kumar S7a004de2016-02-05 16:15:09 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.plugin.manager;
18
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053019import java.util.Iterator;
20import java.util.List;
21
Vinod Kumar S7a004de2016-02-05 16:15:09 +053022import org.apache.maven.plugin.AbstractMojo;
23import org.apache.maven.plugin.MojoExecutionException;
24import org.apache.maven.plugin.MojoFailureException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053025import org.apache.maven.plugins.annotations.Component;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053026import org.apache.maven.plugins.annotations.Mojo;
Bharat saraswal870c56f2016-02-20 21:57:16 +053027import org.apache.maven.plugins.annotations.Parameter;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053028import org.apache.maven.project.MavenProject;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053029import org.onosproject.yangutils.datamodel.YangNode;
30import org.onosproject.yangutils.parser.YangUtilsParser;
31import org.onosproject.yangutils.parser.exceptions.ParserException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053032import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053033import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
Bharat saraswal870c56f2016-02-20 21:57:16 +053034import org.sonatype.plexus.build.incremental.BuildContext;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053035
Bharat saraswale2d51d62016-03-23 19:40:35 +053036import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;
37import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE;
Vinod Kumar S38046502016-03-23 15:30:27 +053038import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
Bharat saraswale2d51d62016-03-23 19:40:35 +053039import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
40import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
41import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Vinod Kumar S38046502016-03-23 15:30:27 +053042import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
43import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
Bharat saraswale2d51d62016-03-23 19:40:35 +053044import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPkgToPath;
Vinod Kumar S38046502016-03-23 15:30:27 +053045import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget;
46import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
47
Vinod Kumar S7a004de2016-02-05 16:15:09 +053048/**
Bharat saraswale2d51d62016-03-23 19:40:35 +053049 * ONOS YANG utility maven plugin.
50 * Goal of plugin is yang2java Execution phase in generate-sources requiresDependencyResolution at compile time.
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053051 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053052@Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE,
53 requiresProject = true)
Vinod Kumar S7a004de2016-02-05 16:15:09 +053054public class YangUtilManager extends AbstractMojo {
55
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053056 /**
57 * Source directory for YANG files.
58 */
59 @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang")
60 private String yangFilesDir;
61
62 /**
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053063 * Source directory for generated files.
64 */
65 @Parameter(property = "genFilesDir", defaultValue = "src/main/java")
66 private String genFilesDir;
67
68 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053069 * Base directory for project.
70 */
71 @Parameter(property = "basedir", defaultValue = "${basedir}")
72 private String baseDir;
73
74 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053075 * Output directory.
76 */
77 @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes")
Vinod Kumar S38046502016-03-23 15:30:27 +053078 private String outputDirectory;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053079
80 /**
81 * Current maven project.
82 */
83 @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}")
84 private MavenProject project;
85
86 /**
87 * Build context.
88 */
89 @Component
90 private BuildContext context;
91
Bharat saraswale2d51d62016-03-23 19:40:35 +053092 private static final String DEFAULT_PKG = SLASH + convertPkgToPath(DEFAULT_BASE_PKG);
Vinod Kumar S38046502016-03-23 15:30:27 +053093
Bharat saraswal870c56f2016-02-20 21:57:16 +053094 private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053095 private String searchDir;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053096 private String codeGenDir;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053097
98 /**
99 * Set current project.
100 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530101 * @param curProject maven project
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530102 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530103 public void setCurrentProject(final MavenProject curProject) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530104
Bharat saraswal870c56f2016-02-20 21:57:16 +0530105 project = curProject;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530106 }
107
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530108 @Override
109 public void execute() throws MojoExecutionException, MojoFailureException {
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530110
111 try {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530112
Bharat saraswal870c56f2016-02-20 21:57:16 +0530113 /**
114 * For deleting the generated code in previous build.
115 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530116 clean(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
117 clean(getDirectory(baseDir, outputDirectory));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530118
Vinod Kumar S38046502016-03-23 15:30:27 +0530119 searchDir = getDirectory(baseDir, yangFilesDir);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530120 codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530121
122 List<String> yangFiles = YangFileScanner.getYangFiles(searchDir);
123 Iterator<String> yangFileIterator = yangFiles.iterator();
124 while (yangFileIterator.hasNext()) {
125 String yangFile = yangFileIterator.next();
126 try {
127 YangNode yangNode = yangUtilsParser.getDataModel(yangFile);
Vinod Kumar S38046502016-03-23 15:30:27 +0530128 generateJavaCode(yangNode, codeGenDir);
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530129 } catch (ParserException e) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530130 String logInfo = "Error in file: " + e.getFileName();
131 if (e.getLineNumber() != 0) {
132 logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: "
133 + e.getCharPositionInLine();
134
135 }
136 if (e.getMessage() != null) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530137 logInfo = logInfo + NEW_LINE + e.getMessage();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530138 }
139 getLog().info(logInfo);
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530140 }
141 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530142
Vinod Kumar S38046502016-03-23 15:30:27 +0530143 addToSource(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG, project, context);
144 copyYangFilesToTarget(yangFiles, getDirectory(baseDir, outputDirectory), project);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530145 } catch (Exception e) {
146 getLog().info(e);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530147 throw new MojoExecutionException("Exception occured due to " + e.getLocalizedMessage());
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530148 }
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530149 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530150}