Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 212fcdd | 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 | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
| 22 | |
Vinod Kumar S | 212fcdd | 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 | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 26 | import org.apache.maven.plugins.annotations.Component; |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 27 | import org.apache.maven.plugins.annotations.Mojo; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 28 | import org.apache.maven.plugins.annotations.Parameter; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 29 | import org.apache.maven.project.MavenProject; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangNode; |
| 31 | import org.onosproject.yangutils.parser.YangUtilsParser; |
| 32 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 34 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
janani b | 1c6acc4 | 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 | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.utils.io.impl.YangFileScanner; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 38 | import org.sonatype.plexus.build.incremental.BuildContext; |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 39 | |
Bharat saraswal | 84366c5 | 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 | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.translatorErrorHandler; |
Bharat saraswal | d532a4c | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | 84366c5 | 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 | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 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 | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 53 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 54 | * Represents ONOS YANG utility maven plugin. |
Bharat saraswal | 2f1077d | 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 | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 58 | */ |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 59 | @Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE, |
| 60 | requiresProject = true) |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 61 | public class YangUtilManager extends AbstractMojo { |
| 62 | |
Bharat saraswal | f790747 | 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 | 5600f0f | 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 | 022dae9 | 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 | f790747 | 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 | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 85 | private String outputDirectory; |
Bharat saraswal | f790747 | 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 | 1c6acc4 | 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 | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 112 | * Build context. |
| 113 | */ |
| 114 | @Component |
| 115 | private BuildContext context; |
| 116 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 117 | private static final String DEFAULT_PKG = SLASH |
| 118 | + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 119 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 120 | private YangUtilsParser yangUtilsParser = new YangUtilsParserManager(); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 121 | private YangNode rootNode; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 122 | |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 123 | @Override |
| 124 | public void execute() throws MojoExecutionException, MojoFailureException { |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 125 | |
| 126 | try { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 127 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 128 | /* |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 129 | * For deleting the generated code in previous build. |
| 130 | */ |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 131 | deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG); |
| 132 | deleteDirectory(getDirectory(baseDir, outputDirectory)); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 133 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 134 | String searchDir = getDirectory(baseDir, yangFilesDir); |
| 135 | String codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH; |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 136 | YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil(); |
| 137 | conflictResolver.setReplacementForPeriod(replacementForPeriod); |
| 138 | conflictResolver.setReplacementForHyphen(replacementForHyphen); |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 139 | conflictResolver.setReplacementForUnderscore(replacementForUnderscore); |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 140 | List<String> yangFiles = YangFileScanner.getYangFiles(searchDir); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 141 | YangPluginConfig yangPlugin = new YangPluginConfig(); |
| 142 | yangPlugin.setCodeGenDir(codeGenDir); |
| 143 | yangPlugin.setConflictResolver(conflictResolver); |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 144 | Iterator<String> yangFileIterator = yangFiles.iterator(); |
| 145 | while (yangFileIterator.hasNext()) { |
| 146 | String yangFile = yangFileIterator.next(); |
| 147 | try { |
| 148 | YangNode yangNode = yangUtilsParser.getDataModel(yangFile); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 149 | setRootNode(yangNode); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 150 | generateJavaCode(yangNode, yangPlugin, yangFile); |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 151 | } catch (ParserException e) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 152 | String logInfo = "Error in file: " + e.getFileName(); |
| 153 | if (e.getLineNumber() != 0) { |
| 154 | logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: " |
| 155 | + e.getCharPositionInLine(); |
| 156 | |
| 157 | } |
| 158 | if (e.getMessage() != null) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 159 | logInfo = logInfo + NEW_LINE + e.getMessage(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 160 | } |
| 161 | getLog().info(logInfo); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 162 | throw e; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 163 | } |
| 164 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 165 | |
Vinod Kumar S | 9f26ae5 | 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 | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 168 | } catch (Exception e) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 169 | String fileName = ""; |
| 170 | if (e instanceof TranslatorException) { |
| 171 | fileName = ((TranslatorException) e).getFileName(); |
| 172 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 173 | try { |
| 174 | translatorErrorHandler(getRootNode()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 175 | deleteDirectory(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 176 | } catch (IOException ex) { |
| 177 | throw new MojoExecutionException( |
| 178 | "Error handler failed to delete files for data model node."); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 179 | } |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame^] | 180 | throw new MojoExecutionException( |
| 181 | "Exception occured due to " + e.getLocalizedMessage() + " in " + fileName |
| 182 | + " YANG file."); |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 183 | } |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 184 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Set current project. |
| 188 | * |
| 189 | * @param curProject maven project |
| 190 | */ |
| 191 | public void setCurrentProject(MavenProject curProject) { |
| 192 | project = curProject; |
| 193 | |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Returns current project. |
| 198 | * |
| 199 | * @return current project |
| 200 | */ |
| 201 | public MavenProject getCurrentProject() { |
| 202 | return project; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Returns current root YANG node of data-model tree. |
| 207 | * |
| 208 | * @return current root YANG node of data-model tree |
| 209 | */ |
| 210 | public YangNode getRootNode() { |
| 211 | return rootNode; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Sets current root YANG node of data-model tree. |
| 216 | * |
| 217 | * @param rootNode current root YANG node of data-model tree |
| 218 | */ |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 219 | public void setRootNode(YangNode rootNode) { |
| 220 | this.rootNode = rootNode; |
| 221 | } |
| 222 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 223 | } |