blob: f602b299efde1566efb49e20c68eec412fdfd8be [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;
Vidyashree Rama1db15562016-05-17 16:16:15 +053031import org.onosproject.yangutils.datamodel.YangSubModule;
32import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053033import 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 saraswalab4c6ba2016-05-17 14:19:38 +053036import org.onosproject.yangutils.translator.exception.TranslatorException;
janani bde4ffab2016-04-15 16:18:30 +053037import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
38import org.onosproject.yangutils.translator.tojava.utils.YangToJavaNamingConflictUtil;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053039import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
Bharat saraswal870c56f2016-02-20 21:57:16 +053040import org.sonatype.plexus.build.incremental.BuildContext;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053041
Bharat saraswale2d51d62016-03-23 19:40:35 +053042import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;
43import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE;
Vinod Kumar S38046502016-03-23 15:30:27 +053044import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053045import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.translatorErrorHandler;
Bharat saraswal2f11f652016-03-25 18:19:46 +053046import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswale2d51d62016-03-23 19:40:35 +053047import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
48import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
49import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Vinod Kumar S38046502016-03-23 15:30:27 +053050import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053051import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Vinod Kumar S38046502016-03-23 15:30:27 +053052import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget;
53import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
Vidyashree Rama1db15562016-05-17 16:16:15 +053054import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findBelongsToModuleNode;
Vinod Kumar S38046502016-03-23 15:30:27 +053055
Vinod Kumar S7a004de2016-02-05 16:15:09 +053056/**
Bharat saraswald9822e92016-04-05 15:13:44 +053057 * Represents ONOS YANG utility maven plugin.
Bharat saraswalef2e6392016-04-19 19:50:32 +053058 * Goal of plugin is yang2java.
59 * Execution phase is generate-sources.
60 * requiresDependencyResolution at compile time.
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053061 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053062@Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE,
63 requiresProject = true)
Vinod Kumar S7a004de2016-02-05 16:15:09 +053064public class YangUtilManager extends AbstractMojo {
65
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053066 /**
67 * Source directory for YANG files.
68 */
69 @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang")
70 private String yangFilesDir;
71
72 /**
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053073 * Source directory for generated files.
74 */
75 @Parameter(property = "genFilesDir", defaultValue = "src/main/java")
76 private String genFilesDir;
77
78 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053079 * Base directory for project.
80 */
81 @Parameter(property = "basedir", defaultValue = "${basedir}")
82 private String baseDir;
83
84 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053085 * Output directory.
86 */
87 @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes")
Vinod Kumar S38046502016-03-23 15:30:27 +053088 private String outputDirectory;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053089
90 /**
91 * Current maven project.
92 */
93 @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}")
94 private MavenProject project;
95
96 /**
janani bde4ffab2016-04-15 16:18:30 +053097 * Replacement required for period special character in the identifier.
98 */
99 @Parameter(property = "replacementForPeriod")
100 private String replacementForPeriod;
101
102 /**
103 * Replacement required for underscore special character in the identifier.
104 */
105 @Parameter(property = "replacementForUnderscore")
106 private String replacementForUnderscore;
107
108 /**
109 * Replacement required for hyphen special character in the identifier.
110 */
111 @Parameter(property = "replacementForHyphen")
112 private String replacementForHyphen;
113
114 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530115 * Build context.
116 */
117 @Component
118 private BuildContext context;
119
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530120 private static final String DEFAULT_PKG = SLASH
121 + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG);
Vinod Kumar S38046502016-03-23 15:30:27 +0530122
Bharat saraswal870c56f2016-02-20 21:57:16 +0530123 private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530124 private YangNode rootNode;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530125
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530126 @Override
127 public void execute() throws MojoExecutionException, MojoFailureException {
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530128
129 try {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530130
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530131 /*
Bharat saraswal870c56f2016-02-20 21:57:16 +0530132 * For deleting the generated code in previous build.
133 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530134 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
135 deleteDirectory(getDirectory(baseDir, outputDirectory));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530136
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530137 String searchDir = getDirectory(baseDir, yangFilesDir);
138 String codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
janani bde4ffab2016-04-15 16:18:30 +0530139 YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil();
140 conflictResolver.setReplacementForPeriod(replacementForPeriod);
141 conflictResolver.setReplacementForHyphen(replacementForHyphen);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530142 conflictResolver.setReplacementForUnderscore(replacementForUnderscore);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530143 List<YangFileInfo> yangFileInfo = YangFileScanner.getYangFiles(searchDir);
144 if (yangFileInfo == null || yangFileInfo.isEmpty()) {
145 // no files to translate
146 return;
147 }
janani bde4ffab2016-04-15 16:18:30 +0530148 YangPluginConfig yangPlugin = new YangPluginConfig();
149 yangPlugin.setCodeGenDir(codeGenDir);
150 yangPlugin.setConflictResolver(conflictResolver);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530151
152 Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530153 while (yangFileIterator.hasNext()) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530154 YangFileInfo yangFile = yangFileIterator.next();
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530155 try {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530156 YangNode yangNode = yangUtilsParser.getDataModel(yangFile.getYangFileName());
157 yangFile.setRootNode(yangNode);
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530158 setRootNode(yangNode);
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530159 } catch (ParserException e) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530160 String logInfo = "Error in file: " + e.getFileName();
161 if (e.getLineNumber() != 0) {
162 logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: "
163 + e.getCharPositionInLine();
164
165 }
166 if (e.getMessage() != null) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530167 logInfo = logInfo + NEW_LINE + e.getMessage();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530168 }
169 getLog().info(logInfo);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530170 throw e;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530171 }
172 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530173
Vidyashree Rama1db15562016-05-17 16:16:15 +0530174 resolveLinkingForSubModule(yangFileInfo);
175
176 translateToJava(yangFileInfo, yangPlugin);
177
Vinod Kumar S38046502016-03-23 15:30:27 +0530178 addToSource(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG, project, context);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530179 copyYangFilesToTarget(yangFileInfo, getDirectory(baseDir, outputDirectory), project);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530180 } catch (Exception e) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530181 String fileName = "";
182 if (e instanceof TranslatorException) {
183 fileName = ((TranslatorException) e).getFileName();
184 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530185 try {
186 translatorErrorHandler(getRootNode());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530187 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530188 } catch (IOException ex) {
189 throw new MojoExecutionException(
190 "Error handler failed to delete files for data model node.");
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530191 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530192 throw new MojoExecutionException(
193 "Exception occured due to " + e.getLocalizedMessage() + " in " + fileName
194 + " YANG file.");
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530195 }
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530196 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530197
198 /**
199 * Set current project.
200 *
201 * @param curProject maven project
202 */
203 public void setCurrentProject(MavenProject curProject) {
204 project = curProject;
205
206 }
207
208 /**
209 * Returns current project.
210 *
211 * @return current project
212 */
213 public MavenProject getCurrentProject() {
214 return project;
215 }
216
217 /**
218 * Returns current root YANG node of data-model tree.
219 *
220 * @return current root YANG node of data-model tree
221 */
222 public YangNode getRootNode() {
223 return rootNode;
224 }
225
226 /**
227 * Sets current root YANG node of data-model tree.
228 *
229 * @param rootNode current root YANG node of data-model tree
230 */
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530231 public void setRootNode(YangNode rootNode) {
232 this.rootNode = rootNode;
233 }
234
Vidyashree Rama1db15562016-05-17 16:16:15 +0530235 /**
236 * Translates to java code corresponding to the YANG schema.
237 *
238 * @param yangFileInfo YANG file information
239 * @param yangPlugin YANG plugin config
240 * @throws IOException when fails to generate java code file the current
241 * node
242 */
243 public static void translateToJava(List<YangFileInfo> yangFileInfo, YangPluginConfig yangPlugin)
244 throws IOException {
245 Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
246 while (yangFileIterator.hasNext()) {
247 YangFileInfo yangFile = yangFileIterator.next();
248 generateJavaCode(yangFile.getRootNode(), yangPlugin, yangFile.getYangFileName());
249 }
250 }
251
252 /**
253 * Resolves sub-module linking.
254 *
255 * @param yangFileInfo YANG file information
256 * @throws DataModelException when belongs-to module node is not found
257 */
258 public static void resolveLinkingForSubModule(List<YangFileInfo> yangFileInfo) throws DataModelException {
259 Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
260 while (yangFileIterator.hasNext()) {
261 YangFileInfo yangFile = yangFileIterator.next();
262 YangNode yangNode = yangFile.getRootNode();
263 if (yangNode instanceof YangSubModule) {
264 String belongsToModuleName = ((YangSubModule) yangNode).getBelongsTo()
265 .getBelongsToModuleName();
266 YangNode moduleNode = findBelongsToModuleNode(yangFileInfo, belongsToModuleName);
267 ((YangSubModule) yangNode).getBelongsTo().setModuleNode(moduleNode);
268 }
269 }
270 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530271}