Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 19 | import java.io.File; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
| 22 | |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 23 | import org.apache.maven.plugin.AbstractMojo; |
| 24 | import org.apache.maven.plugin.MojoExecutionException; |
| 25 | import org.apache.maven.plugin.MojoFailureException; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 26 | import org.apache.maven.plugins.annotations.Component; |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 27 | import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 28 | import org.apache.maven.plugins.annotations.Mojo; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 29 | import org.apache.maven.plugins.annotations.Parameter; |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 30 | import org.apache.maven.plugins.annotations.ResolutionScope; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 31 | import org.apache.maven.project.MavenProject; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.YangNode; |
| 33 | import org.onosproject.yangutils.parser.YangUtilsParser; |
| 34 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.utils.UtilConstants; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.utils.io.impl.YangFileScanner; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 38 | import org.sonatype.plexus.build.incremental.BuildContext; |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 39 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 40 | import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode; |
| 41 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource; |
| 42 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean; |
| 43 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget; |
| 44 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory; |
| 45 | |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 46 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 47 | * ONOS YANG utility maven plugin. Goal of plugin is yang2java Execution phase |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 48 | * in generate-sources requiresDependencyResolution at compile time. |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 49 | */ |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 50 | @Mojo(name = "yang2java", defaultPhase = LifecyclePhase.GENERATE_SOURCES, |
Vinod Kumar S | c26bf19 | 2016-02-23 22:36:57 +0530 | [diff] [blame] | 51 | requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true) |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 52 | public class YangUtilManager extends AbstractMojo { |
| 53 | |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 54 | /** |
| 55 | * Source directory for YANG files. |
| 56 | */ |
| 57 | @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang") |
| 58 | private String yangFilesDir; |
| 59 | |
| 60 | /** |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 61 | * Source directory for generated files. |
| 62 | */ |
| 63 | @Parameter(property = "genFilesDir", defaultValue = "src/main/java") |
| 64 | private String genFilesDir; |
| 65 | |
| 66 | /** |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 67 | * Base directory for project. |
| 68 | */ |
| 69 | @Parameter(property = "basedir", defaultValue = "${basedir}") |
| 70 | private String baseDir; |
| 71 | |
| 72 | /** |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 73 | * Output directory. |
| 74 | */ |
| 75 | @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes") |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 76 | private String outputDirectory; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Current maven project. |
| 80 | */ |
| 81 | @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}") |
| 82 | private MavenProject project; |
| 83 | |
| 84 | /** |
| 85 | * Build context. |
| 86 | */ |
| 87 | @Component |
| 88 | private BuildContext context; |
| 89 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 90 | private static final String DEFAULT_PKG = File.separator |
| 91 | + UtilConstants.DEFAULT_BASE_PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH); |
| 92 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 93 | private YangUtilsParser yangUtilsParser = new YangUtilsParserManager(); |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 94 | private String searchDir; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 95 | private String codeGenDir; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Set current project. |
| 99 | * |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 100 | * @param curProject maven project |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 101 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 102 | public void setCurrentProject(final MavenProject curProject) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 103 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 104 | project = curProject; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 105 | } |
| 106 | |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 107 | @Override |
| 108 | public void execute() throws MojoExecutionException, MojoFailureException { |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 109 | |
| 110 | try { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 111 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 112 | /** |
| 113 | * For deleting the generated code in previous build. |
| 114 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 115 | clean(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG); |
| 116 | clean(getDirectory(baseDir, outputDirectory)); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 117 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 118 | searchDir = getDirectory(baseDir, yangFilesDir); |
| 119 | codeGenDir = getDirectory(baseDir, genFilesDir) + File.separator; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 120 | |
| 121 | List<String> yangFiles = YangFileScanner.getYangFiles(searchDir); |
| 122 | Iterator<String> yangFileIterator = yangFiles.iterator(); |
| 123 | while (yangFileIterator.hasNext()) { |
| 124 | String yangFile = yangFileIterator.next(); |
| 125 | try { |
| 126 | YangNode yangNode = yangUtilsParser.getDataModel(yangFile); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 127 | generateJavaCode(yangNode, codeGenDir); |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 128 | } catch (ParserException e) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 129 | String logInfo = "Error in file: " + e.getFileName(); |
| 130 | if (e.getLineNumber() != 0) { |
| 131 | logInfo = logInfo + " at line: " + e.getLineNumber() + " at position: " |
| 132 | + e.getCharPositionInLine(); |
| 133 | |
| 134 | } |
| 135 | if (e.getMessage() != null) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 136 | logInfo = logInfo + UtilConstants.NEW_LINE + e.getMessage(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 137 | } |
| 138 | getLog().info(logInfo); |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 139 | } |
| 140 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 141 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 142 | addToSource(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG, project, context); |
| 143 | copyYangFilesToTarget(yangFiles, getDirectory(baseDir, outputDirectory), project); |
Bharat saraswal | 5600f0f | 2016-03-09 18:34:56 +0530 | [diff] [blame] | 144 | } catch (Exception e) { |
| 145 | getLog().info(e); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 146 | //throw new MojoExecutionException("Exception occured due to " + e.getLocalizedMessage()); |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame] | 147 | } |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 148 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame^] | 149 | } |