More license header changes to appc-client files
[appc.git] / appc-client / code-generator / src / main / java / org / onap / appc / tools / generator / api / MavenPlugin.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.tools.generator.api;
25
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;
34
35 import java.io.File;
36 import java.io.FileNotFoundException;
37 import java.io.IOException;
38 import java.net.MalformedURLException;
39 import java.net.URL;
40 import java.nio.file.Paths;
41
42 @Mojo(
43         name = "generate-sources",
44         defaultPhase = LifecyclePhase.GENERATE_SOURCES
45 )
46 public class MavenPlugin extends AbstractMojo {
47
48     @Parameter(property = "templateName", required = true)
49     private String templateName;
50
51     @Parameter(property = "sourceFileName")
52     private String sourceFileName;
53
54     @Parameter(property = "outputFileName")
55     private String outputFileName;
56
57     @Parameter(property = "contextBuilderClassName", required = true)
58     private String contextBuilderClassName;
59
60     @Parameter(property = "contextConfigFileName")
61     private String contextConfigFileName;
62
63     @Parameter (property = "project")
64     private MavenProject project;
65
66     @Override
67     public void execute() throws MojoExecutionException, MojoFailureException {
68         ModelGenerator generator = new ModelGenerator();
69         try {
70             trace("\t === Called MavenPlugin on builder <" + contextBuilderClassName +">\n");
71
72             //the source file may be in the class path
73             //or on the file system
74             URL sourceFileURL = lookupURL(sourceFileName);
75
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);
83         }
84     }
85
86
87     /**
88      * Converts the file to absolute path.  If the file does not exist prefix the maven project absolute path.
89      * @param filePath
90      * @return
91      */
92     private String toAbsoluteFile(String filePath){
93
94         File file = new File(filePath);
95
96         //if the file already exist just return the absolutePath
97         if(file.exists()){
98             return file.getAbsolutePath();
99     }
100
101
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;
106         }
107
108         return filePath;
109     }
110
111     /**
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.
116
117      * @param filePath - A String denoting the source yang file path.
118      * @return URL - to the source yang file
119      * @throws MalformedURLException
120      */
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);
125
126         if (sourceYangURL != null) {
127             return sourceYangURL;
128         }
129
130         String errorMessage = String.format(
131                 "YANG file <%s> not found in classpath or on the file system."
132                 ,filePath
133         );
134
135         //check the file system first
136         File sourceFile = new File(toAbsoluteFile(filePath));
137         if (!sourceFile.exists()) {
138             throw new FileNotFoundException(errorMessage);
139         }
140         try {
141             sourceYangURL = sourceFile.toURI().toURL();
142         } catch (MalformedURLException e) {
143             throw new IOException(errorMessage,e);
144         }
145         return sourceYangURL;
146     }
147
148
149     private void trace(String message) {
150         getLog().info(message);
151     }
152 }