2011172dc0e4fa8c0556ad12f2c23ca67be2d828
[ccsdk/cds.git] /
1 /*\r
2  * Copyright © 2018 IBM Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  *\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *     http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.apps.controllerblueprints.service;\r
19 \r
20 import com.att.eelf.configuration.EELFLogger;\r
21 import com.att.eelf.configuration.EELFManager;\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException;\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintArchiveUtils;\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils;\r
26 import org.springframework.beans.factory.annotation.Value;\r
27 import org.springframework.http.codec.multipart.FilePart;\r
28 import org.springframework.stereotype.Service;\r
29 import org.springframework.util.FileSystemUtils;\r
30 import org.springframework.util.StringUtils;\r
31 import reactor.core.publisher.Mono;\r
32 \r
33 import java.io.File;\r
34 import java.io.IOException;\r
35 import java.nio.file.Files;\r
36 import java.nio.file.Path;\r
37 \r
38 /**\r
39  * CbaFileManagementService.java Purpose: Provide Service processing CBA file management\r
40  *\r
41  * @author Steve Siani\r
42  * @version 1.0\r
43  */\r
44 @Service\r
45 public class CbaFileManagementService {\r
46     private static EELFLogger log = EELFManager.getInstance().getLogger(CbaFileManagementService.class);\r
47 \r
48     @Value("${controllerblueprints.loadCbaExtension}")\r
49     private String cbaExtension;\r
50 \r
51     private static final String CBA_FILE_NAME_PATTERN = "CBA_{0}_{1}";\r
52 \r
53 \r
54     /**\r
55      * cleanupSavedCBA: This method cleanup the Zip file and the unzip directory that was added.\r
56      *\r
57      * @param zipFileName zipFileName\r
58      * @param cbaFileLocation cbaFileLocation\r
59      * @return\r
60      * @throws BluePrintException BluePrintException\r
61      */\r
62     public void cleanupSavedCBA(String zipFileName, Path cbaFileLocation) throws BluePrintException {\r
63 \r
64         String fileNameWithoutExtension = BluePrintFileUtils.Companion.stripFileExtension(zipFileName);\r
65 \r
66         //Delete the Zip file from the repository\r
67         FileSystemUtils.deleteRecursively(BluePrintFileUtils.Companion.getBluePrintFile(zipFileName,cbaFileLocation));\r
68 \r
69         //Delete the CBA directory from the repository\r
70         FileSystemUtils.deleteRecursively(BluePrintFileUtils.Companion.getBluePrintFile(fileNameWithoutExtension,cbaFileLocation));\r
71     }\r
72 \r
73     /**\r
74      * This is a saveCBAFile method\r
75      * take a {@link FilePart}, transfer it to disk using a Flux of FilePart and return a {@link Mono} representing the CBA file name\r
76      *\r
77      * @param (filePart, targetDirectory) - the request part containing the file to be saved and the default directory where to save\r
78      * @return a {@link Mono} String representing the result of the operation\r
79      * @throws (BluePrintException, IOException) BluePrintException, IOException\r
80      */\r
81     public Mono<String> saveCBAFile(FilePart filePart, Path targetDirectory) throws BluePrintException, IOException {\r
82 \r
83         // Normalize file name\r
84         final String fileName = StringUtils.cleanPath(filePart.filename());\r
85 \r
86         // Check if the file's extension is "CBA"\r
87         if(!StringUtils.getFilenameExtension(fileName).equals(cbaExtension)) {\r
88             throw new BluePrintException("Invalid file extension required " + cbaExtension);\r
89         }\r
90 \r
91         // Change file name to match a pattern\r
92         String changedFileName = BluePrintFileUtils.Companion.getCBAGeneratedFileName(fileName, this.CBA_FILE_NAME_PATTERN);\r
93 \r
94         // Copy file to the target location (Replacing existing file with the same name)\r
95         Path targetLocation = targetDirectory.resolve(changedFileName);\r
96 \r
97         // if a file with the same name already exists in a repository, delete and recreate it\r
98         File file = new File(targetLocation.toString());\r
99         if (file.exists())\r
100             file.delete();\r
101         file.createNewFile();\r
102 \r
103         return filePart.transferTo(file).thenReturn(changedFileName);\r
104     }\r
105 \r
106     /**\r
107      * Decompress the file into the cbaFileLocation parameter\r
108      * @param zipFileName name of the zipped file\r
109      * @param cbaFileLocation path in which the zipped file will get decompressed\r
110      * @return String the path in which the file is decompressed\r
111      * @throws BluePrintException Exception in the process\r
112      */\r
113     public String decompressCBAFile(final String zipFileName, Path cbaFileLocation) throws BluePrintException {\r
114 \r
115         File file = BluePrintFileUtils.Companion.getBluePrintFile(zipFileName, cbaFileLocation);\r
116         try {\r
117             Path directoryPath = Files.createDirectories(cbaFileLocation.resolve(BluePrintFileUtils.Companion.stripFileExtension(zipFileName)));\r
118             BluePrintArchiveUtils.Companion.deCompress(file, directoryPath.toString());\r
119             return directoryPath.toString();\r
120 \r
121         } catch (BluePrintProcessorException | IOException ex) {\r
122             throw new BluePrintException(" Fail to decompress " + zipFileName, ex);\r
123         }\r
124 \r
125     }\r
126 \r
127 \r
128 }\r