blob: d8f7e53d7466391d050d758daa6563d2496eb94b [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;
Vidyashree Rama1db15562016-05-17 16:16:15 +053027import java.util.Iterator;
Bharat saraswal870c56f2016-02-20 21:57:16 +053028import java.util.List;
Bharat saraswalc0e04842016-05-12 13:16:57 +053029import java.util.Stack;
Bharat saraswal870c56f2016-02-20 21:57:16 +053030
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053031import org.apache.commons.io.FileUtils;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053032import org.apache.maven.model.Resource;
33import org.apache.maven.project.MavenProject;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053034import org.slf4j.Logger;
35import org.sonatype.plexus.build.incremental.BuildContext;
Bharat saraswal870c56f2016-02-20 21:57:16 +053036
Vidyashree Rama1db15562016-05-17 16:16:15 +053037import org.onosproject.yangutils.plugin.manager.YangFileInfo;
Bharat saraswale2d51d62016-03-23 19:40:35 +053038import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
39import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
40import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
41import static org.onosproject.yangutils.utils.UtilConstants.ORG;
42import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053043import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
44import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
45import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
46import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
47import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
48import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
49import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
50import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
Bharat saraswald72411a2016-04-19 01:00:16 +053051import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Bharat saraswale2d51d62016-03-23 19:40:35 +053052import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Vinod Kumar S38046502016-03-23 15:30:27 +053053import static org.slf4j.LoggerFactory.getLogger;
54
Bharat saraswal870c56f2016-02-20 21:57:16 +053055/**
Bharat saraswald9822e92016-04-05 15:13:44 +053056 * Represents common utility functionalities for code generation.
Bharat saraswal870c56f2016-02-20 21:57:16 +053057 */
58public final class YangIoUtils {
59
60 private static final Logger log = getLogger(YangIoUtils.class);
Bharat saraswale2d51d62016-03-23 19:40:35 +053061 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
Bharat saraswal870c56f2016-02-20 21:57:16 +053062
63 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053064 * Creates an instance of YANG io utils.
Bharat saraswal870c56f2016-02-20 21:57:16 +053065 */
66 private YangIoUtils() {
67 }
68
69 /**
70 * Creates the directory structure.
71 *
72 * @param path directory path
73 * @return directory structure
74 */
75 public static File createDirectories(String path) {
b.janani68c55e12016-02-24 12:23:03 +053076 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053077 generatedDir.mkdirs();
78 return generatedDir;
79 }
80
81 /**
82 * Adds package info file for the created directory.
83 *
84 * @param path directory path
85 * @param classInfo class info for the package
86 * @param pack package of the directory
Bharat saraswalc0e04842016-05-12 13:16:57 +053087 * @param isChildNode is it a child node
Vinod Kumar Sc4216002016-03-03 19:55:30 +053088 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +053089 */
Bharat saraswalc0e04842016-05-12 13:16:57 +053090 public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode)
91 throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +053092
Bharat saraswale2d51d62016-03-23 19:40:35 +053093 if (pack.contains(ORG)) {
94 String[] strArray = pack.split(ORG);
95 pack = ORG + strArray[1];
Bharat saraswal4bf8b152016-02-25 02:26:43 +053096 }
Bharat saraswal870c56f2016-02-20 21:57:16 +053097 try {
98
Bharat saraswale2d51d62016-03-23 19:40:35 +053099 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530100 packageInfo.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530101
102 FileWriter fileWriter = new FileWriter(packageInfo);
103 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
104
b.janani68c55e12016-02-24 12:23:03 +0530105 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530106 bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode));
Bharat saraswale2d51d62016-03-23 19:40:35 +0530107 bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN);
108
b.janani68c55e12016-02-24 12:23:03 +0530109 bufferedWriter.close();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530110 fileWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530111 } catch (IOException e) {
112 throw new IOException("Exception occured while creating package info file.");
113 }
114 }
115
116 /**
117 * Cleans the generated directory if already exist in source folder.
118 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530119 * @param dir generated directory in previous build
Bharat saraswale2d51d62016-03-23 19:40:35 +0530120 * @throws IOException when failed to delete directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530121 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530122 public static void deleteDirectory(String dir) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530123 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530124 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 try {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530126 FileUtils.deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530127 } catch (IOException e) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530128 throw new IOException(
129 "Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530130 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530131 }
132 }
133
134 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530135 * Searches and deletes generated temporary directories.
136 *
137 * @param root root directory
138 * @throws IOException when fails to do IO operations.
139 */
140 public static void searchAndDeleteTempDir(String root) throws IOException {
141 List<File> store = new LinkedList<>();
142 Stack<String> stack = new Stack<>();
143 stack.push(root);
144
145 while (!stack.empty()) {
146 root = stack.pop();
147 File file = new File(root);
148 File[] filelist = file.listFiles();
149 if (filelist == null || filelist.length == 0) {
150 continue;
151 }
152 for (File current : filelist) {
153 if (current.isDirectory()) {
154 stack.push(current.toString());
155 if (current.getName().endsWith("-Temp")) {
156 store.add(current);
157 }
158 }
159 }
160 }
161
162 for (File dir : store) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530163 FileUtils.deleteDirectory(dir);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530164 }
165 }
166
167 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530168 * Adds generated source directory to the compilation root.
169 *
170 * @param source directory
171 * @param project current maven project
172 * @param context current build context
173 */
174 public static void addToSource(String source, MavenProject project, BuildContext context) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530175 project.addCompileSourceRoot(source);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530176 context.refresh(project.getBasedir());
177 log.info("Source directory added to compilation root: " + source);
178 }
179
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530180 /**
181 * Removes extra char from the string.
182 *
183 * @param valueString string to be trimmed
184 * @param removealStirng extra chars
185 * @return new string
186 */
187 public static String trimAtLast(String valueString, String removealStirng) {
188 StringBuilder stringBuilder = new StringBuilder(valueString);
189 int index = valueString.lastIndexOf(removealStirng);
190 stringBuilder.deleteCharAt(index);
191 return stringBuilder.toString();
192 }
193
194 /**
195 * Returns new parted string.
196 *
197 * @param partString string to be parted
198 * @return parted string
199 */
200 public static String partString(String partString) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530201 String[] strArray = partString.split(COMMA);
202 String newString = EMPTY_STRING;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530203 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530204 if (i % 4 != 0 || i == 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530205 newString = newString + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530206 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530207 newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
208 + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530209 }
210 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530211 return trimAtLast(newString, COMMA);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530212 }
213
Vinod Kumar S38046502016-03-23 15:30:27 +0530214 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530215 * Returns the directory path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530216 *
217 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswald6f12412016-03-28 15:50:13 +0530218 * put
Vinod Kumar S38046502016-03-23 15:30:27 +0530219 * @param pathOfJavaPkg java package of the file being generated
220 * @return absolute path of the package in canonical form
221 */
222 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
223
224 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530225 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530226 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530227 String[] strArray = pathOfJavaPkg.split(SLASH);
228 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530229 return pathOfJavaPkg;
230 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530231 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530232 }
233 }
234
235 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530236 * Returns the absolute path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530237 *
238 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswald6f12412016-03-28 15:50:13 +0530239 * put
Vinod Kumar S38046502016-03-23 15:30:27 +0530240 * @param pathOfJavaPkg java package of the file being generated
241 * @return absolute path of the package in canonical form
242 */
243 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530244 return baseCodeGenPath + pathOfJavaPkg;
245 }
246
247 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530248 * Copies YANG files to the current project's output directory.
Vinod Kumar S38046502016-03-23 15:30:27 +0530249 *
Vidyashree Rama1db15562016-05-17 16:16:15 +0530250 * @param yangFileInfo list of YANG files
Vinod Kumar S38046502016-03-23 15:30:27 +0530251 * @param outputDir project's output directory
252 * @param project maven project
Bharat saraswale2d51d62016-03-23 19:40:35 +0530253 * @throws IOException when fails to copy files to destination resource directory
Vinod Kumar S38046502016-03-23 15:30:27 +0530254 */
Vidyashree Rama1db15562016-05-17 16:16:15 +0530255 public static void copyYangFilesToTarget(List<YangFileInfo> yangFileInfo, String outputDir, MavenProject project)
Vinod Kumar S38046502016-03-23 15:30:27 +0530256 throws IOException {
257
Vidyashree Rama1db15562016-05-17 16:16:15 +0530258 List<File> files = getListOfFile(yangFileInfo);
Vinod Kumar S38046502016-03-23 15:30:27 +0530259
260 String path = outputDir + TARGET_RESOURCE_PATH;
261 File targetDir = new File(path);
262 targetDir.mkdirs();
263
264 for (File file : files) {
265 Files.copy(file.toPath(),
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530266 new File(path + file.getName()).toPath(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530267 StandardCopyOption.REPLACE_EXISTING);
268 }
269 Resource rsc = new Resource();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530270 rsc.setDirectory(outputDir + SLASH + TEMP + SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530271 project.addResource(rsc);
272 }
273
274 /**
275 * Provides a list of files from list of strings.
276 *
Vidyashree Rama1db15562016-05-17 16:16:15 +0530277 * @param yangFileInfo list of yang file information
Vinod Kumar S38046502016-03-23 15:30:27 +0530278 * @return list of files
279 */
Vidyashree Rama1db15562016-05-17 16:16:15 +0530280 private static List<File> getListOfFile(List<YangFileInfo> yangFileInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530281 List<File> files = new ArrayList<>();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530282 Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
283 while (yangFileIterator.hasNext()) {
284 YangFileInfo yangFile = yangFileIterator.next();
285 files.add(new File(yangFile.getYangFileName()));
Vinod Kumar S38046502016-03-23 15:30:27 +0530286 }
287 return files;
288 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530289
290 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530291 * Merges the temp java files to main java files.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530292 *
293 * @param appendFile temp file
294 * @param srcFile main file
295 * @throws IOException when fails to append contents
296 */
297 public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530298 try {
299 appendFileContents(appendFile, srcFile);
300 } catch (IOException e) {
301 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
302 }
303 }
304
305 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530306 * Inserts data in the generated file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530307 *
308 * @param file file in which need to be inserted
309 * @param data data which need to be inserted
310 * @throws IOException when fails to insert into file
311 */
312 public static void insertDataIntoJavaFile(File file, String data) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530313 try {
314 updateFileHandle(file, data, false);
315 } catch (IOException e) {
316 throw new IOException("Failed to insert in " + file + "file");
317 }
318 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530319}