blob: f15d3b8f48a97eaa69e8f9104c5b2892f99511a5 [file] [log] [blame]
Vinod Kumar S7a004de2016-02-05 16:15:09 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S7a004de2016-02-05 16:15:09 +05303 *
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 saraswal6ef0b762016-04-05 12:45:45 +053019import java.io.IOException;
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.Mojo;
Bharat saraswal870c56f2016-02-20 21:57:16 +053028import org.apache.maven.plugins.annotations.Parameter;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053029import org.apache.maven.project.MavenProject;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053030import org.onosproject.yangutils.datamodel.YangNode;
31import org.onosproject.yangutils.parser.YangUtilsParser;
32import org.onosproject.yangutils.parser.exceptions.ParserException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053033import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053034import org.onosproject.yangutils.translator.exception.TranslatorException;
janani bde4ffab2016-04-15 16:18:30 +053035import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
36import org.onosproject.yangutils.translator.tojava.utils.YangToJavaNamingConflictUtil;
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
Bharat saraswale2d51d62016-03-23 19:40:35 +053040import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;
41import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE;
Vinod Kumar S38046502016-03-23 15:30:27 +053042import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053043import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.translatorErrorHandler;
Bharat saraswal2f11f652016-03-25 18:19:46 +053044import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswale2d51d62016-03-23 19:40:35 +053045import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
46import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
47import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Vinod Kumar S38046502016-03-23 15:30:27 +053048import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053049import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Vinod Kumar S38046502016-03-23 15:30:27 +053050import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget;
51import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
52
Vinod Kumar S7a004de2016-02-05 16:15:09 +053053/**
Bharat saraswald9822e92016-04-05 15:13:44 +053054 * Represents ONOS YANG utility maven plugin.
Bharat saraswalef2e6392016-04-19 19:50:32 +053055 * Goal of plugin is yang2java.
56 * Execution phase is generate-sources.
57 * requiresDependencyResolution at compile time.
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053058 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053059@Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE,
60 requiresProject = true)
Vinod Kumar S7a004de2016-02-05 16:15:09 +053061public class YangUtilManager extends AbstractMojo {
62
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053063 /**
64 * Source directory for YANG files.
65 */
66 @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang")
67 private String yangFilesDir;
68
69 /**
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053070 * Source directory for generated files.
71 */
72 @Parameter(property = "genFilesDir", defaultValue = "src/main/java")
73 private String genFilesDir;
74
75 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053076 * Base directory for project.
77 */
78 @Parameter(property = "basedir", defaultValue = "${basedir}")
79 private String baseDir;
80
81 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053082 * Output directory.
83 */
84 @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes")
Vinod Kumar S38046502016-03-23 15:30:27 +053085 private String outputDirectory;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053086
87 /**
88 * Current maven project.
89 */
90 @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}")
91 private MavenProject project;
92
93 /**
janani bde4ffab2016-04-15 16:18:30 +053094 * Replacement required for period special character in the identifier.
95 */
96 @Parameter(property = "replacementForPeriod")
97 private String replacementForPeriod;
98
99 /**
100 * Replacement required for underscore special character in the identifier.
101 */
102 @Parameter(property = "replacementForUnderscore")
103 private String replacementForUnderscore;
104
105 /**
106 * Replacement required for hyphen special character in the identifier.
107 */
108 @Parameter(property = "replacementForHyphen")
109 private String replacementForHyphen;
110
111 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530112 * Build context.
113 */
114 @Component
115 private BuildContext context;
116
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530117 private static final String DEFAULT_PKG = SLASH
118 + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG);
Vinod Kumar S38046502016-03-23 15:30:27 +0530119
Bharat saraswal870c56f2016-02-20 21:57:16 +0530120 private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530121 private YangNode rootNode;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530122
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530123 @Override
124 public void execute() throws MojoExecutionException, MojoFailureException {
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530125
126 try {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530127
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530128 /*
Bharat saraswal870c56f2016-02-20 21:57:16 +0530129 * For deleting the generated code in previous build.
130 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530131 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
132 deleteDirectory(getDirectory(baseDir, outputDirectory));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530133
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530134 String searchDir = getDirectory(baseDir, yangFilesDir);
135 String codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
janani bde4ffab2016-04-15 16:18:30 +0530136 YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil();
137 conflictResolver.setReplacementForPeriod(replacementForPeriod);
138 conflictResolver.setReplacementForHyphen(replacementForHyphen);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530139 conflictResolver.setReplacementForUnderscore(replacementForUnderscore);
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530140 List<String> yangFiles = YangFileScanner.getYangFiles(searchDir);
janani bde4ffab2016-04-15 16:18:30 +0530141 YangPluginConfig yangPlugin = new YangPluginConfig();
142 yangPlugin.setCodeGenDir(codeGenDir);
143 yangPlugin.setConflictResolver(conflictResolver);
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530144 Iterator<String> yangFileIterator = yangFiles.iterator();
145 while (yangFileIterator.hasNext()) {
146 String yangFile = yangFileIterator.next();
147 try {
148 YangNode yangNode = yangUtilsParser.getDataModel(yangFile);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530149 setRootNode(yangNode);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530150 generateJavaCode(yangNode, yangPlugin, yangFile);
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530151 } catch (ParserException e) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530152 String logInfo = "Error in file: " + e.getFileName();
153 if (e.getLineNumber() != 0) {
154 logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: "
155 + e.getCharPositionInLine();
156
157 }
158 if (e.getMessage() != null) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530159 logInfo = logInfo + NEW_LINE + e.getMessage();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530160 }
161 getLog().info(logInfo);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530162 throw e;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530163 }
164 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530165
Vinod Kumar S38046502016-03-23 15:30:27 +0530166 addToSource(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG, project, context);
167 copyYangFilesToTarget(yangFiles, getDirectory(baseDir, outputDirectory), project);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530168 } catch (Exception e) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530169 String fileName = "";
170 if (e instanceof TranslatorException) {
171 fileName = ((TranslatorException) e).getFileName();
172 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530173 try {
174 translatorErrorHandler(getRootNode());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530175 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530176 } catch (IOException ex) {
177 throw new MojoExecutionException(
178 "Error handler failed to delete files for data model node.");
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530179 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530180 throw new MojoExecutionException(
181 "Exception occured due to " + e.getLocalizedMessage() + " in " + fileName
182 + " YANG file.");
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530183 }
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530184 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530185
186 /**
187 * Set current project.
188 *
189 * @param curProject maven project
190 */
191 public void setCurrentProject(MavenProject curProject) {
192 project = curProject;
193
194 }
195
196 /**
197 * Returns current project.
198 *
199 * @return current project
200 */
201 public MavenProject getCurrentProject() {
202 return project;
203 }
204
205 /**
206 * Returns current root YANG node of data-model tree.
207 *
208 * @return current root YANG node of data-model tree
209 */
210 public YangNode getRootNode() {
211 return rootNode;
212 }
213
214 /**
215 * Sets current root YANG node of data-model tree.
216 *
217 * @param rootNode current root YANG node of data-model tree
218 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530219 public void setRootNode(YangNode rootNode) {
220 this.rootNode = rootNode;
221 }
222
Vinod Kumar S38046502016-03-23 15:30:27 +0530223}