blob: 3eb1ea3d8be05be4b4bfc0cad28d40d809a27fc3 [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)
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053065public class YangUtilManager
66 extends AbstractMojo {
Vinod Kumar S7a004de2016-02-05 16:15:09 +053067
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053068 private YangNode rootNode;
69 // YANG file information set.
70 private Set<YangFileInfo> yangFileInfoSet = new HashSet<>();
71 private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
72 private YangLinker yangLinker = new YangLinkerManager();
Bharat saraswal33dfa012016-05-17 19:59:16 +053073 private YangFileInfo curYangFileInfo = new YangFileInfo();
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053074
75 private static final String DEFAULT_PKG = SLASH + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG);
76
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053077 /**
78 * Source directory for YANG files.
79 */
80 @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang")
81 private String yangFilesDir;
82
83 /**
Bharat saraswal8f2a6c52016-03-09 18:34:56 +053084 * Source directory for generated files.
85 */
86 @Parameter(property = "genFilesDir", defaultValue = "src/main/java")
87 private String genFilesDir;
88
89 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053090 * Base directory for project.
91 */
92 @Parameter(property = "basedir", defaultValue = "${basedir}")
93 private String baseDir;
94
95 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053096 * Output directory.
97 */
98 @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes")
Vinod Kumar S38046502016-03-23 15:30:27 +053099 private String outputDirectory;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530100
101 /**
102 * Current maven project.
103 */
104 @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}")
105 private MavenProject project;
106
107 /**
janani bde4ffab2016-04-15 16:18:30 +0530108 * Replacement required for period special character in the identifier.
109 */
110 @Parameter(property = "replacementForPeriod")
111 private String replacementForPeriod;
112
113 /**
114 * Replacement required for underscore special character in the identifier.
115 */
116 @Parameter(property = "replacementForUnderscore")
117 private String replacementForUnderscore;
118
119 /**
120 * Replacement required for hyphen special character in the identifier.
121 */
122 @Parameter(property = "replacementForHyphen")
123 private String replacementForHyphen;
124
125 /**
janani bdd1314f2016-05-19 17:39:50 +0530126 * Prefix which is required for adding with the identifier.
127 */
128 @Parameter(property = "prefixForIdentifier")
129 private String prefixForIdentifier;
130
131 /**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530132 * Build context.
133 */
134 @Component
135 private BuildContext context;
136
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530137 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530138 public void execute()
139 throws MojoExecutionException, MojoFailureException {
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530140
141 try {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530142
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530143 /*
Bharat saraswal870c56f2016-02-20 21:57:16 +0530144 * For deleting the generated code in previous build.
145 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530146 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
147 deleteDirectory(getDirectory(baseDir, outputDirectory));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530148
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530149 String searchDir = getDirectory(baseDir, yangFilesDir);
150 String codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530151
152 // Creates conflict resolver and set values to it.
janani bde4ffab2016-04-15 16:18:30 +0530153 YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil();
154 conflictResolver.setReplacementForPeriod(replacementForPeriod);
155 conflictResolver.setReplacementForHyphen(replacementForHyphen);
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530156 conflictResolver.setReplacementForUnderscore(replacementForUnderscore);
janani bdd1314f2016-05-19 17:39:50 +0530157 conflictResolver.setPrefixForIdentifier(prefixForIdentifier);
janani bde4ffab2016-04-15 16:18:30 +0530158 YangPluginConfig yangPlugin = new YangPluginConfig();
159 yangPlugin.setCodeGenDir(codeGenDir);
160 yangPlugin.setConflictResolver(conflictResolver);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530161
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530162 /*
163 * Obtain the YANG files at a path mentioned in plugin and creates
164 * YANG file information set.
165 */
166 createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
Bharat saraswal870c56f2016-02-20 21:57:16 +0530167
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530168 // Check if there are any file to translate, if not return.
169 if (getYangFileInfoSet() == null || getYangFileInfoSet().isEmpty()) {
170 // No files to translate
171 return;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530172 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530173
Bharat saraswal33dfa012016-05-17 19:59:16 +0530174 // Carry out the parsing for all the YANG files.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530175 parseYangFileInfoSet();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530176
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530177 // Resolve dependencies using linker.
178 resolveDependenciesUsingLinker();
179
180 // Perform translation to JAVA.
181 translateToJava(getYangFileInfoSet(), yangPlugin);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530182
Bharat saraswal33dfa012016-05-17 19:59:16 +0530183 addToSource(getDirectory(baseDir, genFilesDir), project, context);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530184
185 copyYangFilesToTarget(getYangFileInfoSet(), getDirectory(baseDir, outputDirectory), project);
janani bdd1314f2016-05-19 17:39:50 +0530186 } catch (IOException | ParserException e) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530187 String fileName = "";
Bharat saraswal33dfa012016-05-17 19:59:16 +0530188 if (getCurYangFileInfo() != null) {
189 fileName = getCurYangFileInfo().getYangFileName();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530190 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530191 try {
192 translatorErrorHandler(getRootNode());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530193 deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530194 } catch (IOException ex) {
195 throw new MojoExecutionException(
196 "Error handler failed to delete files for data model node.");
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530197 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530198 throw new MojoExecutionException(
199 "Exception occured due to " + e.getLocalizedMessage() + " in " + fileName
200 + " YANG file.");
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530201 }
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530202 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530203
204 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530205 * Links all the provided with the YANG file info set.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530206 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530207 * @throws MojoExecutionException a violation in mojo excecution
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530208 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530209 public void resolveDependenciesUsingLinker()
210 throws MojoExecutionException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530211 for (YangFileInfo yangFileInfo : getYangFileInfoSet()) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530212 setCurYangFileInfo(yangFileInfo);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530213 try {
214 yangLinker.resolveDependencies(getYangFileInfoSet());
215 } catch (LinkerException e) {
216 throw new MojoExecutionException(e.getMessage());
217 }
218 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530219 }
220
221 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530222 * Parses all the provided YANG files and generates YANG data model tree.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530223 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530224 * @throws IOException a violation in IO
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530225 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530226 public void parseYangFileInfoSet()
227 throws IOException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530228 for (YangFileInfo yangFileInfo : getYangFileInfoSet()) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530229 setCurYangFileInfo(yangFileInfo);
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530230 try {
231 YangNode yangNode = yangUtilsParser.getDataModel(yangFileInfo.getYangFileName());
232 yangFileInfo.setRootNode(yangNode);
233 setRootNode(yangNode);
234 } catch (ParserException e) {
235 String logInfo = "Error in file: " + e.getFileName();
236 if (e.getLineNumber() != 0) {
237 logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: "
238 + e.getCharPositionInLine();
239
240 }
241 if (e.getMessage() != null) {
242 logInfo = logInfo + NEW_LINE + e.getMessage();
243 }
244 getLog().info(logInfo);
245 throw e;
246 }
247 }
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530248 }
249
250 /**
251 * Returns current root YANG node of data-model tree.
252 *
253 * @return current root YANG node of data-model tree
254 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530255 private YangNode getRootNode() {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530256 return rootNode;
257 }
258
259 /**
260 * Sets current root YANG node of data-model tree.
261 *
262 * @param rootNode current root YANG node of data-model tree
263 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530264 private void setRootNode(YangNode rootNode) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530265 this.rootNode = rootNode;
266 }
267
Vidyashree Rama1db15562016-05-17 16:16:15 +0530268 /**
269 * Translates to java code corresponding to the YANG schema.
270 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530271 * @param yangFileInfoSet YANG file information
272 * @param yangPlugin YANG plugin config
Vidyashree Rama1db15562016-05-17 16:16:15 +0530273 * @throws IOException when fails to generate java code file the current
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530274 * node
Vidyashree Rama1db15562016-05-17 16:16:15 +0530275 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530276 public void translateToJava(Set<YangFileInfo> yangFileInfoSet, YangPluginConfig yangPlugin)
Vidyashree Rama1db15562016-05-17 16:16:15 +0530277 throws IOException {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530278 Iterator<YangFileInfo> yangFileIterator = yangFileInfoSet.iterator();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530279 while (yangFileIterator.hasNext()) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530280 YangFileInfo yangFileInfo = yangFileIterator.next();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530281 setCurYangFileInfo(yangFileInfo);
282 generateJavaCode(yangFileInfo.getRootNode(), yangPlugin);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530283 }
284 }
285
286 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530287 * Creates a YANG file info set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530288 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530289 * @param yangFileList YANG files list
Vidyashree Rama1db15562016-05-17 16:16:15 +0530290 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530291 public void createYangFileInfoSet(List<String> yangFileList) {
292 for (String yangFile : yangFileList) {
293 YangFileInfo yangFileInfo = new YangFileInfo();
294 yangFileInfo.setYangFileName(yangFile);
295 getYangFileInfoSet().add(yangFileInfo);
Vidyashree Rama1db15562016-05-17 16:16:15 +0530296 }
297 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530298
299 /**
300 * Returns the YANG file info set.
301 *
302 * @return the YANG file info set
303 */
304 public Set<YangFileInfo> getYangFileInfoSet() {
305 return yangFileInfoSet;
306 }
307
308 /**
309 * Sets the YANG file info set.
310 *
311 * @param yangFileInfoSet the YANG file info set
312 */
313 public void setYangFileInfoSet(Set<YangFileInfo> yangFileInfoSet) {
314 this.yangFileInfoSet = yangFileInfoSet;
315 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530316
317 /**
318 * Returns current YANG file's info.
319 *
320 * @return the yangFileInfo
321 */
322 public YangFileInfo getCurYangFileInfo() {
323 return curYangFileInfo;
324 }
325
326 /**
327 * Sets current YANG file's info.
328 *
329 * @param yangFileInfo the yangFileInfo to set
330 */
331 public void setCurYangFileInfo(YangFileInfo yangFileInfo) {
332 this.curYangFileInfo = yangFileInfo;
333 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530334}