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 | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 19 | import org.apache.maven.artifact.repository.ArtifactRepository; |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 20 | import org.apache.maven.plugin.AbstractMojo; |
| 21 | import org.apache.maven.plugin.MojoExecutionException; |
| 22 | import org.apache.maven.plugin.MojoFailureException; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 23 | import org.apache.maven.plugins.annotations.Component; |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 24 | import org.apache.maven.plugins.annotations.Mojo; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 25 | import org.apache.maven.plugins.annotations.Parameter; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 26 | import org.apache.maven.project.MavenProject; |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 27 | import org.apache.maven.rtinfo.RuntimeInformation; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangNode; |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
| 30 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.linker.YangLinker; |
| 32 | import org.onosproject.yangutils.linker.exceptions.LinkerException; |
| 33 | import org.onosproject.yangutils.linker.impl.YangLinkerManager; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.parser.YangUtilsParser; |
| 35 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.tool.CallablePlugin; |
| 38 | import org.onosproject.yangutils.tool.YangFileInfo; |
| 39 | import org.onosproject.yangutils.tool.YangToolManager; |
| 40 | import org.onosproject.yangutils.tool.exception.YangToolException; |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame] | 41 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
| 42 | import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 43 | import org.sonatype.plexus.build.incremental.BuildContext; |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 44 | |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 45 | import java.io.IOException; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 46 | import java.util.HashSet; |
| 47 | import java.util.LinkedList; |
| 48 | import java.util.List; |
| 49 | import java.util.Set; |
| 50 | |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 51 | import static java.util.Collections.sort; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 52 | import static org.apache.maven.plugins.annotations.LifecyclePhase.PROCESS_SOURCES; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 53 | import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE; |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.datamodel.ResolvableType.YANG_DERIVED_DATA_TYPE; |
| 55 | import static org.onosproject.yangutils.datamodel.ResolvableType.YANG_IDENTITYREF; |
| 56 | import static org.onosproject.yangutils.linker.impl.YangLinkerUtils.resolveGroupingInDefinationScope; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 57 | import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.addToCompilationRoot; |
| 58 | import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.copyYangFilesToTarget; |
| 59 | import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.resolveInterJarDependencies; |
| 60 | import static org.onosproject.yangutils.plugin.manager.YangPluginUtils.serializeDataModel; |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 61 | import static org.onosproject.yangutils.tool.YangToolManager.DEFAULT_JAR_RES_PATH; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 63 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 64 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 65 | import static org.onosproject.yangutils.utils.UtilConstants.IN; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 66 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 67 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.utils.UtilConstants.TEMP; |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 69 | import static org.onosproject.yangutils.utils.UtilConstants.VERSION_ERROR; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 70 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES; |
Bharat saraswal | 5dafddb | 2016-11-22 20:11:58 +0530 | [diff] [blame] | 71 | import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getYangFiles; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 72 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 73 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 74 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 75 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getVersionValue; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 77 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 78 | * Represents ONOS YANG utility maven plugin. |
Bharat saraswal | 2f1077d | 2016-04-19 19:50:32 +0530 | [diff] [blame] | 79 | * Goal of plugin is yang2java. |
| 80 | * Execution phase is generate-sources. |
| 81 | * requiresDependencyResolution at compile time. |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 82 | */ |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 83 | @Mojo(name = "yang2java", defaultPhase = PROCESS_SOURCES, |
| 84 | requiresDependencyResolution = COMPILE) |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 85 | public class YangUtilManager extends AbstractMojo implements CallablePlugin { |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 86 | |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 87 | private static final String DEFAULT_PKG = |
| 88 | getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG); |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 89 | private static final int SUPPORTED_VERSION = 339; |
| 90 | private final YangPluginConfig yangPlugin = new YangPluginConfig(); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 91 | private YangNode rootNode; |
| 92 | // YANG file information set. |
| 93 | private Set<YangFileInfo> yangFileInfoSet = new HashSet<>(); |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 94 | private final YangUtilsParser yangUtilsParser = new YangUtilsParserManager(); |
| 95 | private final YangLinker yangLinker = new YangLinkerManager(); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 96 | private YangFileInfo curYangFileInfo = new YangFileInfo(); |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 97 | private final Set<YangNode> yangNodeSet = new HashSet<>(); |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 98 | |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 99 | /** |
| 100 | * Source directory for YANG files. |
| 101 | */ |
| 102 | @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang") |
| 103 | private String yangFilesDir; |
| 104 | |
| 105 | /** |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 106 | * Source directory for generated files. |
| 107 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 108 | @Parameter(property = "classFileDir", defaultValue = "target/generated-sources") |
| 109 | private String classFileDir; |
| 110 | |
| 111 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 112 | * Base directory for project. |
| 113 | */ |
| 114 | @Parameter(property = "basedir", defaultValue = "${basedir}") |
| 115 | private String baseDir; |
| 116 | |
| 117 | /** |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 118 | * Output directory. |
| 119 | */ |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 120 | @Parameter(property = "project.build.outputDirectory", required = true, |
| 121 | defaultValue = "target/classes") |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 122 | private String outputDirectory; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Current maven project. |
| 126 | */ |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 127 | @Parameter(property = "project", required = true, readonly = true, |
| 128 | defaultValue = "${project}") |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 129 | private MavenProject project; |
| 130 | |
| 131 | /** |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 132 | * Replacement required for period special character in the identifier. |
| 133 | */ |
| 134 | @Parameter(property = "replacementForPeriod") |
| 135 | private String replacementForPeriod; |
| 136 | |
| 137 | /** |
| 138 | * Replacement required for underscore special character in the identifier. |
| 139 | */ |
| 140 | @Parameter(property = "replacementForUnderscore") |
| 141 | private String replacementForUnderscore; |
| 142 | |
| 143 | /** |
| 144 | * Replacement required for hyphen special character in the identifier. |
| 145 | */ |
| 146 | @Parameter(property = "replacementForHyphen") |
| 147 | private String replacementForHyphen; |
| 148 | |
| 149 | /** |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 150 | * Prefix which is required for adding with the identifier. |
| 151 | */ |
| 152 | @Parameter(property = "prefixForIdentifier") |
| 153 | private String prefixForIdentifier; |
| 154 | |
| 155 | /** |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 156 | * Build context. |
| 157 | */ |
| 158 | @Component |
| 159 | private BuildContext context; |
| 160 | |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 161 | /** |
| 162 | * Local maven repository. |
| 163 | */ |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 164 | @Parameter(readonly = true, defaultValue = "${localRepository}") |
| 165 | private ArtifactRepository localRepository; |
| 166 | |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 167 | /** |
| 168 | * Remote maven repositories. |
| 169 | */ |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 170 | @Parameter(readonly = true, defaultValue = "${project.remoteArtifactRepositories}") |
| 171 | private List<ArtifactRepository> remoteRepository; |
| 172 | |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 173 | /** |
| 174 | * Code generation is for nbi or sbi. |
| 175 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 176 | @Parameter(property = "generateJavaFileForSbi", defaultValue = "nbi") |
| 177 | private String generateJavaFileForSbi; |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 178 | |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 179 | /** |
| 180 | * The Runtime information for the current instance of Maven. |
| 181 | */ |
| 182 | @Component |
| 183 | private RuntimeInformation runtime; |
| 184 | |
| 185 | /** |
| 186 | * The name of the property in which to store the version of Maven. |
| 187 | */ |
| 188 | @Parameter(defaultValue = "maven.version") |
| 189 | private String versionProperty; |
| 190 | |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 191 | private String outputDir; |
| 192 | private String codeGenDir; |
| 193 | |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 194 | @Override |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 195 | public void execute() |
| 196 | throws MojoExecutionException, MojoFailureException { |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 197 | |
| 198 | try { |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 199 | validateMavenVersion(); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 200 | /* |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 201 | * For deleting the generated code in previous build. |
| 202 | */ |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 203 | outputDir = getDirectory(baseDir, outputDirectory); |
| 204 | deleteDirectory(outputDir + SLASH + TEMP); |
| 205 | deleteDirectory(outputDir + SLASH + YANG_RESOURCES); |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 206 | String searchDir = getDirectory(baseDir, yangFilesDir); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 207 | codeGenDir = getDirectory(baseDir, classFileDir) + SLASH; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 208 | |
| 209 | // Creates conflict resolver and set values to it. |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 210 | YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil(); |
| 211 | conflictResolver.setReplacementForPeriod(replacementForPeriod); |
| 212 | conflictResolver.setReplacementForHyphen(replacementForHyphen); |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 213 | conflictResolver.setReplacementForUnderscore(replacementForUnderscore); |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 214 | conflictResolver.setPrefixForIdentifier(prefixForIdentifier); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 215 | yangPlugin.setCodeGenDir(codeGenDir); |
| 216 | yangPlugin.setConflictResolver(conflictResolver); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 217 | |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 218 | yangPlugin.resourceGenDir(outputDir + DEFAULT_JAR_RES_PATH); |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 219 | yangPlugin.setCodeGenerateForSbi(generateJavaFileForSbi.toLowerCase()); |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 220 | |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 221 | /* |
| 222 | * Obtain the YANG files at a path mentioned in plugin and creates |
| 223 | * YANG file information set. |
| 224 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 225 | |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 226 | YangToolManager toolManager = new YangToolManager(); |
Bharat saraswal | 5dafddb | 2016-11-22 20:11:58 +0530 | [diff] [blame] | 227 | |
| 228 | yangFileInfoSet = toolManager.createYangFileInfoSet( |
| 229 | getYangFiles(searchDir)); |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 230 | List<YangNode> interJarResolvedNodes = |
| 231 | resolveInterJarDependencies(project, localRepository, |
| 232 | remoteRepository, outputDir); |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 233 | toolManager.compileYangFiles(yangFileInfoSet, |
| 234 | interJarResolvedNodes, yangPlugin, |
| 235 | this); |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 236 | } catch (YangToolException e) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 237 | String fileName = EMPTY_STRING; |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 238 | if (e.getCurYangFile() != null) { |
| 239 | fileName = e.getCurYangFile().getYangFileName(); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 240 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 241 | try { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 242 | deleteDirectory(codeGenDir + DEFAULT_PKG); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 243 | } catch (IOException ex) { |
Gaurav Agrawal | d39a921 | 2016-08-20 18:51:20 +0530 | [diff] [blame] | 244 | e.printStackTrace(); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 245 | throw new MojoExecutionException( |
| 246 | "Error handler failed to delete files for data model node."); |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 247 | } |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 248 | getLog().info(e.getCause()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 249 | throw new MojoExecutionException( |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 250 | "Exception occurred due to " + e.getLocalizedMessage() + |
| 251 | IN + fileName + " YANG file."); |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 252 | } catch (IOException e) { |
| 253 | throw new MojoExecutionException( |
| 254 | "Failed to process files"); |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 255 | } |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 256 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 257 | |
| 258 | /** |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 259 | * Validates current maven version of system. |
| 260 | * |
| 261 | * @throws MojoExecutionException when maven version is below 3.3.9 |
| 262 | */ |
| 263 | private void validateMavenVersion() throws MojoExecutionException { |
| 264 | String version = runtime.getMavenVersion(); |
| 265 | if (getVersionValue(version) < SUPPORTED_VERSION) { |
| 266 | throw new MojoExecutionException(VERSION_ERROR + version); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 271 | * Returns the YANG node set. |
| 272 | * |
| 273 | * @return YANG node set |
| 274 | */ |
Bharat saraswal | b41904b | 2016-10-20 19:22:36 +0530 | [diff] [blame] | 275 | public Set<YangNode> getYangNodeSet() { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 276 | return yangNodeSet; |
| 277 | } |
| 278 | |
| 279 | /** |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 280 | * TODO: Delete me and use the tool code for UT test cases |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 281 | * Links all the provided with the YANG file info set. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 282 | * |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 283 | * @throws MojoExecutionException a violation in mojo execution |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 284 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 285 | public void resolveDependenciesUsingLinker() |
| 286 | throws MojoExecutionException { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 287 | createYangNodeSet(); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 288 | try { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 289 | yangLinker.resolveDependencies(yangNodeSet); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 290 | } catch (LinkerException e) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 291 | printLog(e.getFileName(), e.getLineNumber(), e.getCharPositionInLine(), |
| 292 | e.getMessage(), e.getLocalizedMessage()); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 293 | throw new MojoExecutionException(e.getMessage()); |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 294 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 298 | * TODO: Delete me and use the tool code for UT test cases |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 299 | * Creates YANG nodes set. |
| 300 | */ |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 301 | protected void createYangNodeSet() { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 302 | for (YangFileInfo yangFileInfo : yangFileInfoSet) { |
| 303 | yangNodeSet.add(yangFileInfo.getRootNode()); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 308 | * TODO: Delete me and use the tool code for UT test cases |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 309 | * Parses all the provided YANG files and generates YANG data model tree. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 310 | * |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 311 | * @throws IOException a violation in IO |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 312 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 313 | public void parseYangFileInfoSet() |
| 314 | throws IOException { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 315 | for (YangFileInfo yangFileInfo : yangFileInfoSet) { |
| 316 | curYangFileInfo = yangFileInfo; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 317 | if (yangFileInfo.isForTranslator()) { |
| 318 | try { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 319 | YangNode yangNode = yangUtilsParser.getDataModel( |
| 320 | yangFileInfo.getYangFileName()); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 321 | yangFileInfo.setRootNode(yangNode); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 322 | rootNode = yangNode; |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 323 | resolveGroupingInDefinationScope((YangReferenceResolver) yangNode); |
| 324 | try { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 325 | ((YangReferenceResolver) yangNode) |
| 326 | .resolveSelfFileLinking(YANG_DERIVED_DATA_TYPE); |
| 327 | ((YangReferenceResolver) yangNode) |
| 328 | .resolveSelfFileLinking(YANG_IDENTITYREF); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 329 | } catch (DataModelException e) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 330 | printLog(e.getFileName(), e.getLineNumber(), e |
| 331 | .getCharPositionInLine(), e.getMessage(), e |
| 332 | .getLocalizedMessage()); |
VinodKumarS-Huawei | 423dc9a | 2016-08-17 22:08:42 +0530 | [diff] [blame] | 333 | } |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 334 | } catch (ParserException e) { |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 335 | printLog(e.getFileName(), e.getLineNumber(), e |
| 336 | .getCharPositionInLine(), e.getMessage(), e |
| 337 | .getLocalizedMessage()); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 338 | throw e; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 339 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 340 | } |
| 341 | } |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 345 | * TODO: Delete me and use the tool code for UT test cases |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 346 | * Translates to java code corresponding to the YANG schema. |
| 347 | * |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 348 | * @param yangPlugin YANG plugin config |
| 349 | * @throws IOException when fails to generate java code file the current node |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 350 | */ |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 351 | public void translateToJava(YangPluginConfig yangPlugin) |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 352 | throws IOException { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 353 | List<YangNode> yangNodeSortedList = new LinkedList<>(); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 354 | yangNodeSortedList.addAll(yangNodeSet); |
| 355 | sort(yangNodeSortedList); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 356 | for (YangNode node : yangNodeSortedList) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 357 | if (node.isToTranslate()) { |
| 358 | generateJavaCode(node, yangPlugin); |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 359 | } |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 364 | * Creates a YANG file info set. |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 365 | * |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 366 | * @param yangFileList YANG files list |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 367 | */ |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 368 | public void createYangFileInfoSet(List<String> yangFileList) { |
| 369 | for (String yangFile : yangFileList) { |
| 370 | YangFileInfo yangFileInfo = new YangFileInfo(); |
| 371 | yangFileInfo.setYangFileName(yangFile); |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 372 | yangFileInfoSet.add(yangFileInfo); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 373 | } |
| 374 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 375 | |
| 376 | /** |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 377 | * TODO: Delete me and use the tool code for UT test cases |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 378 | * Returns the YANG file info set. |
| 379 | * |
| 380 | * @return the YANG file info set |
| 381 | */ |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 382 | public Set<YangFileInfo> getYangFileInfoSet() { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 383 | return yangFileInfoSet; |
| 384 | } |
| 385 | |
| 386 | /** |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 387 | * TODO: Delete me and use the tool code for UT test cases |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 388 | * Sets the YANG file info set. |
| 389 | * |
| 390 | * @param yangFileInfoSet the YANG file info set |
| 391 | */ |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 392 | void setYangFileInfoSet(Set<YangFileInfo> yangFileInfoSet) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 393 | this.yangFileInfoSet = yangFileInfoSet; |
| 394 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 395 | |
| 396 | /** |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 397 | * Adds log info for exception. |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 398 | * |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 399 | * @param fileName file name |
| 400 | * @param line line number |
| 401 | * @param position character position |
| 402 | * @param msg error message |
| 403 | * @param localMsg local message |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 404 | */ |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 405 | private void printLog(String fileName, int line, int position, String |
| 406 | msg, String localMsg) { |
| 407 | String logInfo = "Error in file: " + fileName; |
| 408 | if (line != 0) { |
| 409 | logInfo = logInfo + " at line: " + line + " at position: " |
| 410 | + position; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 411 | |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 412 | } |
| 413 | if (msg != null) { |
| 414 | logInfo = logInfo + NEW_LINE + localMsg; |
| 415 | } |
| 416 | getLog().info(logInfo); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 417 | } |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 418 | |
VinodKumarS-Huawei | 618fdfa | 2016-11-14 11:50:48 +0530 | [diff] [blame] | 419 | @Override |
| 420 | public void addGeneratedCodeToBundle() { |
| 421 | addToCompilationRoot(codeGenDir, project, context); |
| 422 | } |
| 423 | |
| 424 | |
| 425 | @Override |
| 426 | public void addCompiledSchemaToBundle() throws IOException { |
| 427 | serializeDataModel(outputDir, project, true); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public void addYangFilesToBundle() throws IOException { |
| 432 | copyYangFilesToTarget(outputDir, project); |
| 433 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 434 | } |