blob: 62330fb3a13bb17afdeab356bda81a1da03cf52b [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
tom5f38b3a2014-08-27 23:50:54 -070016package org.onlab.util;
17
Madan Jampani2bfa94c2015-04-11 05:03:49 -070018import static java.nio.file.Files.delete;
19import static java.nio.file.Files.walkFileTree;
20import static org.onlab.util.GroupedThreadFactory.groupedThreadFactory;
21import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080022
tom53efab52014-10-07 17:43:48 -070023import java.io.BufferedReader;
24import java.io.File;
Ray Milkey705d9bc2014-11-18 08:19:00 -080025import java.io.FileInputStream;
tom53efab52014-10-07 17:43:48 -070026import java.io.IOException;
Ray Milkey705d9bc2014-11-18 08:19:00 -080027import java.io.InputStreamReader;
28import java.nio.charset.StandardCharsets;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080029import java.nio.file.FileVisitResult;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080030import java.nio.file.Files;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080031import java.nio.file.Path;
32import java.nio.file.Paths;
33import java.nio.file.SimpleFileVisitor;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080034import java.nio.file.StandardCopyOption;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080035import java.nio.file.attribute.BasicFileAttributes;
tom53efab52014-10-07 17:43:48 -070036import java.util.ArrayList;
Brian O'Connore2eac102015-02-12 18:30:22 -080037import java.util.Collection;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070038import java.util.Dictionary;
tom53efab52014-10-07 17:43:48 -070039import java.util.List;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070040import java.util.concurrent.ExecutionException;
41import java.util.concurrent.Future;
tom5f38b3a2014-08-27 23:50:54 -070042import java.util.concurrent.ThreadFactory;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070043import java.util.concurrent.TimeUnit;
44import java.util.concurrent.TimeoutException;
tom5f38b3a2014-08-27 23:50:54 -070045
Madan Jampani2bfa94c2015-04-11 05:03:49 -070046import org.slf4j.Logger;
47
48import com.google.common.base.Strings;
49import com.google.common.primitives.UnsignedLongs;
50import com.google.common.util.concurrent.ThreadFactoryBuilder;
Ray Milkey705d9bc2014-11-18 08:19:00 -080051
Thomas Vachuskac13b90a2015-02-18 18:19:55 -080052/**
53 * Miscellaneous utility methods.
54 */
tom5f38b3a2014-08-27 23:50:54 -070055public abstract class Tools {
56
57 private Tools() {
58 }
59
Thomas Vachuska02aeb032015-01-06 22:36:30 -080060 private static final Logger log = getLogger(Tools.class);
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080061
tom5f38b3a2014-08-27 23:50:54 -070062 /**
63 * Returns a thread factory that produces threads named according to the
64 * supplied name pattern.
65 *
66 * @param pattern name pattern
67 * @return thread factory
68 */
69 public static ThreadFactory namedThreads(String pattern) {
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080070 return new ThreadFactoryBuilder()
71 .setNameFormat(pattern)
Thomas Vachuska480adad2015-03-06 10:27:09 -080072 .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e))
73 .build();
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080074 }
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080075
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080076 /**
77 * Returns a thread factory that produces threads named according to the
78 * supplied name pattern and from the specified thread-group. The thread
79 * group name is expected to be specified in slash-delimited format, e.g.
Thomas Vachuskac13b90a2015-02-18 18:19:55 -080080 * {@code onos/intent}. The thread names will be produced by converting
81 * the thread group name into dash-delimited format and pre-pended to the
82 * specified pattern.
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -080083 *
84 * @param groupName group name in slash-delimited format to indicate hierarchy
85 * @param pattern name pattern
86 * @return thread factory
87 */
88 public static ThreadFactory groupedThreads(String groupName, String pattern) {
89 return new ThreadFactoryBuilder()
90 .setThreadFactory(groupedThreadFactory(groupName))
Thomas Vachuskac13b90a2015-02-18 18:19:55 -080091 .setNameFormat(groupName.replace(GroupedThreadFactory.DELIMITER, "-") + "-" + pattern)
Thomas Vachuska480adad2015-03-06 10:27:09 -080092 .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e))
93 .build();
tom5f38b3a2014-08-27 23:50:54 -070094 }
95
tom782a7cf2014-09-11 23:58:38 -070096 /**
Yuta HIGUCHI06586272014-11-25 14:27:03 -080097 * Returns a thread factory that produces threads with MIN_PRIORITY.
98 *
99 * @param factory backing ThreadFactory
100 * @return thread factory
101 */
102 public static ThreadFactory minPriority(ThreadFactory factory) {
103 return new ThreadFactoryBuilder()
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800104 .setThreadFactory(factory)
105 .setPriority(Thread.MIN_PRIORITY)
106 .build();
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800107 }
108
109 /**
Brian O'Connore2eac102015-02-12 18:30:22 -0800110 * Returns true if the collection is null or is empty.
111 *
112 * @param collection collection to test
113 * @return true if null or empty; false otherwise
114 */
115 public static boolean isNullOrEmpty(Collection collection) {
116 return collection == null || collection.isEmpty();
117 }
118
119 /**
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700120 * Returns the specified item if that items is null; otherwise throws
121 * not found exception.
122 *
123 * @param item item to check
124 * @param message not found message
125 * @param <T> item type
126 * @return item if not null
127 * @throws org.onlab.util.ItemNotFoundException if item is null
128 */
129 public static <T> T nullIsNotFound(T item, String message) {
130 if (item == null) {
131 throw new ItemNotFoundException(message);
132 }
133 return item;
134 }
135
136 /**
tom782a7cf2014-09-11 23:58:38 -0700137 * Converts a string from hex to long.
138 *
139 * @param string hex number in string form; sans 0x
140 * @return long value
141 */
142 public static long fromHex(String string) {
143 return UnsignedLongs.parseUnsignedLong(string, 16);
144 }
145
146 /**
147 * Converts a long value to hex string; 16 wide and sans 0x.
148 *
149 * @param value long value
150 * @return hex string
151 */
152 public static String toHex(long value) {
153 return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
154 }
155
156 /**
157 * Converts a long value to hex string; 16 wide and sans 0x.
158 *
159 * @param value long value
160 * @param width string width; zero padded
161 * @return hex string
162 */
163 public static String toHex(long value, int width) {
164 return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
165 }
tomf110fff2014-09-26 00:38:18 -0700166
167 /**
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700168 * Get property as a string value.
169 *
170 * @param properties properties to be looked up
171 * @param propertyName the name of the property to look up
172 * @return value when the propertyName is defined or return null
173 */
174 public static String get(Dictionary<?, ?> properties, String propertyName) {
175 Object v = properties.get(propertyName);
176 String s = (v instanceof String) ? (String) v :
177 v != null ? v.toString() : null;
178 return Strings.isNullOrEmpty(s) ? null : s.trim();
179 }
180
181 /**
tomf110fff2014-09-26 00:38:18 -0700182 * Suspends the current thread for a specified number of millis.
183 *
184 * @param ms number of millis
185 */
186 public static void delay(int ms) {
187 try {
188 Thread.sleep(ms);
189 } catch (InterruptedException e) {
190 throw new RuntimeException("Interrupted", e);
191 }
192 }
193
tom53efab52014-10-07 17:43:48 -0700194 /**
195 * Slurps the contents of a file into a list of strings, one per line.
196 *
197 * @param path file path
198 * @return file contents
199 */
200 public static List<String> slurp(File path) {
Ray Milkey705d9bc2014-11-18 08:19:00 -0800201 try {
202 BufferedReader br = new BufferedReader(
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800203 new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
Ray Milkey705d9bc2014-11-18 08:19:00 -0800204
tom53efab52014-10-07 17:43:48 -0700205 List<String> lines = new ArrayList<>();
206 String line;
207 while ((line = br.readLine()) != null) {
208 lines.add(line);
209 }
210 return lines;
211
212 } catch (IOException e) {
213 return null;
214 }
215 }
216
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800217
218 /**
219 * Purges the specified directory path.&nbsp;Use with great caution since
220 * no attempt is made to check for symbolic links, which could result in
221 * deletion of unintended files.
222 *
223 * @param path directory to be removed
224 * @throws java.io.IOException if unable to remove contents
225 */
226 public static void removeDirectory(String path) throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800227 DirectoryDeleter visitor = new DirectoryDeleter();
228 walkFileTree(Paths.get(path), visitor);
229 if (visitor.exception != null) {
230 throw visitor.exception;
231 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800232 }
233
234 /**
235 * Purges the specified directory path.&nbsp;Use with great caution since
236 * no attempt is made to check for symbolic links, which could result in
237 * deletion of unintended files.
238 *
239 * @param dir directory to be removed
240 * @throws java.io.IOException if unable to remove contents
241 */
242 public static void removeDirectory(File dir) throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800243 DirectoryDeleter visitor = new DirectoryDeleter();
244 walkFileTree(Paths.get(dir.getAbsolutePath()), visitor);
245 if (visitor.exception != null) {
246 throw visitor.exception;
247 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800248 }
249
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800250 // Auxiliary path visitor for recursive directory structure removal.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800251 private static class DirectoryDeleter extends SimpleFileVisitor<Path> {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800252
253 private IOException exception;
254
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800255 @Override
256 public FileVisitResult visitFile(Path file, BasicFileAttributes attributes)
257 throws IOException {
258 if (attributes.isRegularFile()) {
259 delete(file);
260 }
261 return FileVisitResult.CONTINUE;
262 }
263
264 @Override
265 public FileVisitResult postVisitDirectory(Path directory, IOException ioe)
266 throws IOException {
267 delete(directory);
268 return FileVisitResult.CONTINUE;
269 }
270
271 @Override
272 public FileVisitResult visitFileFailed(Path file, IOException ioe)
273 throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800274 this.exception = ioe;
275 return FileVisitResult.TERMINATE;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800276 }
277 }
278
Madan Jampani30a57f82015-03-02 12:19:41 -0800279 /**
280 * Returns a human friendly time ago string for a specified system time.
Thomas Vachuska480adad2015-03-06 10:27:09 -0800281 *
Madan Jampani30a57f82015-03-02 12:19:41 -0800282 * @param unixTime system time in millis
283 * @return human friendly time ago
284 */
285 public static String timeAgo(long unixTime) {
286 long deltaMillis = System.currentTimeMillis() - unixTime;
287 long secondsSince = (long) (deltaMillis / 1000.0);
288 long minsSince = (long) (deltaMillis / (1000.0 * 60));
289 long hoursSince = (long) (deltaMillis / (1000.0 * 60 * 60));
290 long daysSince = (long) (deltaMillis / (1000.0 * 60 * 60 * 24));
291 if (daysSince > 0) {
292 return String.format("%dd ago", daysSince);
293 } else if (hoursSince > 0) {
294 return String.format("%dh ago", hoursSince);
295 } else if (minsSince > 0) {
296 return String.format("%dm ago", minsSince);
297 } else if (secondsSince > 0) {
298 return String.format("%ds ago", secondsSince);
299 } else {
300 return "just now";
301 }
302 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800303
304 /**
305 * Copies the specified directory path.&nbsp;Use with great caution since
306 * no attempt is made to check for symbolic links, which could result in
307 * copy of unintended files.
308 *
309 * @param src directory to be copied
310 * @param dst destination directory to be removed
311 * @throws java.io.IOException if unable to remove contents
312 */
313 public static void copyDirectory(String src, String dst) throws IOException {
314 walkFileTree(Paths.get(src), new DirectoryCopier(src, dst));
315 }
316
317 /**
318 * Copies the specified directory path.&nbsp;Use with great caution since
319 * no attempt is made to check for symbolic links, which could result in
320 * copy of unintended files.
321 *
322 * @param src directory to be copied
323 * @param dst destination directory to be removed
324 * @throws java.io.IOException if unable to remove contents
325 */
326 public static void copyDirectory(File src, File dst) throws IOException {
327 walkFileTree(Paths.get(src.getAbsolutePath()),
328 new DirectoryCopier(src.getAbsolutePath(),
329 dst.getAbsolutePath()));
330 }
331
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700332 /**
333 * Returns the future value when complete or if future
334 * completes exceptionally returns the defaultValue.
335 * @param future future
336 * @param defaultValue default value
337 * @param <T> future value type
338 * @return future value when complete or if future
339 * completes exceptionally returns the defaultValue.
340 */
341 public static <T> T futureGetOrElse(Future<T> future, T defaultValue) {
342 try {
343 return future.get();
344 } catch (InterruptedException e) {
345 Thread.currentThread().interrupt();
346 return defaultValue;
347 } catch (ExecutionException e) {
348 return defaultValue;
349 }
350 }
351
352 /**
353 * Returns the future value when complete or if future
354 * completes exceptionally returns the defaultValue.
355 * @param future future
356 * @param timeout time to wait for successful completion
357 * @param timeUnit time unit
358 * @param defaultValue default value
359 * @param <T> future value type
360 * @return future value when complete or if future
361 * completes exceptionally returns the defaultValue.
362 */
363 public static <T> T futureGetOrElse(Future<T> future,
364 long timeout,
365 TimeUnit timeUnit,
366 T defaultValue) {
367 try {
368 return future.get(timeout, timeUnit);
369 } catch (InterruptedException e) {
370 Thread.currentThread().interrupt();
371 return defaultValue;
372 } catch (ExecutionException | TimeoutException e) {
373 return defaultValue;
374 }
375 }
376
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800377 // Auxiliary path visitor for recursive directory structure copying.
378 private static class DirectoryCopier extends SimpleFileVisitor<Path> {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800379 private Path src;
380 private Path dst;
381 private StandardCopyOption copyOption = StandardCopyOption.REPLACE_EXISTING;
382
383 DirectoryCopier(String src, String dst) {
384 this.src = Paths.get(src);
385 this.dst = Paths.get(dst);
386 }
387
388 @Override
389 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
390 Path targetPath = dst.resolve(src.relativize(dir));
391 if (!Files.exists(targetPath)) {
392 Files.createDirectory(targetPath);
393 }
394 return FileVisitResult.CONTINUE;
395 }
396
397 @Override
398 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
399 Files.copy(file, dst.resolve(src.relativize(file)), copyOption);
400 return FileVisitResult.CONTINUE;
401 }
402 }
403
tom5f38b3a2014-08-27 23:50:54 -0700404}