Optimizing Imports and Formatting code
[ccsdk/cds.git] / ms / sdclistener / application / src / main / java / org / onap / ccsdk / cds / sdclistener / util / FileUtil.java
1 /*
2  * Copyright © 2019 Bell Canada
3  *
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 package org.onap.ccsdk.cds.sdclistener.util;
17
18 import org.apache.commons.io.FileUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30
31 import static java.nio.file.Files.walk;
32
33 public final class FileUtil {
34     private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
35
36     private FileUtil() {
37
38     }
39
40     /**
41      * Delete the file.
42      *
43      * @param file - Given file
44      * @param path - Given path
45      */
46     public static void deleteFile(File file, String path) {
47         boolean value = file.delete();
48         if (!value) {
49             LOGGER.error("Failed to delete the file {} at the location {}", file, path);
50         }
51     }
52
53     /**
54      * Extract files from the given path
55      *
56      * @param path where files reside.
57      * @return list of files.
58      */
59     public static List<File> getFilesFromDisk(Path path) {
60
61         try (Stream<Path> fileTree = walk(path)) {
62             // Get the list of files from the path
63             return fileTree.filter(Files::isRegularFile)
64                            .map(Path::toFile)
65                            .collect(Collectors.toList());
66         } catch (IOException e) {
67             LOGGER.error("Failed to find the file due to", e);
68         }
69         return new ArrayList<>();
70     }
71 }