2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.tools.generator.api;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.LifecyclePhase;
31 import org.apache.maven.plugins.annotations.Mojo;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.project.MavenProject;
34 import org.onap.appc.tools.generator.impl.ModelGenerator;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.net.MalformedURLException;
41 import java.nio.file.Paths;
44 name = "generate-sources",
45 defaultPhase = LifecyclePhase.GENERATE_SOURCES
47 public class MavenPlugin extends AbstractMojo {
49 @Parameter(property = "templateName", required = true)
50 private String templateName;
52 @Parameter(property = "sourceFileName")
53 private String sourceFileName;
55 @Parameter(property = "outputFileName")
56 private String outputFileName;
58 @Parameter(property = "contextBuilderClassName", required = true)
59 private String contextBuilderClassName;
61 @Parameter(property = "contextConfigFileName")
62 private String contextConfigFileName;
64 @Parameter (property = "project")
65 private MavenProject project;
68 public void execute() throws MojoExecutionException, MojoFailureException {
69 ModelGenerator generator = new ModelGenerator();
71 trace("\t === Called MavenPlugin on builder <" + contextBuilderClassName +">\n");
73 //the source file may be in the class path
74 //or on the file system
75 URL sourceFileURL = lookupURL(sourceFileName);
77 //prefix with the project absolute path to the output file
78 outputFileName = toAbsoluteFile(outputFileName);
79 String workDirectory = Paths.get(outputFileName).getParent().toString();
80 generator.execute(sourceFileURL,outputFileName,templateName,contextBuilderClassName,contextConfigFileName);
81 project.addCompileSourceRoot(workDirectory);
82 } catch (Exception e) {
83 throw new MojoExecutionException(e.getMessage(),e);
89 * Converts the file to absolute path. If the file does not exist prefix the maven project absolute path.
93 private String toAbsoluteFile(String filePath){
95 File file = new File(filePath);
97 //if the file already exist just return the absolutePath
99 return file.getAbsolutePath();
103 //prefix with the project absolute path to the output file
104 if(!file.isAbsolute()){
105 File projectDir = new File(this.project.getBuild().getDirectory()).getParentFile();
106 filePath = projectDir.getAbsolutePath() + "/" + filePath;
113 * Tries three lookups
114 * First try to lookup the file in the classpath.
115 * else try relative path
116 * else try prefixing the relative path with the maven project path.
118 * @param filePath - A String denoting the source yang file path.
119 * @return URL - to the source yang file
120 * @throws MalformedURLException
122 private URL lookupURL(String filePath) throws IOException {
123 //check out the class path first
124 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
125 URL sourceYangURL = classLoader.getResource(filePath);
127 if (sourceYangURL != null) {
128 return sourceYangURL;
131 String errorMessage = String.format(
132 "YANG file <%s> not found in classpath or on the file system."
136 //check the file system first
137 File sourceFile = new File(toAbsoluteFile(filePath));
138 if (!sourceFile.exists()) {
139 throw new FileNotFoundException(errorMessage);
142 sourceYangURL = sourceFile.toURI().toURL();
143 } catch (MalformedURLException e) {
144 throw new IOException(errorMessage,e);
146 return sourceYangURL;
150 private void trace(String message) {
151 getLog().info(message);