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