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