blob: b8c9f35efc985f41eb6c0efc1c55a9868d17cd4b [file] [log] [blame]
Vinod Kumar S7a004de2016-02-05 16:15:09 +05301/*
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
17package org.onosproject.yangutils.plugin.manager;
18
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053019import java.io.File;
20import java.io.IOException;
21import java.util.Iterator;
22import java.util.List;
23
Vinod Kumar S7a004de2016-02-05 16:15:09 +053024import org.apache.maven.plugin.AbstractMojo;
25import org.apache.maven.plugin.MojoExecutionException;
26import org.apache.maven.plugin.MojoFailureException;
27import org.apache.maven.plugins.annotations.LifecyclePhase;
28import org.apache.maven.plugins.annotations.Mojo;
29import org.apache.maven.plugins.annotations.ResolutionScope;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053030import org.apache.maven.project.MavenProject;
31import org.apache.maven.plugins.annotations.Parameter;
32import org.apache.maven.plugins.annotations.Component;
33import org.sonatype.plexus.build.incremental.BuildContext;
34
35import org.onosproject.yangutils.datamodel.YangNode;
36import org.onosproject.yangutils.parser.YangUtilsParser;
37import org.onosproject.yangutils.parser.exceptions.ParserException;
38import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053039
40/**
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053041 * 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 S7a004de2016-02-05 16:15:09 +053046@Mojo(name = "yang2java", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053047requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
Vinod Kumar S7a004de2016-02-05 16:15:09 +053048public class YangUtilManager extends AbstractMojo {
49
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053050 /**
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 S7a004de2016-02-05 16:15:09 +053087 @Override
88 public void execute() throws MojoExecutionException, MojoFailureException {
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053089
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 S7a004de2016-02-05 16:15:09 +0530108 }
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530109}