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