2 * Copyright © 2018 IBM Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 package org.onap.ccsdk.apps.controllerblueprints.service;
\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
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
39 * CbaFileManagementService.java Purpose: Provide Service processing CBA file management
\r
41 * @author Steve Siani
\r
45 public class CbaFileManagementService {
\r
46 private static EELFLogger log = EELFManager.getInstance().getLogger(CbaFileManagementService.class);
\r
48 @Value("${controllerblueprints.loadCbaExtension}")
\r
49 private String cbaExtension;
\r
51 private static final String CBA_FILE_NAME_PATTERN = "CBA_{0}_{1}";
\r
55 * cleanupSavedCBA: This method cleanup the Zip file and the unzip directory that was added.
\r
57 * @param zipFileName zipFileName
\r
58 * @param cbaFileLocation cbaFileLocation
\r
60 * @throws BluePrintException BluePrintException
\r
62 public void cleanupSavedCBA(String zipFileName, Path cbaFileLocation) throws BluePrintException {
\r
64 String fileNameWithoutExtension = BluePrintFileUtils.Companion.stripFileExtension(zipFileName);
\r
66 //Delete the Zip file from the repository
\r
67 FileSystemUtils.deleteRecursively(BluePrintFileUtils.Companion.getBluePrintFile(zipFileName,cbaFileLocation));
\r
69 //Delete the CBA directory from the repository
\r
70 FileSystemUtils.deleteRecursively(BluePrintFileUtils.Companion.getBluePrintFile(fileNameWithoutExtension,cbaFileLocation));
\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
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
81 public Mono<String> saveCBAFile(FilePart filePart, Path targetDirectory) throws BluePrintException, IOException {
\r
83 // Normalize file name
\r
84 final String fileName = StringUtils.cleanPath(filePart.filename());
\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
91 // Change file name to match a pattern
\r
92 String changedFileName = BluePrintFileUtils.Companion.getCBAGeneratedFileName(fileName, this.CBA_FILE_NAME_PATTERN);
\r
94 // Copy file to the target location (Replacing existing file with the same name)
\r
95 Path targetLocation = targetDirectory.resolve(changedFileName);
\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
101 file.createNewFile();
\r
103 return filePart.transferTo(file).thenReturn(changedFileName);
\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
113 public String decompressCBAFile(final String zipFileName, Path cbaFileLocation) throws BluePrintException {
\r
115 File file = BluePrintFileUtils.Companion.getBluePrintFile(zipFileName, cbaFileLocation);
\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
121 } catch (BluePrintProcessorException | IOException ex) {
\r
122 throw new BluePrintException(" Fail to decompress " + zipFileName, ex);
\r