blob: 835b6758055b540f9e2dff94d54abd9956b56ad8 [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;
Vidyashree Rama1db15562016-05-17 16:16:15 +053026import java.util.Iterator;
Bharat saraswal870c56f2016-02-20 21:57:16 +053027import java.util.List;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053028import java.util.LinkedList;
Bharat saraswalc0e04842016-05-12 13:16:57 +053029import java.util.Stack;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053030import java.util.Set;
Bharat saraswal870c56f2016-02-20 21:57:16 +053031
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053032import org.apache.commons.io.FileUtils;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053033import org.apache.maven.model.Resource;
34import org.apache.maven.project.MavenProject;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053035import org.onosproject.yangutils.plugin.manager.YangFileInfo;
36
Vinod Kumar Sc4216002016-03-03 19:55:30 +053037import org.slf4j.Logger;
38import org.sonatype.plexus.build.incremental.BuildContext;
Bharat saraswal870c56f2016-02-20 21:57:16 +053039
Bharat saraswale2d51d62016-03-23 19:40:35 +053040import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
41import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
42import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
43import static org.onosproject.yangutils.utils.UtilConstants.ORG;
44import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053045import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
46import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
47import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
48import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
49import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
50import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
51import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
52import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
Bharat saraswald72411a2016-04-19 01:00:16 +053053import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Bharat saraswale2d51d62016-03-23 19:40:35 +053054import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Vinod Kumar S38046502016-03-23 15:30:27 +053055import static org.slf4j.LoggerFactory.getLogger;
56
Bharat saraswal870c56f2016-02-20 21:57:16 +053057/**
Bharat saraswald9822e92016-04-05 15:13:44 +053058 * Represents common utility functionalities for code generation.
Bharat saraswal870c56f2016-02-20 21:57:16 +053059 */
60public final class YangIoUtils {
61
62 private static final Logger log = getLogger(YangIoUtils.class);
Bharat saraswale2d51d62016-03-23 19:40:35 +053063 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
Bharat saraswal870c56f2016-02-20 21:57:16 +053064
65 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053066 * Creates an instance of YANG io utils.
Bharat saraswal870c56f2016-02-20 21:57:16 +053067 */
68 private YangIoUtils() {
69 }
70
71 /**
72 * Creates the directory structure.
73 *
74 * @param path directory path
75 * @return directory structure
76 */
77 public static File createDirectories(String path) {
b.janani68c55e12016-02-24 12:23:03 +053078 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053079 generatedDir.mkdirs();
80 return generatedDir;
81 }
82
83 /**
84 * Adds package info file for the created directory.
85 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053086 * @param path directory path
87 * @param classInfo class info for the package
88 * @param pack package of the directory
Bharat saraswalc0e04842016-05-12 13:16:57 +053089 * @param isChildNode is it a child node
Vinod Kumar Sc4216002016-03-03 19:55:30 +053090 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +053091 */
Bharat saraswalc0e04842016-05-12 13:16:57 +053092 public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode)
93 throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +053094
Bharat saraswale2d51d62016-03-23 19:40:35 +053095 if (pack.contains(ORG)) {
96 String[] strArray = pack.split(ORG);
97 pack = ORG + strArray[1];
Bharat saraswal4bf8b152016-02-25 02:26:43 +053098 }
Bharat saraswal870c56f2016-02-20 21:57:16 +053099 try {
100
Bharat saraswale2d51d62016-03-23 19:40:35 +0530101 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530102 packageInfo.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530103
104 FileWriter fileWriter = new FileWriter(packageInfo);
105 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
106
b.janani68c55e12016-02-24 12:23:03 +0530107 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530108 bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode));
Bharat saraswale2d51d62016-03-23 19:40:35 +0530109 bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN);
110
b.janani68c55e12016-02-24 12:23:03 +0530111 bufferedWriter.close();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530112 fileWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530113 } catch (IOException e) {
114 throw new IOException("Exception occured while creating package info file.");
115 }
116 }
117
118 /**
119 * Cleans the generated directory if already exist in source folder.
120 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 * @param dir generated directory in previous build
Bharat saraswale2d51d62016-03-23 19:40:35 +0530122 * @throws IOException when failed to delete directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530123 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530124 public static void deleteDirectory(String dir) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530125 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530126 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530127 try {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530128 FileUtils.deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530129 } catch (IOException e) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530130 throw new IOException(
131 "Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530132 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530133 }
134 }
135
136 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530137 * Searches and deletes generated temporary directories.
138 *
139 * @param root root directory
140 * @throws IOException when fails to do IO operations.
141 */
142 public static void searchAndDeleteTempDir(String root) throws IOException {
143 List<File> store = new LinkedList<>();
144 Stack<String> stack = new Stack<>();
145 stack.push(root);
146
147 while (!stack.empty()) {
148 root = stack.pop();
149 File file = new File(root);
150 File[] filelist = file.listFiles();
151 if (filelist == null || filelist.length == 0) {
152 continue;
153 }
154 for (File current : filelist) {
155 if (current.isDirectory()) {
156 stack.push(current.toString());
157 if (current.getName().endsWith("-Temp")) {
158 store.add(current);
159 }
160 }
161 }
162 }
163
164 for (File dir : store) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530165 FileUtils.deleteDirectory(dir);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530166 }
167 }
168
169 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530170 * Adds generated source directory to the compilation root.
171 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530172 * @param source directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530173 * @param project current maven project
174 * @param context current build context
175 */
176 public static void addToSource(String source, MavenProject project, BuildContext context) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530177 project.addCompileSourceRoot(source);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530178 context.refresh(project.getBasedir());
179 log.info("Source directory added to compilation root: " + source);
180 }
181
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530182 /**
183 * Removes extra char from the string.
184 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530185 * @param valueString string to be trimmed
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530186 * @param removealStirng extra chars
187 * @return new string
188 */
189 public static String trimAtLast(String valueString, String removealStirng) {
190 StringBuilder stringBuilder = new StringBuilder(valueString);
191 int index = valueString.lastIndexOf(removealStirng);
192 stringBuilder.deleteCharAt(index);
193 return stringBuilder.toString();
194 }
195
196 /**
197 * Returns new parted string.
198 *
199 * @param partString string to be parted
200 * @return parted string
201 */
202 public static String partString(String partString) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530203 String[] strArray = partString.split(COMMA);
204 String newString = EMPTY_STRING;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530205 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530206 if (i % 4 != 0 || i == 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530207 newString = newString + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530208 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530209 newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
210 + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530211 }
212 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530213 return trimAtLast(newString, COMMA);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530214 }
215
Vinod Kumar S38046502016-03-23 15:30:27 +0530216 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530217 * Returns the directory path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530218 *
219 * @param baseCodeGenPath base path where the generated files needs to be
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530220 * put
221 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530222 * @return absolute path of the package in canonical form
223 */
224 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
225
226 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530227 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530228 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530229 String[] strArray = pathOfJavaPkg.split(SLASH);
230 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530231 return pathOfJavaPkg;
232 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530233 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530234 }
235 }
236
237 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530238 * Returns the absolute path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530239 *
240 * @param baseCodeGenPath base path where the generated files needs to be
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530241 * put
242 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530243 * @return absolute path of the package in canonical form
244 */
245 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530246 return baseCodeGenPath + pathOfJavaPkg;
247 }
248
249 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530250 * Copies YANG files to the current project's output directory.
Vinod Kumar S38046502016-03-23 15:30:27 +0530251 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530252 * @param yangFileInfo set of YANG files
253 * @param outputDir project's output directory
254 * @param project maven project
Bharat saraswale2d51d62016-03-23 19:40:35 +0530255 * @throws IOException when fails to copy files to destination resource directory
Vinod Kumar S38046502016-03-23 15:30:27 +0530256 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530257 public static void copyYangFilesToTarget(Set<YangFileInfo> yangFileInfo, String outputDir, MavenProject project)
Vinod Kumar S38046502016-03-23 15:30:27 +0530258 throws IOException {
259
Vidyashree Rama1db15562016-05-17 16:16:15 +0530260 List<File> files = getListOfFile(yangFileInfo);
Vinod Kumar S38046502016-03-23 15:30:27 +0530261
262 String path = outputDir + TARGET_RESOURCE_PATH;
263 File targetDir = new File(path);
264 targetDir.mkdirs();
265
266 for (File file : files) {
267 Files.copy(file.toPath(),
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530268 new File(path + file.getName()).toPath(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530269 StandardCopyOption.REPLACE_EXISTING);
270 }
271 Resource rsc = new Resource();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530272 rsc.setDirectory(outputDir + SLASH + TEMP + SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530273 project.addResource(rsc);
274 }
275
276 /**
277 * Provides a list of files from list of strings.
278 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530279 * @param yangFileInfo set of yang file information
Vinod Kumar S38046502016-03-23 15:30:27 +0530280 * @return list of files
281 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530282 private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530283 List<File> files = new ArrayList<>();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530284 Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
285 while (yangFileIterator.hasNext()) {
286 YangFileInfo yangFile = yangFileIterator.next();
287 files.add(new File(yangFile.getYangFileName()));
Vinod Kumar S38046502016-03-23 15:30:27 +0530288 }
289 return files;
290 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530291
292 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530293 * Merges the temp java files to main java files.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530294 *
295 * @param appendFile temp file
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530296 * @param srcFile main file
Bharat saraswale2d51d62016-03-23 19:40:35 +0530297 * @throws IOException when fails to append contents
298 */
299 public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530300 try {
301 appendFileContents(appendFile, srcFile);
302 } catch (IOException e) {
303 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
304 }
305 }
306
307 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530308 * Inserts data in the generated file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530309 *
310 * @param file file in which need to be inserted
311 * @param data data which need to be inserted
312 * @throws IOException when fails to insert into file
313 */
314 public static void insertDataIntoJavaFile(File file, String data) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530315 try {
316 updateFileHandle(file, data, false);
317 } catch (IOException e) {
318 throw new IOException("Failed to insert in " + file + "file");
319 }
320 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530321}