blob: 8db668ce4b5da9e134190d216f01755d6fa83357 [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;
Bharat saraswal870c56f2016-02-20 21:57:16 +053027import org.apache.maven.plugins.annotations.Component;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053028import org.apache.maven.plugins.annotations.LifecyclePhase;
29import org.apache.maven.plugins.annotations.Mojo;
Bharat saraswal870c56f2016-02-20 21:57:16 +053030import org.apache.maven.plugins.annotations.Parameter;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053031import org.apache.maven.plugins.annotations.ResolutionScope;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053032import org.apache.maven.project.MavenProject;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053033import org.onosproject.yangutils.datamodel.YangNode;
34import org.onosproject.yangutils.parser.YangUtilsParser;
35import org.onosproject.yangutils.parser.exceptions.ParserException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053036import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
37import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
38import org.onosproject.yangutils.utils.UtilConstants;
39import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053040import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
Bharat saraswal870c56f2016-02-20 21:57:16 +053041import org.onosproject.yangutils.utils.io.impl.YangIoUtils;
42import org.sonatype.plexus.build.incremental.BuildContext;
Vinod Kumar S7a004de2016-02-05 16:15:09 +053043
44/**
Bharat saraswal870c56f2016-02-20 21:57:16 +053045 * ONOS YANG utility maven plugin. Goal of plugin is yang2java Execution phase
46 * in generate-sources requiresDependencyResolution at compile time
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053047 */
Vinod Kumar S7a004de2016-02-05 16:15:09 +053048@Mojo(name = "yang2java", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053049requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
Vinod Kumar S7a004de2016-02-05 16:15:09 +053050public class YangUtilManager extends AbstractMojo {
51
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053052 /**
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 saraswal870c56f2016-02-20 21:57:16 +053076 private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053077 private String baseDir;
78 private String searchDir;
79
80 /**
81 * Set current project.
82 *
Bharat saraswal870c56f2016-02-20 21:57:16 +053083 * @param curProject maven project.
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053084 */
Bharat saraswal870c56f2016-02-20 21:57:16 +053085 public void setCurrentProject(final MavenProject curProject) {
86 project = curProject;
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053087 }
88
Vinod Kumar S7a004de2016-02-05 16:15:09 +053089 @Override
90 public void execute() throws MojoExecutionException, MojoFailureException {
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053091
92 try {
Bharat saraswal870c56f2016-02-20 21:57:16 +053093
94 CopyrightHeader.parseCopyrightHeader();
Bharat saraswalec4ef7c2016-02-11 22:00:49 +053095 baseDir = project.getBasedir().toString();
Bharat saraswal870c56f2016-02-20 21:57:16 +053096
97 /**
98 * For deleting the generated code in previous build.
99 */
100 YangIoUtils.clean(baseDir);
101
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530102 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 saraswal870c56f2016-02-20 21:57:16 +0530110 JavaCodeGenerator.generateJavaCode(yangNode);
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530111 } catch (ParserException e) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530112 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 saraswalec4ef7c2016-02-11 22:00:49 +0530122 }
123 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530124
125 YangIoUtils.addToSource(baseDir + File.separator + UtilConstants.YANG_GEN_DIR, project, context);
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530126 } catch (final IOException e) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530127 getLog().info("IOException occured");
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530128 }
Vinod Kumar S7a004de2016-02-05 16:15:09 +0530129 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530130
Bharat saraswalec4ef7c2016-02-11 22:00:49 +0530131}