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