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; |
| 27 | import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 28 | import org.apache.maven.plugins.annotations.Mojo; |
| 29 | import org.apache.maven.plugins.annotations.ResolutionScope; |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame^] | 30 | import org.apache.maven.project.MavenProject; |
| 31 | import org.apache.maven.plugins.annotations.Parameter; |
| 32 | import org.apache.maven.plugins.annotations.Component; |
| 33 | import org.sonatype.plexus.build.incremental.BuildContext; |
| 34 | |
| 35 | import org.onosproject.yangutils.datamodel.YangNode; |
| 36 | import org.onosproject.yangutils.parser.YangUtilsParser; |
| 37 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 38 | import org.onosproject.yangutils.utils.io.impl.YangFileScanner; |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 39 | |
| 40 | /** |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame^] | 41 | * ONOS YANG utility maven plugin. |
| 42 | * Goal of plugin is yang2java |
| 43 | * Execution phase in generate-sources |
| 44 | * requiresDependencyResolution at compile time |
| 45 | */ |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 46 | @Mojo(name = "yang2java", defaultPhase = LifecyclePhase.GENERATE_SOURCES, |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame^] | 47 | requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true) |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 48 | public class YangUtilManager extends AbstractMojo { |
| 49 | |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame^] | 50 | /** |
| 51 | * Source directory for YANG files. |
| 52 | */ |
| 53 | @Parameter(property = "yangFilesDir", defaultValue = "src/main/yang") |
| 54 | private String yangFilesDir; |
| 55 | |
| 56 | /** |
| 57 | * Output directory. |
| 58 | */ |
| 59 | @Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes") |
| 60 | private File outputDirectory; |
| 61 | |
| 62 | /** |
| 63 | * Current maven project. |
| 64 | */ |
| 65 | @Parameter(property = "project", required = true, readonly = true, defaultValue = "${project}") |
| 66 | private MavenProject project; |
| 67 | |
| 68 | /** |
| 69 | * Build context. |
| 70 | */ |
| 71 | @Component |
| 72 | private BuildContext context; |
| 73 | |
| 74 | private YangUtilsParser yangUtilsParser; |
| 75 | private String baseDir; |
| 76 | private String searchDir; |
| 77 | |
| 78 | /** |
| 79 | * Set current project. |
| 80 | * |
| 81 | * @param project maven project. |
| 82 | */ |
| 83 | public void setCurrentProject(final MavenProject project) { |
| 84 | this.project = project; |
| 85 | } |
| 86 | |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 87 | @Override |
| 88 | public void execute() throws MojoExecutionException, MojoFailureException { |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame^] | 89 | |
| 90 | try { |
| 91 | baseDir = project.getBasedir().toString(); |
| 92 | searchDir = baseDir + File.separator + yangFilesDir; |
| 93 | |
| 94 | List<String> yangFiles = YangFileScanner.getYangFiles(searchDir); |
| 95 | Iterator<String> yangFileIterator = yangFiles.iterator(); |
| 96 | while (yangFileIterator.hasNext()) { |
| 97 | String yangFile = yangFileIterator.next(); |
| 98 | try { |
| 99 | YangNode yangNode = yangUtilsParser.getDataModel(yangFile); |
| 100 | //TODO: send this data model to translator and create the corresponding java files. |
| 101 | } catch (ParserException e) { |
| 102 | getLog().info("Invalid yang file."); |
| 103 | } |
| 104 | } |
| 105 | } catch (final IOException e) { |
| 106 | getLog().info("Exception occured"); |
| 107 | } |
Vinod Kumar S | 212fcdd | 2016-02-05 16:15:09 +0530 | [diff] [blame] | 108 | } |
Bharat saraswal | f790747 | 2016-02-11 22:00:49 +0530 | [diff] [blame^] | 109 | } |