blob: bcbe5af2ef43e36ecafeac465b3d15e1deaba5cd [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;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053024import org.apache.maven.plugin.AbstractMojo;
25import org.apache.maven.plugin.MojoExecutionException;
26import org.apache.maven.plugin.MojoFailureException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053027import org.apache.maven.plugins.annotations.Component;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053028import org.apache.maven.plugins.annotations.Mojo;
Bharat saraswal870c56f2016-02-20 21:57:16 +053029import org.apache.maven.plugins.annotations.Parameter;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053030import org.apache.maven.project.MavenProject;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053031import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053032import org.onosproject.yangutils.linker.YangLinker;
33import org.onosproject.yangutils.linker.exceptions.LinkerException;
34import org.onosproject.yangutils.linker.impl.YangLinkerManager;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053035import org.onosproject.yangutils.parser.YangUtilsParser;
36import org.onosproject.yangutils.parser.exceptions.ParserException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053037import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053038import org.onosproject.yangutils.translator.exception.TranslatorException;
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();
72
73 private static final String DEFAULT_PKG = SLASH + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG);
74
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053075 /**
76 * Source directory for YANG files.
77 */
78 @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang")
79 private String yangFilesDir;
80
81 /**
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053082 * Source directory for generated files.
83 */
84 @Parameter(property = "genFilesDir", defaultValue = "src/main/java")
85 private String genFilesDir;
86
87 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053088 * Base directory for project.
89 */
90 @Parameter(property = "basedir", defaultValue = "${basedir}")
91 private String baseDir;
92
93 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053094 * Output directory.
95 */
96 @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes")
Vinod Kumar S38046502016-03-23 15:30:27 +053097 private String outputDirectory;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053098
99 /**
100 * Current maven project.
101 */
102 @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}")
103 private MavenProject project;
104
105 /**
janani bde4ffab2016-04-15 16:18:30 +0530106 * Replacement required for period special character in the identifier.
107 */
108 @Parameter(property = "replacementForPeriod")
109 private String replacementForPeriod;
110
111 /**
112 * Replacement required for underscore special character in the identifier.
113 */
114 @Parameter(property = "replacementForUnderscore")
115 private String replacementForUnderscore;
116
117 /**
118 * Replacement required for hyphen special character in the identifier.
119 */
120 @Parameter(property = "replacementForHyphen")
121 private String replacementForHyphen;
122
123 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530124 * Build context.
125 */
126 @Component
127 private BuildContext context;
128
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530129 @Override
130 public void execute() throws MojoExecutionException, MojoFailureException {
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530131
132 try {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530133
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530134 /*
Bharat saraswal870c56f2016-02-20 21:57:16 +0530135 * For deleting the generated code in previous build.
136 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530137 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
138 deleteDirectory(getDirectory(baseDir, outputDirectory));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530139
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530140 String searchDir = getDirectory(baseDir, yangFilesDir);
141 String codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530142
143 // Creates conflict resolver and set values to it.
janani bde4ffab2016-04-15 16:18:30 +0530144 YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil();
145 conflictResolver.setReplacementForPeriod(replacementForPeriod);
146 conflictResolver.setReplacementForHyphen(replacementForHyphen);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530147 conflictResolver.setReplacementForUnderscore(replacementForUnderscore);
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
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530152 /*
153 * Obtain the YANG files at a path mentioned in plugin and creates
154 * YANG file information set.
155 */
156 createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530157
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530158 // Check if there are any file to translate, if not return.
159 if (getYangFileInfoSet() == null || getYangFileInfoSet().isEmpty()) {
160 // No files to translate
161 return;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530162 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530163
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530164 //Carry out the parsing for all the YANG files.
165 parseYangFileInfoSet();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530166
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530167 // Resolve dependencies using linker.
168 resolveDependenciesUsingLinker();
169
170 // Perform translation to JAVA.
171 translateToJava(getYangFileInfoSet(), yangPlugin);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530172
Vinod Kumar S38046502016-03-23 15:30:27 +0530173 addToSource(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG, project, context);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530174
175 copyYangFilesToTarget(getYangFileInfoSet(), getDirectory(baseDir, outputDirectory), project);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530176 } catch (Exception e) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530177 String fileName = "";
178 if (e instanceof TranslatorException) {
179 fileName = ((TranslatorException) e).getFileName();
180 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530181 try {
182 translatorErrorHandler(getRootNode());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530183 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530184 } catch (IOException ex) {
185 throw new MojoExecutionException(
186 "Error handler failed to delete files for data model node.");
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530187 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530188 throw new MojoExecutionException(
189 "Exception occured due to " + e.getLocalizedMessage() + " in " + fileName
190 + " YANG file.");
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530191 }
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530192 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530193
194 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530195 * Links all the provided with the YANG file info set.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530196 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530197 * @throws MojoExecutionException a violation in mojo excecution
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530198 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530199 public void resolveDependenciesUsingLinker() throws MojoExecutionException {
200 for (YangFileInfo yangFileInfo : getYangFileInfoSet()) {
201 try {
202 yangLinker.resolveDependencies(getYangFileInfoSet());
203 } catch (LinkerException e) {
204 throw new MojoExecutionException(e.getMessage());
205 }
206 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530207 }
208
209 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530210 * Parses all the provided YANG files and generates YANG data model tree.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530211 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530212 * @throws IOException a violation in IO
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530213 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530214 public void parseYangFileInfoSet() throws IOException {
215 for (YangFileInfo yangFileInfo : getYangFileInfoSet()) {
216 try {
217 YangNode yangNode = yangUtilsParser.getDataModel(yangFileInfo.getYangFileName());
218 yangFileInfo.setRootNode(yangNode);
219 setRootNode(yangNode);
220 } catch (ParserException e) {
221 String logInfo = "Error in file: " + e.getFileName();
222 if (e.getLineNumber() != 0) {
223 logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: "
224 + e.getCharPositionInLine();
225
226 }
227 if (e.getMessage() != null) {
228 logInfo = logInfo + NEW_LINE + e.getMessage();
229 }
230 getLog().info(logInfo);
231 throw e;
232 }
233 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530234 }
235
236 /**
237 * Returns current root YANG node of data-model tree.
238 *
239 * @return current root YANG node of data-model tree
240 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530241 private YangNode getRootNode() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530242 return rootNode;
243 }
244
245 /**
246 * Sets current root YANG node of data-model tree.
247 *
248 * @param rootNode current root YANG node of data-model tree
249 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530250 private void setRootNode(YangNode rootNode) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530251 this.rootNode = rootNode;
252 }
253
Vidyashree Rama1db15562016-05-17 16:16:15 +0530254 /**
255 * Translates to java code corresponding to the YANG schema.
256 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530257 * @param yangFileInfoSet YANG file information
258 * @param yangPlugin YANG plugin config
Vidyashree Rama1db15562016-05-17 16:16:15 +0530259 * @throws IOException when fails to generate java code file the current
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530260 * node
Vidyashree Rama1db15562016-05-17 16:16:15 +0530261 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530262 public void translateToJava(Set<YangFileInfo> yangFileInfoSet, YangPluginConfig yangPlugin)
Vidyashree Rama1db15562016-05-17 16:16:15 +0530263 throws IOException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530264 Iterator<YangFileInfo> yangFileIterator = yangFileInfoSet.iterator();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530265 while (yangFileIterator.hasNext()) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530266 YangFileInfo yangFileInfo = yangFileIterator.next();
267 generateJavaCode(yangFileInfo.getRootNode(), yangPlugin, yangFileInfo.getYangFileName());
Vidyashree Rama1db15562016-05-17 16:16:15 +0530268 }
269 }
270
271 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530272 * Creates a YANG file info set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530273 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530274 * @param yangFileList YANG files list
Vidyashree Rama1db15562016-05-17 16:16:15 +0530275 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530276 public void createYangFileInfoSet(List<String> yangFileList) {
277 for (String yangFile : yangFileList) {
278 YangFileInfo yangFileInfo = new YangFileInfo();
279 yangFileInfo.setYangFileName(yangFile);
280 getYangFileInfoSet().add(yangFileInfo);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530281 }
282 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530283
284 /**
285 * Returns the YANG file info set.
286 *
287 * @return the YANG file info set
288 */
289 public Set<YangFileInfo> getYangFileInfoSet() {
290 return yangFileInfoSet;
291 }
292
293 /**
294 * Sets the YANG file info set.
295 *
296 * @param yangFileInfoSet the YANG file info set
297 */
298 public void setYangFileInfoSet(Set<YangFileInfo> yangFileInfoSet) {
299 this.yangFileInfoSet = yangFileInfoSet;
300 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530301}