blob: c15d580c5d3a15319761ea20521cc500171d8f94 [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 saraswal870c56f2016-02-20 21:57:16 +053026import java.util.List;
27
Vinod Kumar Sc4216002016-03-03 19:55:30 +053028import org.apache.maven.model.Resource;
29import org.apache.maven.project.MavenProject;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053030import org.slf4j.Logger;
31import org.sonatype.plexus.build.incremental.BuildContext;
Bharat saraswal870c56f2016-02-20 21:57:16 +053032
Bharat saraswale2d51d62016-03-23 19:40:35 +053033import static org.apache.commons.io.FileUtils.deleteDirectory;
34import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
35import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
36import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
37import static org.onosproject.yangutils.utils.UtilConstants.ORG;
38import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053039import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
40import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
41import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
42import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
43import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
44import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
45import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
46import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
47import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Vinod Kumar S38046502016-03-23 15:30:27 +053048import static org.slf4j.LoggerFactory.getLogger;
49
Bharat saraswal870c56f2016-02-20 21:57:16 +053050/**
Bharat saraswald9822e92016-04-05 15:13:44 +053051 * Represents common utility functionalities for code generation.
Bharat saraswal870c56f2016-02-20 21:57:16 +053052 */
53public final class YangIoUtils {
54
55 private static final Logger log = getLogger(YangIoUtils.class);
Bharat saraswale2d51d62016-03-23 19:40:35 +053056 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
Bharat saraswal870c56f2016-02-20 21:57:16 +053057
58 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053059 * Creates an instance of YANG io utils.
Bharat saraswal870c56f2016-02-20 21:57:16 +053060 */
61 private YangIoUtils() {
62 }
63
64 /**
65 * Creates the directory structure.
66 *
67 * @param path directory path
68 * @return directory structure
69 */
70 public static File createDirectories(String path) {
b.janani68c55e12016-02-24 12:23:03 +053071 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053072 generatedDir.mkdirs();
73 return generatedDir;
74 }
75
76 /**
77 * Adds package info file for the created directory.
78 *
79 * @param path directory path
80 * @param classInfo class info for the package
81 * @param pack package of the directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053082 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +053083 */
84 public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
85
Bharat saraswale2d51d62016-03-23 19:40:35 +053086 if (pack.contains(ORG)) {
87 String[] strArray = pack.split(ORG);
88 pack = ORG + strArray[1];
Bharat saraswal4bf8b152016-02-25 02:26:43 +053089 }
Bharat saraswal870c56f2016-02-20 21:57:16 +053090 try {
91
Bharat saraswale2d51d62016-03-23 19:40:35 +053092 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal870c56f2016-02-20 21:57:16 +053093 packageInfo.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +053094
95 FileWriter fileWriter = new FileWriter(packageInfo);
96 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
97
b.janani68c55e12016-02-24 12:23:03 +053098 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswale2d51d62016-03-23 19:40:35 +053099 bufferedWriter.write(JavaDocGen.getJavaDoc(PACKAGE_INFO, classInfo, false));
100 bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN);
101
b.janani68c55e12016-02-24 12:23:03 +0530102 bufferedWriter.close();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530103 fileWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530104 } catch (IOException e) {
105 throw new IOException("Exception occured while creating package info file.");
106 }
107 }
108
109 /**
110 * Cleans the generated directory if already exist in source folder.
111 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530112 * @param dir generated directory in previous build
Bharat saraswale2d51d62016-03-23 19:40:35 +0530113 * @throws IOException when failed to delete directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530114 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530115 public static void clean(String dir) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530116 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530117 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530118 try {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530119 deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530120 } catch (IOException e) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530121 throw new IOException("Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530122 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530123 }
124 }
125
126 /**
127 * Adds generated source directory to the compilation root.
128 *
129 * @param source directory
130 * @param project current maven project
131 * @param context current build context
132 */
133 public static void addToSource(String source, MavenProject project, BuildContext context) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530134 project.addCompileSourceRoot(source);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530135 context.refresh(project.getBasedir());
136 log.info("Source directory added to compilation root: " + source);
137 }
138
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530139 /**
140 * Removes extra char from the string.
141 *
142 * @param valueString string to be trimmed
143 * @param removealStirng extra chars
144 * @return new string
145 */
146 public static String trimAtLast(String valueString, String removealStirng) {
147 StringBuilder stringBuilder = new StringBuilder(valueString);
148 int index = valueString.lastIndexOf(removealStirng);
149 stringBuilder.deleteCharAt(index);
150 return stringBuilder.toString();
151 }
152
153 /**
154 * Returns new parted string.
155 *
156 * @param partString string to be parted
157 * @return parted string
158 */
159 public static String partString(String partString) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530160 String[] strArray = partString.split(COMMA);
161 String newString = EMPTY_STRING;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530162 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530163 if (i % 4 != 0 || i == 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530164 newString = newString + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530165 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530166 newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
167 + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530168 }
169 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530170 return trimAtLast(newString, COMMA);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530171 }
172
Vinod Kumar S38046502016-03-23 15:30:27 +0530173 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530174 * Returns the directory path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530175 *
176 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswald6f12412016-03-28 15:50:13 +0530177 * put
Vinod Kumar S38046502016-03-23 15:30:27 +0530178 * @param pathOfJavaPkg java package of the file being generated
179 * @return absolute path of the package in canonical form
180 */
181 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
182
183 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530184 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530185 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530186 String[] strArray = pathOfJavaPkg.split(SLASH);
187 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530188 return pathOfJavaPkg;
189 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530190 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530191 }
192 }
193
194 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530195 * Returns the absolute path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530196 *
197 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswald6f12412016-03-28 15:50:13 +0530198 * put
Vinod Kumar S38046502016-03-23 15:30:27 +0530199 * @param pathOfJavaPkg java package of the file being generated
200 * @return absolute path of the package in canonical form
201 */
202 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530203 return baseCodeGenPath + pathOfJavaPkg;
204 }
205
206 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530207 * Copies YANG files to the current project's output directory.
Vinod Kumar S38046502016-03-23 15:30:27 +0530208 *
209 * @param yangFiles list of YANG files
210 * @param outputDir project's output directory
211 * @param project maven project
Bharat saraswale2d51d62016-03-23 19:40:35 +0530212 * @throws IOException when fails to copy files to destination resource directory
Vinod Kumar S38046502016-03-23 15:30:27 +0530213 */
214 public static void copyYangFilesToTarget(List<String> yangFiles, String outputDir, MavenProject project)
215 throws IOException {
216
217 List<File> files = getListOfFile(yangFiles);
218
219 String path = outputDir + TARGET_RESOURCE_PATH;
220 File targetDir = new File(path);
221 targetDir.mkdirs();
222
223 for (File file : files) {
224 Files.copy(file.toPath(),
Bharat saraswale2d51d62016-03-23 19:40:35 +0530225 (new File(path + file.getName())).toPath(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530226 StandardCopyOption.REPLACE_EXISTING);
227 }
228 Resource rsc = new Resource();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530229 rsc.setDirectory(outputDir + SLASH + TEMP + SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530230 project.addResource(rsc);
231 }
232
233 /**
234 * Provides a list of files from list of strings.
235 *
236 * @param strings list of strings
237 * @return list of files
238 */
239 private static List<File> getListOfFile(List<String> strings) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530240 List<File> files = new ArrayList<>();
241 for (String file : strings) {
242 files.add(new File(file));
243 }
244 return files;
245 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530246
247 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530248 * Merges the temp java files to main java files.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530249 *
250 * @param appendFile temp file
251 * @param srcFile main file
252 * @throws IOException when fails to append contents
253 */
254 public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530255 try {
256 appendFileContents(appendFile, srcFile);
257 } catch (IOException e) {
258 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
259 }
260 }
261
262 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530263 * Inserts data in the generated file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530264 *
265 * @param file file in which need to be inserted
266 * @param data data which need to be inserted
267 * @throws IOException when fails to insert into file
268 */
269 public static void insertDataIntoJavaFile(File file, String data) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530270 try {
271 updateFileHandle(file, data, false);
272 } catch (IOException e) {
273 throw new IOException("Failed to insert in " + file + "file");
274 }
275 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530276}