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; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 20 | import java.util.HashSet; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 21 | import java.util.List; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 22 | import java.util.Set; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 23 | import org.apache.maven.artifact.repository.ArtifactRepository; |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 24 | import org.apache.maven.plugin.AbstractMojo; |
| 25 | import org.apache.maven.plugin.MojoExecutionException; |
| 26 | import org.apache.maven.plugin.MojoFailureException; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 27 | import org.apache.maven.plugins.annotations.Component; |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 28 | import org.apache.maven.plugins.annotations.Mojo; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 29 | import org.apache.maven.plugins.annotations.Parameter; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 30 | import org.apache.maven.project.MavenProject; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.linker.YangLinker; |
| 33 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
| 34 | import org.onosproject.yangutils.linker.impl.YangLinkerManager; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.parser.YangUtilsParser; |
| 36 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.utils.io.impl.YangFileScanner; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 39 | import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; |
| 40 | import org.onosproject.yangutils.utils.io.impl.YangToJavaNamingConflictUtil; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 41 | import org.sonatype.plexus.build.incremental.BuildContext; |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 42 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 43 | import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES; |
| 44 | import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE; |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 45 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.updateFilePriority; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.addToCompilationRoot; |
| 47 | import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.copyYangFilesToTarget; |
| 48 | import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.resolveInterJarDependencies; |
| 49 | import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.serializeDataModel; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode; |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.translatorErrorHandler; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG; |
| 53 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 54 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 55 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 56 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 57 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage; |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 58 | |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 59 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 60 | * Represents ONOS YANG utility maven plugin. |
Bharat saraswal | ef2e639 | 2016-04-19 19:50:32 +0530 | [diff] [blame] | 61 | * Goal of plugin is yang2java. |
| 62 | * Execution phase is generate-sources. |
| 63 | * requiresDependencyResolution at compile time. |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 64 | */ |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 65 | @Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE, |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 66 | requiresProject = true) |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 67 | public class YangUtilManager |
| 68 | extends AbstractMojo { |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 69 | |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 70 | private static final String DEFAULT_PKG = SLASH + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 71 | private YangNode rootNode; |
| 72 | // YANG file information set. |
| 73 | private Set<YangFileInfo> yangFileInfoSet = new HashSet<>(); |
| 74 | private YangUtilsParser yangUtilsParser = new YangUtilsParserManager(); |
| 75 | private YangLinker yangLinker = new YangLinkerManager(); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 76 | private YangFileInfo curYangFileInfo = new YangFileInfo(); |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 77 | private Set<YangNode> yangNodeSet = new HashSet<>(); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 78 | /** |
| 79 | * Source directory for YANG files. |
| 80 | */ |
| 81 | @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang") |
| 82 | private String yangFilesDir; |
| 83 | |
| 84 | /** |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 85 | * Source directory for generated files. |
| 86 | */ |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 87 | @Parameter(property = "classFileDir", defaultValue = "target/generated-sources") |
| 88 | private String classFileDir; |
| 89 | |
| 90 | /** |
| 91 | * Source directory for manager's generated files. |
| 92 | */ |
| 93 | @Parameter(property = "managerFileDir", defaultValue = "src/main/java") |
| 94 | private String managerFileDir; |
Bharat saraswal | 8f2a6c5 | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 95 | |
| 96 | /** |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 97 | * Base directory for project. |
| 98 | */ |
| 99 | @Parameter(property = "basedir", defaultValue = "${basedir}") |
| 100 | private String baseDir; |
| 101 | |
| 102 | /** |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 103 | * Output directory. |
| 104 | */ |
| 105 | @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes") |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 106 | private String outputDirectory; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Current maven project. |
| 110 | */ |
| 111 | @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}") |
| 112 | private MavenProject project; |
| 113 | |
| 114 | /** |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 115 | * Replacement required for period special character in the identifier. |
| 116 | */ |
| 117 | @Parameter(property = "replacementForPeriod") |
| 118 | private String replacementForPeriod; |
| 119 | |
| 120 | /** |
| 121 | * Replacement required for underscore special character in the identifier. |
| 122 | */ |
| 123 | @Parameter(property = "replacementForUnderscore") |
| 124 | private String replacementForUnderscore; |
| 125 | |
| 126 | /** |
| 127 | * Replacement required for hyphen special character in the identifier. |
| 128 | */ |
| 129 | @Parameter(property = "replacementForHyphen") |
| 130 | private String replacementForHyphen; |
| 131 | |
| 132 | /** |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 133 | * Prefix which is required for adding with the identifier. |
| 134 | */ |
| 135 | @Parameter(property = "prefixForIdentifier") |
| 136 | private String prefixForIdentifier; |
| 137 | |
| 138 | /** |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 139 | * Build context. |
| 140 | */ |
| 141 | @Component |
| 142 | private BuildContext context; |
| 143 | |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 144 | /** |
| 145 | * Local maven repository. |
| 146 | */ |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 147 | @Parameter(readonly = true, defaultValue = "${localRepository}") |
| 148 | private ArtifactRepository localRepository; |
| 149 | |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 150 | /** |
| 151 | * Remote maven repositories. |
| 152 | */ |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 153 | @Parameter(readonly = true, defaultValue = "${project.remoteArtifactRepositories}") |
| 154 | private List<ArtifactRepository> remoteRepository; |
| 155 | |
Shankara-Huawei | b9999eb | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 156 | /** |
| 157 | * Code generation is for nbi or sbi. |
| 158 | */ |
| 159 | @Parameter(property = "generateJavaFileForsbi", defaultValue = "nbi") |
| 160 | private String generateJavaFileForsbi; |
| 161 | |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 162 | @Override |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 163 | public void execute() |
| 164 | throws MojoExecutionException, MojoFailureException { |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 165 | |
| 166 | try { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 167 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 168 | /* |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 169 | * For deleting the generated code in previous build. |
| 170 | */ |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 171 | deleteDirectory(getDirectory(baseDir, classFileDir) + DEFAULT_PKG); |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 172 | deleteDirectory(getDirectory(baseDir, outputDirectory)); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 173 | |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 174 | String searchDir = getDirectory(baseDir, yangFilesDir); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 175 | String codeGenDir = getDirectory(baseDir, classFileDir) + SLASH; |
| 176 | String managerCodeGenDir = getDirectory(baseDir, managerFileDir) + SLASH; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 177 | |
| 178 | // Creates conflict resolver and set values to it. |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 179 | YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil(); |
| 180 | conflictResolver.setReplacementForPeriod(replacementForPeriod); |
| 181 | conflictResolver.setReplacementForHyphen(replacementForHyphen); |
Vidyashree Rama | 7142d9c | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 182 | conflictResolver.setReplacementForUnderscore(replacementForUnderscore); |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 183 | conflictResolver.setPrefixForIdentifier(prefixForIdentifier); |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 184 | YangPluginConfig yangPlugin = new YangPluginConfig(); |
| 185 | yangPlugin.setCodeGenDir(codeGenDir); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 186 | yangPlugin.setManagerCodeGenDir(managerCodeGenDir); |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 187 | yangPlugin.setConflictResolver(conflictResolver); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 188 | |
Shankara-Huawei | b9999eb | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 189 | yangPlugin.setCodeGenerateForsbi(generateJavaFileForsbi.toLowerCase()); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 190 | /* |
| 191 | * Obtain the YANG files at a path mentioned in plugin and creates |
| 192 | * YANG file information set. |
| 193 | */ |
| 194 | createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir)); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 195 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 196 | // Check if there are any file to translate, if not return. |
| 197 | if (getYangFileInfoSet() == null || getYangFileInfoSet().isEmpty()) { |
| 198 | // No files to translate |
| 199 | return; |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 200 | } |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 201 | // Resolve inter jar dependency. |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 202 | resolveInterJarDependency(); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 203 | |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 204 | // Carry out the parsing for all the YANG files. |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 205 | parseYangFileInfoSet(); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 206 | |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 207 | // Resolve dependencies using linker. |
| 208 | resolveDependenciesUsingLinker(); |
| 209 | |
| 210 | // Perform translation to JAVA. |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 211 | translateToJava(yangPlugin); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 212 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 213 | // Serialize data model. |
| 214 | serializeDataModel(getDirectory(baseDir, outputDirectory), getYangFileInfoSet(), project, true); |
| 215 | |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 216 | addToCompilationRoot(codeGenDir, project, context); |
| 217 | addToCompilationRoot(managerCodeGenDir, project, context); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 218 | |
| 219 | copyYangFilesToTarget(getYangFileInfoSet(), getDirectory(baseDir, outputDirectory), project); |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 220 | } catch (IOException | ParserException e) { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 221 | String fileName = ""; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 222 | if (getCurYangFileInfo() != null) { |
| 223 | fileName = getCurYangFileInfo().getYangFileName(); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 224 | } |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 225 | try { |
| 226 | translatorErrorHandler(getRootNode()); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 227 | deleteDirectory(getDirectory(baseDir, classFileDir) + DEFAULT_PKG); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 228 | } catch (IOException ex) { |
| 229 | throw new MojoExecutionException( |
| 230 | "Error handler failed to delete files for data model node."); |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 231 | } |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 232 | throw new MojoExecutionException( |
| 233 | "Exception occured due to " + e.getLocalizedMessage() + " in " + fileName |
| 234 | + " YANG file."); |
Bharat saraswal | ec4ef7c | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 235 | } |
Vinod Kumar S | 7a004de | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 236 | } |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 237 | |
| 238 | /** |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 239 | * Returns the YANG node set. |
| 240 | * |
| 241 | * @return YANG node set |
| 242 | */ |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 243 | Set<YangNode> getYangNodeSet() { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 244 | return yangNodeSet; |
| 245 | } |
| 246 | |
| 247 | /** |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 248 | * Resolved inter-jar dependencies. |
| 249 | * |
| 250 | * @throws IOException when fails to do IO operations |
| 251 | */ |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 252 | private void resolveInterJarDependency() throws IOException { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 253 | try { |
| 254 | List<YangNode> interJarResolvedNodes = resolveInterJarDependencies(project, localRepository, |
| 255 | remoteRepository, getDirectory(baseDir, outputDirectory)); |
| 256 | for (YangNode node : interJarResolvedNodes) { |
| 257 | YangFileInfo dependentFileInfo = new YangFileInfo(); |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 258 | node.setToTranslate(false); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 259 | dependentFileInfo.setRootNode(node); |
| 260 | dependentFileInfo.setForTranslator(false); |
| 261 | dependentFileInfo.setYangFileName(node.getName()); |
| 262 | getYangFileInfoSet().add(dependentFileInfo); |
| 263 | } |
| 264 | } catch (IOException e) { |
| 265 | throw new IOException("failed to resolve in interjar scenario."); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 270 | * Links all the provided with the YANG file info set. |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 271 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 272 | * @throws MojoExecutionException a violation in mojo excecution |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 273 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 274 | public void resolveDependenciesUsingLinker() |
| 275 | throws MojoExecutionException { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 276 | createYangNodeSet(); |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 277 | try { |
| 278 | yangLinker.resolveDependencies(getYangNodeSet()); |
| 279 | } catch (LinkerException e) { |
| 280 | throw new MojoExecutionException(e.getMessage()); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 281 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 282 | |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 286 | * Creates YANG nodes set. |
| 287 | */ |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 288 | void createYangNodeSet() { |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 289 | for (YangFileInfo yangFileInfo : getYangFileInfoSet()) { |
| 290 | getYangNodeSet().add(yangFileInfo.getRootNode()); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 295 | * Parses all the provided YANG files and generates YANG data model tree. |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 296 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 297 | * @throws IOException a violation in IO |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 298 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 299 | public void parseYangFileInfoSet() |
| 300 | throws IOException { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 301 | for (YangFileInfo yangFileInfo : getYangFileInfoSet()) { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 302 | setCurYangFileInfo(yangFileInfo); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 303 | if (yangFileInfo.isForTranslator()) { |
| 304 | try { |
| 305 | YangNode yangNode = yangUtilsParser.getDataModel(yangFileInfo.getYangFileName()); |
| 306 | yangFileInfo.setRootNode(yangNode); |
| 307 | setRootNode(yangNode); |
| 308 | } catch (ParserException e) { |
| 309 | String logInfo = "Error in file: " + e.getFileName(); |
| 310 | if (e.getLineNumber() != 0) { |
| 311 | logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: " |
| 312 | + e.getCharPositionInLine(); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 313 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 314 | } |
| 315 | if (e.getMessage() != null) { |
| 316 | logInfo = logInfo + NEW_LINE + e.getMessage(); |
| 317 | } |
| 318 | getLog().info(logInfo); |
| 319 | throw e; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 320 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 321 | } |
| 322 | } |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Returns current root YANG node of data-model tree. |
| 327 | * |
| 328 | * @return current root YANG node of data-model tree |
| 329 | */ |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 330 | private YangNode getRootNode() { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 331 | return rootNode; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Sets current root YANG node of data-model tree. |
| 336 | * |
| 337 | * @param rootNode current root YANG node of data-model tree |
| 338 | */ |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 339 | private void setRootNode(YangNode rootNode) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 340 | this.rootNode = rootNode; |
| 341 | } |
| 342 | |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 343 | /** |
| 344 | * Translates to java code corresponding to the YANG schema. |
| 345 | * |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 346 | * @param yangPlugin YANG plugin config |
| 347 | * @throws IOException when fails to generate java code file the current node |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 348 | */ |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 349 | public void translateToJava(YangPluginConfig yangPlugin) |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 350 | throws IOException { |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 351 | updateFilePriority(getYangNodeSet()); |
| 352 | for (YangNode node : getYangNodeSet()) { |
| 353 | if (node.isToTranslate()) { |
| 354 | generateJavaCode(node, yangPlugin); |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 355 | } |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | /** |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 360 | * Creates a YANG file info set. |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 361 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 362 | * @param yangFileList YANG files list |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 363 | */ |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 364 | public void createYangFileInfoSet(List<String> yangFileList) { |
| 365 | for (String yangFile : yangFileList) { |
| 366 | YangFileInfo yangFileInfo = new YangFileInfo(); |
| 367 | yangFileInfo.setYangFileName(yangFile); |
| 368 | getYangFileInfoSet().add(yangFileInfo); |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 369 | } |
| 370 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 371 | |
| 372 | /** |
| 373 | * Returns the YANG file info set. |
| 374 | * |
| 375 | * @return the YANG file info set |
| 376 | */ |
| 377 | public Set<YangFileInfo> getYangFileInfoSet() { |
| 378 | return yangFileInfoSet; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Sets the YANG file info set. |
| 383 | * |
| 384 | * @param yangFileInfoSet the YANG file info set |
| 385 | */ |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 386 | void setYangFileInfoSet(Set<YangFileInfo> yangFileInfoSet) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 387 | this.yangFileInfoSet = yangFileInfoSet; |
| 388 | } |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 389 | |
| 390 | /** |
| 391 | * Returns current YANG file's info. |
| 392 | * |
| 393 | * @return the yangFileInfo |
| 394 | */ |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 395 | private YangFileInfo getCurYangFileInfo() { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 396 | return curYangFileInfo; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Sets current YANG file's info. |
| 401 | * |
| 402 | * @param yangFileInfo the yangFileInfo to set |
| 403 | */ |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame^] | 404 | private void setCurYangFileInfo(YangFileInfo yangFileInfo) { |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 405 | curYangFileInfo = yangFileInfo; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 406 | } |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 407 | } |