2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.tools.generator.api;
26 import org.apache.maven.plugin.AbstractMojo;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.MojoFailureException;
29 import org.apache.maven.plugins.annotations.LifecyclePhase;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.project.MavenProject;
33 import org.onap.appc.tools.generator.impl.ModelGenerator;
36 import java.io.FileNotFoundException;
37 import java.io.IOException;
38 import java.net.MalformedURLException;
40 import java.nio.file.Paths;
43 name = "generate-sources",
44 defaultPhase = LifecyclePhase.GENERATE_SOURCES
46 public class MavenPlugin extends AbstractMojo {
48 @Parameter(property = "templateName", required = true)
49 private String templateName;
51 @Parameter(property = "sourceFileName")
52 private String sourceFileName;
54 @Parameter(property = "outputFileName")
55 private String outputFileName;
57 @Parameter(property = "contextBuilderClassName", required = true)
58 private String contextBuilderClassName;
60 @Parameter(property = "contextConfigFileName")
61 private String contextConfigFileName;
63 @Parameter (property = "project")
64 private MavenProject project;
67 public void execute() throws MojoExecutionException, MojoFailureException {
68 ModelGenerator generator = new ModelGenerator();
70 trace("\t === Called MavenPlugin on builder <" + contextBuilderClassName +">\n");
72 //the source file may be in the class path
73 //or on the file system
74 URL sourceFileURL = lookupURL(sourceFileName);
76 //prefix with the project absolute path to the output file
77 outputFileName = toAbsoluteFile(outputFileName);
78 String workDirectory = Paths.get(outputFileName).getParent().toString();
79 generator.execute(sourceFileURL,outputFileName,templateName,contextBuilderClassName,contextConfigFileName);
80 project.addCompileSourceRoot(workDirectory);
81 } catch (Exception e) {
82 throw new MojoExecutionException(e.getMessage(),e);
88 * Converts the file to absolute path. If the file does not exist prefix the maven project absolute path.
92 private String toAbsoluteFile(String filePath){
94 File file = new File(filePath);
96 //if the file already exist just return the absolutePath
98 return file.getAbsolutePath();
102 //prefix with the project absolute path to the output file
103 if(!file.isAbsolute()){
104 File projectDir = new File(this.project.getBuild().getDirectory()).getParentFile();
105 filePath = projectDir.getAbsolutePath() + "/" + filePath;
112 * Tries three lookups
113 * First try to lookup the file in the classpath.
114 * else try relative path
115 * else try prefixing the relative path with the maven project path.
117 * @param filePath - A String denoting the source yang file path.
118 * @return URL - to the source yang file
119 * @throws MalformedURLException
121 private URL lookupURL(String filePath) throws IOException {
122 //check out the class path first
123 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
124 URL sourceYangURL = classLoader.getResource(filePath);
126 if (sourceYangURL != null) {
127 return sourceYangURL;
130 String errorMessage = String.format(
131 "YANG file <%s> not found in classpath or on the file system."
135 //check the file system first
136 File sourceFile = new File(toAbsoluteFile(filePath));
137 if (!sourceFile.exists()) {
138 throw new FileNotFoundException(errorMessage);
141 sourceYangURL = sourceFile.toURI().toURL();
142 } catch (MalformedURLException e) {
143 throw new IOException(errorMessage,e);
145 return sourceYangURL;
149 private void trace(String message) {
150 getLog().info(message);