blob: efbb9391311ff7db3fcb07b3fdefaeb57573920f [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.io.File;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053020import java.util.Iterator;
21import java.util.List;
22
Vinod Kumar S7a004de2016-02-05 16:15:09 +053023import org.apache.maven.plugin.AbstractMojo;
24import org.apache.maven.plugin.MojoExecutionException;
25import org.apache.maven.plugin.MojoFailureException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053026import org.apache.maven.plugins.annotations.Component;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053027import org.apache.maven.plugins.annotations.LifecyclePhase;
28import org.apache.maven.plugins.annotations.Mojo;
Bharat saraswal870c56f2016-02-20 21:57:16 +053029import org.apache.maven.plugins.annotations.Parameter;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053030import org.apache.maven.plugins.annotations.ResolutionScope;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053031import org.apache.maven.project.MavenProject;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053032import org.onosproject.yangutils.datamodel.YangNode;
33import org.onosproject.yangutils.parser.YangUtilsParser;
34import org.onosproject.yangutils.parser.exceptions.ParserException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053035import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
Bharat saraswal870c56f2016-02-20 21:57:16 +053036import org.onosproject.yangutils.utils.UtilConstants;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053037import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
Bharat saraswal870c56f2016-02-20 21:57:16 +053038import org.sonatype.plexus.build.incremental.BuildContext;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053039
Vinod Kumar S38046502016-03-23 15:30:27 +053040import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
41import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
42import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
43import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget;
44import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
45
Vinod Kumar S7a004de2016-02-05 16:15:09 +053046/**
Bharat saraswal870c56f2016-02-20 21:57:16 +053047 * ONOS YANG utility maven plugin. Goal of plugin is yang2java Execution phase
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053048 * in generate-sources requiresDependencyResolution at compile time.
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053049 */
Vinod Kumar S7a004de2016-02-05 16:15:09 +053050@Mojo(name = "yang2java", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053051 requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
Vinod Kumar S7a004de2016-02-05 16:15:09 +053052public class YangUtilManager extends AbstractMojo {
53
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053054 /**
55 * Source directory for YANG files.
56 */
57 @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang")
58 private String yangFilesDir;
59
60 /**
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053061 * Source directory for generated files.
62 */
63 @Parameter(property = "genFilesDir", defaultValue = "src/main/java")
64 private String genFilesDir;
65
66 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053067 * Base directory for project.
68 */
69 @Parameter(property = "basedir", defaultValue = "${basedir}")
70 private String baseDir;
71
72 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053073 * Output directory.
74 */
75 @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes")
Vinod Kumar S38046502016-03-23 15:30:27 +053076 private String outputDirectory;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053077
78 /**
79 * Current maven project.
80 */
81 @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}")
82 private MavenProject project;
83
84 /**
85 * Build context.
86 */
87 @Component
88 private BuildContext context;
89
Vinod Kumar S38046502016-03-23 15:30:27 +053090 private static final String DEFAULT_PKG = File.separator
91 + UtilConstants.DEFAULT_BASE_PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
92
Bharat saraswal870c56f2016-02-20 21:57:16 +053093 private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053094 private String searchDir;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053095 private String codeGenDir;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053096
97 /**
98 * Set current project.
99 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530100 * @param curProject maven project
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530101 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530102 public void setCurrentProject(final MavenProject curProject) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530103
Bharat saraswal870c56f2016-02-20 21:57:16 +0530104 project = curProject;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530105 }
106
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530107 @Override
108 public void execute() throws MojoExecutionException, MojoFailureException {
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530109
110 try {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530111
Bharat saraswal870c56f2016-02-20 21:57:16 +0530112 /**
113 * For deleting the generated code in previous build.
114 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530115 clean(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
116 clean(getDirectory(baseDir, outputDirectory));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530117
Vinod Kumar S38046502016-03-23 15:30:27 +0530118 searchDir = getDirectory(baseDir, yangFilesDir);
119 codeGenDir = getDirectory(baseDir, genFilesDir) + File.separator;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530120
121 List<String> yangFiles = YangFileScanner.getYangFiles(searchDir);
122 Iterator<String> yangFileIterator = yangFiles.iterator();
123 while (yangFileIterator.hasNext()) {
124 String yangFile = yangFileIterator.next();
125 try {
126 YangNode yangNode = yangUtilsParser.getDataModel(yangFile);
Vinod Kumar S38046502016-03-23 15:30:27 +0530127 generateJavaCode(yangNode, codeGenDir);
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530128 } catch (ParserException e) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530129 String logInfo = "Error in file: " + e.getFileName();
130 if (e.getLineNumber() != 0) {
131 logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: "
132 + e.getCharPositionInLine();
133
134 }
135 if (e.getMessage() != null) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530136 logInfo = logInfo + UtilConstants.NEW_LINE + e.getMessage();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530137 }
138 getLog().info(logInfo);
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530139 }
140 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530141
Vinod Kumar S38046502016-03-23 15:30:27 +0530142 addToSource(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG, project, context);
143 copyYangFilesToTarget(yangFiles, getDirectory(baseDir, outputDirectory), project);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530144 } catch (Exception e) {
145 getLog().info(e);
Vinod Kumar S38046502016-03-23 15:30:27 +0530146 //throw new MojoExecutionException("Exception occured due to " + e.getLocalizedMessage());
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530147 }
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530148 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530149}