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; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.parser.YangUtilsParser; |
| 33 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
| 36 | import org.onosproject.yangutils.translator.tojava.utils.YangToJavaNamingConflictUtil; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.utils.io.impl.YangFileScanner; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 38 | import org.sonatype.plexus.build.incremental.BuildContext; |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 39 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 40 | import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES; |
| 41 | import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.translatorErrorHandler; |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG; |
| 46 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 47 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource; |
| 49 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean; |
| 50 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget; |
| 51 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory; |
| 52 | |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 53 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 54 | * Represents ONOS YANG utility maven plugin. |
Bharat saraswal | ef2e639 | 2016-04-19 19:50:32 +0530 | [diff] [blame^] | 55 | * Goal of plugin is yang2java. |
| 56 | * Execution phase is generate-sources. |
| 57 | * requiresDependencyResolution at compile time. |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 58 | */ |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 59 | @Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE, |
| 60 | requiresProject = true) |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 61 | public class YangUtilManager extends AbstractMojo { |
| 62 | |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 63 | /** |
| 64 | * Source directory for YANG files. |
| 65 | */ |
| 66 | @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang") |
| 67 | private String yangFilesDir; |
| 68 | |
| 69 | /** |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 70 | * Source directory for generated files. |
| 71 | */ |
| 72 | @Parameter(property = "genFilesDir", defaultValue = "src/main/java") |
| 73 | private String genFilesDir; |
| 74 | |
| 75 | /** |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 76 | * Base directory for project. |
| 77 | */ |
| 78 | @Parameter(property = "basedir", defaultValue = "${basedir}") |
| 79 | private String baseDir; |
| 80 | |
| 81 | /** |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 82 | * Output directory. |
| 83 | */ |
| 84 | @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes") |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 85 | private String outputDirectory; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Current maven project. |
| 89 | */ |
| 90 | @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}") |
| 91 | private MavenProject project; |
| 92 | |
| 93 | /** |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 94 | * Replacement required for period special character in the identifier. |
| 95 | */ |
| 96 | @Parameter(property = "replacementForPeriod") |
| 97 | private String replacementForPeriod; |
| 98 | |
| 99 | /** |
| 100 | * Replacement required for underscore special character in the identifier. |
| 101 | */ |
| 102 | @Parameter(property = "replacementForUnderscore") |
| 103 | private String replacementForUnderscore; |
| 104 | |
| 105 | /** |
| 106 | * Replacement required for hyphen special character in the identifier. |
| 107 | */ |
| 108 | @Parameter(property = "replacementForHyphen") |
| 109 | private String replacementForHyphen; |
| 110 | |
| 111 | /** |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 112 | * Build context. |
| 113 | */ |
| 114 | @Component |
| 115 | private BuildContext context; |
| 116 | |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 117 | private static final String DEFAULT_PKG = SLASH + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 118 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 119 | private YangUtilsParser yangUtilsParser = new YangUtilsParserManager(); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 120 | private String searchDir; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 121 | private String codeGenDir; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 122 | private YangNode rootNode; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 123 | |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 124 | @Override |
| 125 | public void execute() throws MojoExecutionException, MojoFailureException { |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 126 | |
| 127 | try { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 128 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 129 | /** |
| 130 | * For deleting the generated code in previous build. |
| 131 | */ |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 132 | clean(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG); |
| 133 | clean(getDirectory(baseDir, outputDirectory)); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 134 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 135 | searchDir = getDirectory(baseDir, yangFilesDir); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 136 | codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH; |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 137 | YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil(); |
| 138 | conflictResolver.setReplacementForPeriod(replacementForPeriod); |
| 139 | conflictResolver.setReplacementForHyphen(replacementForHyphen); |
| 140 | conflictResolver.setReplacementForUnderscore(replacementForHyphen); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 141 | List<String> yangFiles = YangFileScanner.getYangFiles(searchDir); |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 142 | YangPluginConfig yangPlugin = new YangPluginConfig(); |
| 143 | yangPlugin.setCodeGenDir(codeGenDir); |
| 144 | yangPlugin.setConflictResolver(conflictResolver); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 145 | Iterator<String> yangFileIterator = yangFiles.iterator(); |
| 146 | while (yangFileIterator.hasNext()) { |
| 147 | String yangFile = yangFileIterator.next(); |
| 148 | try { |
| 149 | YangNode yangNode = yangUtilsParser.getDataModel(yangFile); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 150 | setRootNode(yangNode); |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 151 | generateJavaCode(yangNode, yangPlugin); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 152 | } catch (ParserException e) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 153 | String logInfo = "Error in file: " + e.getFileName(); |
| 154 | if (e.getLineNumber() != 0) { |
| 155 | logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: " |
| 156 | + e.getCharPositionInLine(); |
| 157 | |
| 158 | } |
| 159 | if (e.getMessage() != null) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 160 | logInfo = logInfo + NEW_LINE + e.getMessage(); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 161 | } |
| 162 | getLog().info(logInfo); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 163 | } |
| 164 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 165 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 166 | addToSource(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG, project, context); |
| 167 | copyYangFilesToTarget(yangFiles, getDirectory(baseDir, outputDirectory), project); |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 168 | } catch (Exception e) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 169 | try { |
| 170 | translatorErrorHandler(getRootNode()); |
| 171 | clean(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG); |
| 172 | } catch (IOException | DataModelException ex) { |
| 173 | throw new MojoExecutionException("Error handler failed to delete files for data model node."); |
| 174 | } |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 175 | throw new MojoExecutionException("Exception occured due to " + e.getLocalizedMessage()); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 176 | } |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 177 | } |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * Set current project. |
| 181 | * |
| 182 | * @param curProject maven project |
| 183 | */ |
| 184 | public void setCurrentProject(MavenProject curProject) { |
| 185 | project = curProject; |
| 186 | |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns current project. |
| 191 | * |
| 192 | * @return current project |
| 193 | */ |
| 194 | public MavenProject getCurrentProject() { |
| 195 | return project; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Returns current root YANG node of data-model tree. |
| 200 | * |
| 201 | * @return current root YANG node of data-model tree |
| 202 | */ |
| 203 | public YangNode getRootNode() { |
| 204 | return rootNode; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Sets current root YANG node of data-model tree. |
| 209 | * |
| 210 | * @param rootNode current root YANG node of data-model tree |
| 211 | */ |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 212 | public void setRootNode(YangNode rootNode) { |
| 213 | this.rootNode = rootNode; |
| 214 | } |
| 215 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 216 | } |