blob: a076fbe27359780e5e57a86f96115085ae17da7e [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal870c56f2016-02-20 21:57:16 +05303 *
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.utils.io.impl;
18
19import java.io.BufferedWriter;
20import java.io.File;
21import java.io.FileWriter;
22import java.io.IOException;
Vinod Kumar S38046502016-03-23 15:30:27 +053023import java.nio.file.Files;
24import java.nio.file.StandardCopyOption;
25import java.util.ArrayList;
Bharat saraswalc0e04842016-05-12 13:16:57 +053026import java.util.LinkedList;
Bharat saraswal870c56f2016-02-20 21:57:16 +053027import java.util.List;
Bharat saraswalc0e04842016-05-12 13:16:57 +053028import java.util.Stack;
Bharat saraswal870c56f2016-02-20 21:57:16 +053029
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053030import org.apache.commons.io.FileUtils;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053031import org.apache.maven.model.Resource;
32import org.apache.maven.project.MavenProject;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053033import org.slf4j.Logger;
34import org.sonatype.plexus.build.incremental.BuildContext;
Bharat saraswal870c56f2016-02-20 21:57:16 +053035
Bharat saraswale2d51d62016-03-23 19:40:35 +053036import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
37import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
38import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
39import static org.onosproject.yangutils.utils.UtilConstants.ORG;
40import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053041import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
42import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
43import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
44import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
45import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
46import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
47import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
48import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
Bharat saraswald72411a2016-04-19 01:00:16 +053049import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Bharat saraswale2d51d62016-03-23 19:40:35 +053050import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Vinod Kumar S38046502016-03-23 15:30:27 +053051import static org.slf4j.LoggerFactory.getLogger;
52
Bharat saraswal870c56f2016-02-20 21:57:16 +053053/**
Bharat saraswald9822e92016-04-05 15:13:44 +053054 * Represents common utility functionalities for code generation.
Bharat saraswal870c56f2016-02-20 21:57:16 +053055 */
56public final class YangIoUtils {
57
58 private static final Logger log = getLogger(YangIoUtils.class);
Bharat saraswale2d51d62016-03-23 19:40:35 +053059 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
Bharat saraswal870c56f2016-02-20 21:57:16 +053060
61 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053062 * Creates an instance of YANG io utils.
Bharat saraswal870c56f2016-02-20 21:57:16 +053063 */
64 private YangIoUtils() {
65 }
66
67 /**
68 * Creates the directory structure.
69 *
70 * @param path directory path
71 * @return directory structure
72 */
73 public static File createDirectories(String path) {
b.janani68c55e12016-02-24 12:23:03 +053074 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053075 generatedDir.mkdirs();
76 return generatedDir;
77 }
78
79 /**
80 * Adds package info file for the created directory.
81 *
82 * @param path directory path
83 * @param classInfo class info for the package
84 * @param pack package of the directory
Bharat saraswalc0e04842016-05-12 13:16:57 +053085 * @param isChildNode is it a child node
Vinod Kumar Sc4216002016-03-03 19:55:30 +053086 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +053087 */
Bharat saraswalc0e04842016-05-12 13:16:57 +053088 public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode)
89 throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +053090
Bharat saraswale2d51d62016-03-23 19:40:35 +053091 if (pack.contains(ORG)) {
92 String[] strArray = pack.split(ORG);
93 pack = ORG + strArray[1];
Bharat saraswal4bf8b152016-02-25 02:26:43 +053094 }
Bharat saraswal870c56f2016-02-20 21:57:16 +053095 try {
96
Bharat saraswale2d51d62016-03-23 19:40:35 +053097 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal870c56f2016-02-20 21:57:16 +053098 packageInfo.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +053099
100 FileWriter fileWriter = new FileWriter(packageInfo);
101 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
102
b.janani68c55e12016-02-24 12:23:03 +0530103 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530104 bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode));
Bharat saraswale2d51d62016-03-23 19:40:35 +0530105 bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN);
106
b.janani68c55e12016-02-24 12:23:03 +0530107 bufferedWriter.close();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530108 fileWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530109 } catch (IOException e) {
110 throw new IOException("Exception occured while creating package info file.");
111 }
112 }
113
114 /**
115 * Cleans the generated directory if already exist in source folder.
116 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530117 * @param dir generated directory in previous build
Bharat saraswale2d51d62016-03-23 19:40:35 +0530118 * @throws IOException when failed to delete directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530119 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530120 public static void deleteDirectory(String dir) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530122 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530123 try {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530124 FileUtils.deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 } catch (IOException e) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530126 throw new IOException(
127 "Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530128 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530129 }
130 }
131
132 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530133 * Searches and deletes generated temporary directories.
134 *
135 * @param root root directory
136 * @throws IOException when fails to do IO operations.
137 */
138 public static void searchAndDeleteTempDir(String root) throws IOException {
139 List<File> store = new LinkedList<>();
140 Stack<String> stack = new Stack<>();
141 stack.push(root);
142
143 while (!stack.empty()) {
144 root = stack.pop();
145 File file = new File(root);
146 File[] filelist = file.listFiles();
147 if (filelist == null || filelist.length == 0) {
148 continue;
149 }
150 for (File current : filelist) {
151 if (current.isDirectory()) {
152 stack.push(current.toString());
153 if (current.getName().endsWith("-Temp")) {
154 store.add(current);
155 }
156 }
157 }
158 }
159
160 for (File dir : store) {
161 dir.delete();
162 }
163 }
164
165 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530166 * Adds generated source directory to the compilation root.
167 *
168 * @param source directory
169 * @param project current maven project
170 * @param context current build context
171 */
172 public static void addToSource(String source, MavenProject project, BuildContext context) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530173 project.addCompileSourceRoot(source);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530174 context.refresh(project.getBasedir());
175 log.info("Source directory added to compilation root: " + source);
176 }
177
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530178 /**
179 * Removes extra char from the string.
180 *
181 * @param valueString string to be trimmed
182 * @param removealStirng extra chars
183 * @return new string
184 */
185 public static String trimAtLast(String valueString, String removealStirng) {
186 StringBuilder stringBuilder = new StringBuilder(valueString);
187 int index = valueString.lastIndexOf(removealStirng);
188 stringBuilder.deleteCharAt(index);
189 return stringBuilder.toString();
190 }
191
192 /**
193 * Returns new parted string.
194 *
195 * @param partString string to be parted
196 * @return parted string
197 */
198 public static String partString(String partString) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530199 String[] strArray = partString.split(COMMA);
200 String newString = EMPTY_STRING;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530201 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530202 if (i % 4 != 0 || i == 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530203 newString = newString + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530204 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530205 newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
206 + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530207 }
208 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530209 return trimAtLast(newString, COMMA);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530210 }
211
Vinod Kumar S38046502016-03-23 15:30:27 +0530212 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530213 * Returns the directory path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530214 *
215 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswald6f12412016-03-28 15:50:13 +0530216 * put
Vinod Kumar S38046502016-03-23 15:30:27 +0530217 * @param pathOfJavaPkg java package of the file being generated
218 * @return absolute path of the package in canonical form
219 */
220 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
221
222 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530223 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530224 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530225 String[] strArray = pathOfJavaPkg.split(SLASH);
226 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530227 return pathOfJavaPkg;
228 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530229 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530230 }
231 }
232
233 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530234 * Returns the absolute path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530235 *
236 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswald6f12412016-03-28 15:50:13 +0530237 * put
Vinod Kumar S38046502016-03-23 15:30:27 +0530238 * @param pathOfJavaPkg java package of the file being generated
239 * @return absolute path of the package in canonical form
240 */
241 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530242 return baseCodeGenPath + pathOfJavaPkg;
243 }
244
245 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530246 * Copies YANG files to the current project's output directory.
Vinod Kumar S38046502016-03-23 15:30:27 +0530247 *
248 * @param yangFiles list of YANG files
249 * @param outputDir project's output directory
250 * @param project maven project
Bharat saraswale2d51d62016-03-23 19:40:35 +0530251 * @throws IOException when fails to copy files to destination resource directory
Vinod Kumar S38046502016-03-23 15:30:27 +0530252 */
253 public static void copyYangFilesToTarget(List<String> yangFiles, String outputDir, MavenProject project)
254 throws IOException {
255
256 List<File> files = getListOfFile(yangFiles);
257
258 String path = outputDir + TARGET_RESOURCE_PATH;
259 File targetDir = new File(path);
260 targetDir.mkdirs();
261
262 for (File file : files) {
263 Files.copy(file.toPath(),
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530264 new File(path + file.getName()).toPath(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530265 StandardCopyOption.REPLACE_EXISTING);
266 }
267 Resource rsc = new Resource();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530268 rsc.setDirectory(outputDir + SLASH + TEMP + SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530269 project.addResource(rsc);
270 }
271
272 /**
273 * Provides a list of files from list of strings.
274 *
275 * @param strings list of strings
276 * @return list of files
277 */
278 private static List<File> getListOfFile(List<String> strings) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530279 List<File> files = new ArrayList<>();
280 for (String file : strings) {
281 files.add(new File(file));
282 }
283 return files;
284 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530285
286 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530287 * Merges the temp java files to main java files.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530288 *
289 * @param appendFile temp file
290 * @param srcFile main file
291 * @throws IOException when fails to append contents
292 */
293 public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530294 try {
295 appendFileContents(appendFile, srcFile);
296 } catch (IOException e) {
297 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
298 }
299 }
300
301 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530302 * Inserts data in the generated file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530303 *
304 * @param file file in which need to be inserted
305 * @param data data which need to be inserted
306 * @throws IOException when fails to insert into file
307 */
308 public static void insertDataIntoJavaFile(File file, String data) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530309 try {
310 updateFileHandle(file, data, false);
311 } catch (IOException e) {
312 throw new IOException("Failed to insert in " + file + "file");
313 }
314 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530315}