Bharat saraswal
Committed by Gerrit Code Review

[ONOS-3875] Implementation of maven plugin.

Change-Id: Id2930fec97037dc238a35fea0b118e6a00300f8e
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
17 package org.onosproject.yangutils.parser; 17 package org.onosproject.yangutils.parser;
18 18
19 import org.onosproject.yangutils.datamodel.YangNode; 19 import org.onosproject.yangutils.datamodel.YangNode;
20 +import org.onosproject.yangutils.parser.exceptions.ParserException;
21 +
22 +import java.io.IOException;
20 23
21 /** 24 /**
22 * Abstraction of entity which provides parser service of YANG files for yangutils-maven-plugin. 25 * Abstraction of entity which provides parser service of YANG files for yangutils-maven-plugin.
...@@ -28,6 +31,7 @@ public interface YangUtilsParser { ...@@ -28,6 +31,7 @@ public interface YangUtilsParser {
28 * 31 *
29 * @param file input YANG file 32 * @param file input YANG file
30 * @return YangNode root node of the data model tree 33 * @return YangNode root node of the data model tree
34 + * @throws ParserException when fails to get the data model
31 */ 35 */
32 - YangNode getDataModel(String file); 36 + YangNode getDataModel(String file) throws IOException, ParserException;
33 } 37 }
......
...@@ -16,25 +16,94 @@ ...@@ -16,25 +16,94 @@
16 16
17 package org.onosproject.yangutils.plugin.manager; 17 package org.onosproject.yangutils.plugin.manager;
18 18
19 +import java.io.File;
20 +import java.io.IOException;
21 +import java.util.Iterator;
22 +import java.util.List;
23 +
19 import org.apache.maven.plugin.AbstractMojo; 24 import org.apache.maven.plugin.AbstractMojo;
20 import org.apache.maven.plugin.MojoExecutionException; 25 import org.apache.maven.plugin.MojoExecutionException;
21 import org.apache.maven.plugin.MojoFailureException; 26 import org.apache.maven.plugin.MojoFailureException;
22 import org.apache.maven.plugins.annotations.LifecyclePhase; 27 import org.apache.maven.plugins.annotations.LifecyclePhase;
23 import org.apache.maven.plugins.annotations.Mojo; 28 import org.apache.maven.plugins.annotations.Mojo;
24 import org.apache.maven.plugins.annotations.ResolutionScope; 29 import org.apache.maven.plugins.annotations.ResolutionScope;
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;
25 39
26 /** 40 /**
27 -* ONOS YANG utility maven plugin. 41 + * ONOS YANG utility maven plugin.
28 -* Goal of plugin is yang2java 42 + * Goal of plugin is yang2java
29 -* Execution phase in generate-sources 43 + * Execution phase in generate-sources
30 -* requiresDependencyResolution at compile time 44 + * requiresDependencyResolution at compile time
31 -*/ 45 + */
32 @Mojo(name = "yang2java", defaultPhase = LifecyclePhase.GENERATE_SOURCES, 46 @Mojo(name = "yang2java", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
33 - requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true) 47 +requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
34 public class YangUtilManager extends AbstractMojo { 48 public class YangUtilManager extends AbstractMojo {
35 49
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 +
36 @Override 87 @Override
37 public void execute() throws MojoExecutionException, MojoFailureException { 88 public void execute() throws MojoExecutionException, MojoFailureException {
38 - //TODO: implement the MOJO plugin 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 + }
39 } 108 }
40 -} 109 +}
...\ No newline at end of file ...\ No newline at end of file
......