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