Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yangutils.plugin.manager; |
| 18 | |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
| 22 | |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 23 | import org.apache.maven.plugin.AbstractMojo; |
| 24 | import org.apache.maven.plugin.MojoExecutionException; |
| 25 | import org.apache.maven.plugin.MojoFailureException; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 26 | import org.apache.maven.plugins.annotations.Component; |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 27 | import org.apache.maven.plugins.annotations.Mojo; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 28 | import org.apache.maven.plugins.annotations.Parameter; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 29 | import org.apache.maven.project.MavenProject; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangNode; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 31 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 32 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.YangUtilsParser; |
| 34 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
| 38 | import org.onosproject.yangutils.translator.tojava.utils.YangToJavaNamingConflictUtil; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 39 | import org.onosproject.yangutils.utils.io.impl.YangFileScanner; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 40 | import org.sonatype.plexus.build.incremental.BuildContext; |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 41 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 42 | import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES; |
| 43 | import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.translatorErrorHandler; |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG; |
| 48 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 49 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget; |
| 53 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 54 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.findBelongsToModuleNode; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 55 | |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 56 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 57 | * Represents ONOS YANG utility maven plugin. |
Bharat saraswal | ef2e639 | 2016-04-19 19:50:32 +0530 | [diff] [blame] | 58 | * Goal of plugin is yang2java. |
| 59 | * Execution phase is generate-sources. |
| 60 | * requiresDependencyResolution at compile time. |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 61 | */ |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 62 | @Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE, |
| 63 | requiresProject = true) |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 64 | public class YangUtilManager extends AbstractMojo { |
| 65 | |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 66 | /** |
| 67 | * Source directory for YANG files. |
| 68 | */ |
| 69 | @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang") |
| 70 | private String yangFilesDir; |
| 71 | |
| 72 | /** |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 73 | * Source directory for generated files. |
| 74 | */ |
| 75 | @Parameter(property = "genFilesDir", defaultValue = "src/main/java") |
| 76 | private String genFilesDir; |
| 77 | |
| 78 | /** |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 79 | * Base directory for project. |
| 80 | */ |
| 81 | @Parameter(property = "basedir", defaultValue = "${basedir}") |
| 82 | private String baseDir; |
| 83 | |
| 84 | /** |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 85 | * Output directory. |
| 86 | */ |
| 87 | @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes") |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 88 | private String outputDirectory; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Current maven project. |
| 92 | */ |
| 93 | @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}") |
| 94 | private MavenProject project; |
| 95 | |
| 96 | /** |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 97 | * 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 saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 115 | * Build context. |
| 116 | */ |
| 117 | @Component |
| 118 | private BuildContext context; |
| 119 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 120 | private static final String DEFAULT_PKG = SLASH |
| 121 | + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 122 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 123 | private YangUtilsParser yangUtilsParser = new YangUtilsParserManager(); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 124 | private YangNode rootNode; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 125 | |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 126 | @Override |
| 127 | public void execute() throws MojoExecutionException, MojoFailureException { |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 128 | |
| 129 | try { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 130 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 131 | /* |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 132 | * For deleting the generated code in previous build. |
| 133 | */ |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 134 | deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG); |
| 135 | deleteDirectory(getDirectory(baseDir, outputDirectory)); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 136 | |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 137 | String searchDir = getDirectory(baseDir, yangFilesDir); |
| 138 | String codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH; |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 139 | YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil(); |
| 140 | conflictResolver.setReplacementForPeriod(replacementForPeriod); |
| 141 | conflictResolver.setReplacementForHyphen(replacementForHyphen); |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 142 | conflictResolver.setReplacementForUnderscore(replacementForUnderscore); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 143 | List<YangFileInfo> yangFileInfo = YangFileScanner.getYangFiles(searchDir); |
| 144 | if (yangFileInfo == null || yangFileInfo.isEmpty()) { |
| 145 | // no files to translate |
| 146 | return; |
| 147 | } |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 148 | YangPluginConfig yangPlugin = new YangPluginConfig(); |
| 149 | yangPlugin.setCodeGenDir(codeGenDir); |
| 150 | yangPlugin.setConflictResolver(conflictResolver); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 151 | |
| 152 | Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator(); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 153 | while (yangFileIterator.hasNext()) { |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 154 | YangFileInfo yangFile = yangFileIterator.next(); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 155 | try { |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 156 | YangNode yangNode = yangUtilsParser.getDataModel(yangFile.getYangFileName()); |
| 157 | yangFile.setRootNode(yangNode); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 158 | setRootNode(yangNode); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 159 | } catch (ParserException e) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 160 | 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 saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 167 | logInfo = logInfo + NEW_LINE + e.getMessage(); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 168 | } |
| 169 | getLog().info(logInfo); |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 170 | throw e; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 171 | } |
| 172 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 173 | |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 174 | resolveLinkingForSubModule(yangFileInfo); |
| 175 | |
| 176 | translateToJava(yangFileInfo, yangPlugin); |
| 177 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 178 | addToSource(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG, project, context); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 179 | copyYangFilesToTarget(yangFileInfo, getDirectory(baseDir, outputDirectory), project); |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 180 | } catch (Exception e) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 181 | String fileName = ""; |
| 182 | if (e instanceof TranslatorException) { |
| 183 | fileName = ((TranslatorException) e).getFileName(); |
| 184 | } |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 185 | try { |
| 186 | translatorErrorHandler(getRootNode()); |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 187 | deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 188 | } catch (IOException ex) { |
| 189 | throw new MojoExecutionException( |
| 190 | "Error handler failed to delete files for data model node."); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 191 | } |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 192 | throw new MojoExecutionException( |
| 193 | "Exception occured due to " + e.getLocalizedMessage() + " in " + fileName |
| 194 | + " YANG file."); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 195 | } |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 196 | } |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 197 | |
| 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 saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 231 | public void setRootNode(YangNode rootNode) { |
| 232 | this.rootNode = rootNode; |
| 233 | } |
| 234 | |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 235 | /** |
| 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 S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 271 | } |