blob: fbde3527f9b1e28c20dd82eb2bf5ee3f012cc205 [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;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053020import java.util.HashSet;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053021import java.util.Iterator;
22import java.util.List;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053023import java.util.Set;
Bharat saraswal33dfa012016-05-17 19:59:16 +053024
Vinod Kumar S7a004de2016-02-05 16:15:09 +053025import org.apache.maven.plugin.AbstractMojo;
26import org.apache.maven.plugin.MojoExecutionException;
27import org.apache.maven.plugin.MojoFailureException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053028import org.apache.maven.plugins.annotations.Component;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053029import org.apache.maven.plugins.annotations.Mojo;
Bharat saraswal870c56f2016-02-20 21:57:16 +053030import org.apache.maven.plugins.annotations.Parameter;
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;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053033import org.onosproject.yangutils.linker.YangLinker;
34import org.onosproject.yangutils.linker.exceptions.LinkerException;
35import org.onosproject.yangutils.linker.impl.YangLinkerManager;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053036import org.onosproject.yangutils.parser.YangUtilsParser;
37import org.onosproject.yangutils.parser.exceptions.ParserException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053038import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
janani bde4ffab2016-04-15 16:18:30 +053039import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
40import org.onosproject.yangutils.translator.tojava.utils.YangToJavaNamingConflictUtil;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053041import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
Bharat saraswal870c56f2016-02-20 21:57:16 +053042import org.sonatype.plexus.build.incremental.BuildContext;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053043
Bharat saraswale2d51d62016-03-23 19:40:35 +053044import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;
45import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE;
Vinod Kumar S38046502016-03-23 15:30:27 +053046import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053047import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.translatorErrorHandler;
Bharat saraswal2f11f652016-03-25 18:19:46 +053048import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswale2d51d62016-03-23 19:40:35 +053049import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
50import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
51import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Vinod Kumar S38046502016-03-23 15:30:27 +053052import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
Vinod Kumar S38046502016-03-23 15:30:27 +053053import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053054import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Vinod Kumar S38046502016-03-23 15:30:27 +053055import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
56
Vinod Kumar S7a004de2016-02-05 16:15:09 +053057/**
Bharat saraswald9822e92016-04-05 15:13:44 +053058 * Represents ONOS YANG utility maven plugin.
Bharat saraswalef2e6392016-04-19 19:50:32 +053059 * Goal of plugin is yang2java.
60 * Execution phase is generate-sources.
61 * requiresDependencyResolution at compile time.
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053062 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053063@Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE,
64 requiresProject = true)
Vinod Kumar S7a004de2016-02-05 16:15:09 +053065public class YangUtilManager extends AbstractMojo {
66
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053067 private YangNode rootNode;
68 // YANG file information set.
69 private Set<YangFileInfo> yangFileInfoSet = new HashSet<>();
70 private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
71 private YangLinker yangLinker = new YangLinkerManager();
Bharat saraswal33dfa012016-05-17 19:59:16 +053072 private YangFileInfo curYangFileInfo = new YangFileInfo();
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053073
74 private static final String DEFAULT_PKG = SLASH + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG);
75
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053076 /**
77 * Source directory for YANG files.
78 */
79 @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang")
80 private String yangFilesDir;
81
82 /**
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053083 * Source directory for generated files.
84 */
85 @Parameter(property = "genFilesDir", defaultValue = "src/main/java")
86 private String genFilesDir;
87
88 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053089 * Base directory for project.
90 */
91 @Parameter(property = "basedir", defaultValue = "${basedir}")
92 private String baseDir;
93
94 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053095 * Output directory.
96 */
97 @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes")
Vinod Kumar S38046502016-03-23 15:30:27 +053098 private String outputDirectory;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053099
100 /**
101 * Current maven project.
102 */
103 @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}")
104 private MavenProject project;
105
106 /**
janani bde4ffab2016-04-15 16:18:30 +0530107 * Replacement required for period special character in the identifier.
108 */
109 @Parameter(property = "replacementForPeriod")
110 private String replacementForPeriod;
111
112 /**
113 * Replacement required for underscore special character in the identifier.
114 */
115 @Parameter(property = "replacementForUnderscore")
116 private String replacementForUnderscore;
117
118 /**
119 * Replacement required for hyphen special character in the identifier.
120 */
121 @Parameter(property = "replacementForHyphen")
122 private String replacementForHyphen;
123
124 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530125 * Build context.
126 */
127 @Component
128 private BuildContext context;
129
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530130 @Override
131 public void execute() throws MojoExecutionException, MojoFailureException {
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530132
133 try {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530134
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530135 /*
Bharat saraswal870c56f2016-02-20 21:57:16 +0530136 * For deleting the generated code in previous build.
137 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530138 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
139 deleteDirectory(getDirectory(baseDir, outputDirectory));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530140
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530141 String searchDir = getDirectory(baseDir, yangFilesDir);
142 String codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530143
144 // Creates conflict resolver and set values to it.
janani bde4ffab2016-04-15 16:18:30 +0530145 YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil();
146 conflictResolver.setReplacementForPeriod(replacementForPeriod);
147 conflictResolver.setReplacementForHyphen(replacementForHyphen);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530148 conflictResolver.setReplacementForUnderscore(replacementForUnderscore);
janani bde4ffab2016-04-15 16:18:30 +0530149 YangPluginConfig yangPlugin = new YangPluginConfig();
150 yangPlugin.setCodeGenDir(codeGenDir);
151 yangPlugin.setConflictResolver(conflictResolver);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530152
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530153 /*
154 * Obtain the YANG files at a path mentioned in plugin and creates
155 * YANG file information set.
156 */
157 createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530158
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530159 // Check if there are any file to translate, if not return.
160 if (getYangFileInfoSet() == null || getYangFileInfoSet().isEmpty()) {
161 // No files to translate
162 return;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530163 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530164
Bharat saraswal33dfa012016-05-17 19:59:16 +0530165 // Carry out the parsing for all the YANG files.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530166 parseYangFileInfoSet();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530167
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530168 // Resolve dependencies using linker.
169 resolveDependenciesUsingLinker();
170
171 // Perform translation to JAVA.
172 translateToJava(getYangFileInfoSet(), yangPlugin);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530173
Bharat saraswal33dfa012016-05-17 19:59:16 +0530174 addToSource(getDirectory(baseDir, genFilesDir), project, context);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530175
176 copyYangFilesToTarget(getYangFileInfoSet(), getDirectory(baseDir, outputDirectory), project);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530177 } catch (Exception e) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530178 String fileName = "";
Bharat saraswal33dfa012016-05-17 19:59:16 +0530179 if (getCurYangFileInfo() != null) {
180 fileName = getCurYangFileInfo().getYangFileName();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530181 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530182 try {
183 translatorErrorHandler(getRootNode());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530184 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530185 } catch (IOException ex) {
186 throw new MojoExecutionException(
187 "Error handler failed to delete files for data model node.");
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530188 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530189 throw new MojoExecutionException(
190 "Exception occured due to " + e.getLocalizedMessage() + " in " + fileName
191 + " YANG file.");
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530192 }
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530193 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530194
195 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530196 * Links all the provided with the YANG file info set.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530197 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530198 * @throws MojoExecutionException a violation in mojo excecution
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530199 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530200 public void resolveDependenciesUsingLinker() throws MojoExecutionException {
201 for (YangFileInfo yangFileInfo : getYangFileInfoSet()) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530202 setCurYangFileInfo(yangFileInfo);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530203 try {
204 yangLinker.resolveDependencies(getYangFileInfoSet());
205 } catch (LinkerException e) {
206 throw new MojoExecutionException(e.getMessage());
207 }
208 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530209 }
210
211 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530212 * Parses all the provided YANG files and generates YANG data model tree.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530213 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530214 * @throws IOException a violation in IO
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530215 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530216 public void parseYangFileInfoSet() throws IOException {
217 for (YangFileInfo yangFileInfo : getYangFileInfoSet()) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530218 setCurYangFileInfo(yangFileInfo);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530219 try {
220 YangNode yangNode = yangUtilsParser.getDataModel(yangFileInfo.getYangFileName());
221 yangFileInfo.setRootNode(yangNode);
222 setRootNode(yangNode);
223 } catch (ParserException e) {
224 String logInfo = "Error in file: " + e.getFileName();
225 if (e.getLineNumber() != 0) {
226 logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: "
227 + e.getCharPositionInLine();
228
229 }
230 if (e.getMessage() != null) {
231 logInfo = logInfo + NEW_LINE + e.getMessage();
232 }
233 getLog().info(logInfo);
234 throw e;
235 }
236 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530237 }
238
239 /**
240 * Returns current root YANG node of data-model tree.
241 *
242 * @return current root YANG node of data-model tree
243 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530244 private YangNode getRootNode() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530245 return rootNode;
246 }
247
248 /**
249 * Sets current root YANG node of data-model tree.
250 *
251 * @param rootNode current root YANG node of data-model tree
252 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530253 private void setRootNode(YangNode rootNode) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530254 this.rootNode = rootNode;
255 }
256
Vidyashree Rama1db15562016-05-17 16:16:15 +0530257 /**
258 * Translates to java code corresponding to the YANG schema.
259 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530260 * @param yangFileInfoSet YANG file information
261 * @param yangPlugin YANG plugin config
Vidyashree Rama1db15562016-05-17 16:16:15 +0530262 * @throws IOException when fails to generate java code file the current
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530263 * node
Vidyashree Rama1db15562016-05-17 16:16:15 +0530264 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530265 public void translateToJava(Set<YangFileInfo> yangFileInfoSet, YangPluginConfig yangPlugin)
Vidyashree Rama1db15562016-05-17 16:16:15 +0530266 throws IOException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530267 Iterator<YangFileInfo> yangFileIterator = yangFileInfoSet.iterator();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530268 while (yangFileIterator.hasNext()) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530269 YangFileInfo yangFileInfo = yangFileIterator.next();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530270 setCurYangFileInfo(yangFileInfo);
271 generateJavaCode(yangFileInfo.getRootNode(), yangPlugin);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530272 }
273 }
274
275 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530276 * Creates a YANG file info set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530277 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530278 * @param yangFileList YANG files list
Vidyashree Rama1db15562016-05-17 16:16:15 +0530279 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530280 public void createYangFileInfoSet(List<String> yangFileList) {
281 for (String yangFile : yangFileList) {
282 YangFileInfo yangFileInfo = new YangFileInfo();
283 yangFileInfo.setYangFileName(yangFile);
284 getYangFileInfoSet().add(yangFileInfo);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530285 }
286 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530287
288 /**
289 * Returns the YANG file info set.
290 *
291 * @return the YANG file info set
292 */
293 public Set<YangFileInfo> getYangFileInfoSet() {
294 return yangFileInfoSet;
295 }
296
297 /**
298 * Sets the YANG file info set.
299 *
300 * @param yangFileInfoSet the YANG file info set
301 */
302 public void setYangFileInfoSet(Set<YangFileInfo> yangFileInfoSet) {
303 this.yangFileInfoSet = yangFileInfoSet;
304 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530305
306 /**
307 * Returns current YANG file's info.
308 *
309 * @return the yangFileInfo
310 */
311 public YangFileInfo getCurYangFileInfo() {
312 return curYangFileInfo;
313 }
314
315 /**
316 * Sets current YANG file's info.
317 *
318 * @param yangFileInfo the yangFileInfo to set
319 */
320 public void setCurYangFileInfo(YangFileInfo yangFileInfo) {
321 this.curYangFileInfo = yangFileInfo;
322 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530323}