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.jetbrains.annotations.NotNull;
\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.service.domain.CbaContent;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.service.model.BlueprintModelResponse;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.service.model.ItemCbaResponse;
\r
29 import org.onap.ccsdk.apps.controllerblueprints.service.utils.CbaStateEnum;
\r
30 import org.springframework.beans.factory.annotation.Autowired;
\r
31 import org.springframework.beans.factory.annotation.Value;
\r
32 import org.springframework.boot.context.event.ApplicationReadyEvent;
\r
33 import org.springframework.context.event.EventListener;
\r
34 import org.springframework.core.io.ByteArrayResource;
\r
35 import org.springframework.core.io.Resource;
\r
36 import org.springframework.http.HttpHeaders;
\r
37 import org.springframework.http.MediaType;
\r
38 import org.springframework.http.ResponseEntity;
\r
39 import org.springframework.http.codec.multipart.FilePart;
\r
40 import org.springframework.stereotype.Service;
\r
41 import reactor.core.publisher.Mono;
\r
42 import java.io.IOException;
\r
43 import java.nio.file.Path;
\r
44 import java.util.ArrayList;
\r
45 import java.util.List;
\r
46 import java.util.Optional;
\r
49 * CbaService.java Purpose: Provide Service Template Service processing CbaService
\r
51 * @author Steve Siani
\r
56 public class CbaService {
\r
58 private static EELFLogger log = EELFManager.getInstance().getLogger(CbaService.class);
\r
60 @Value("${controllerblueprints.blueprintArchivePath}")
\r
61 private String cbaArchivePath;
\r
62 private Path cbaLocation;
\r
65 private CbaFileManagementService cbaFileManagementService;
\r
68 private CbaToDatabaseService cbaToDatabaseService;
\r
72 * This method would be used by SpringBoot to initialize the cba location
\r
74 @EventListener(ApplicationReadyEvent.class)
\r
75 private void initCbaService() {
\r
76 this.cbaLocation = BluePrintFileUtils.Companion.getCbaStorageDirectory(cbaArchivePath);
\r
77 log.info("CBA service Initiated...");
\r
81 * This is a uploadCBAFile method
\r
82 * take a {@link FilePart}, transfer it to disk using WebFlux and return a {@link Mono} representing the result
\r
84 * @param filePart - the request part containing the file to be saved
\r
85 * @return a {@link Mono< BlueprintModelResponse >} representing the result of the operation
\r
87 public Mono<BlueprintModelResponse> uploadCBAFile(FilePart filePart) {
\r
90 return this.cbaFileManagementService.saveCBAFile(filePart, cbaLocation).map(fileName -> {
\r
91 ConfigModel configModel;
\r
92 BlueprintModelResponse blueprintModelResponse = null;
\r
95 String cbaDirectory = this.cbaFileManagementService.decompressCBAFile(fileName, cbaLocation);
\r
96 configModel = this.cbaToDatabaseService.storeBluePrints(cbaDirectory, fileName, cbaLocation.resolve(fileName));
\r
97 blueprintModelResponse = new BlueprintModelResponse(configModel.getId(), configModel.getArtifactName(), configModel.getArtifactVersion(), configModel.getArtifactDescription(), configModel.getConfigModelCBA().getCbaUUID());
\r
98 } catch (BluePrintException be) {
\r
99 Mono.error(new BluePrintException("Error loading CBA in database.", be));
\r
102 this.cbaFileManagementService.cleanupSavedCBA(fileName, cbaLocation);
\r
103 } catch (BluePrintException be) {
\r
104 Mono.error(new BluePrintException("Error while cleaning up.", be));
\r
107 return blueprintModelResponse;
\r
109 } catch (IOException | BluePrintException e) {
\r
110 return Mono.error(new BluePrintException("Error uploading the CBA file in channel.", e));
\r
115 * This is a deleteCba method
\r
118 * @throws BluePrintException BluePrintException
\r
120 public void deleteCBA(@NotNull Long id) throws BluePrintException {
\r
121 this.cbaToDatabaseService.deleteCBA(id);
\r
125 * This is a downloadCBAFile method to find the target file to download and return a file ressource using MONO
\r
128 * @return ResponseEntity<Resource>
\r
130 public ResponseEntity<Resource> downloadCBAFile(@NotNull String id) {
\r
131 Optional<CbaContent> optionalContent = this.cbaToDatabaseService.findByUUID(id);
\r
133 CbaContent cbaContent = optionalContent.get();
\r
135 return ResponseEntity.ok()
\r
136 .contentType(MediaType.parseMediaType("text/plain"))
\r
137 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + cbaContent.getCbaName() + "\"")
\r
138 .body(new ByteArrayResource(cbaContent.getCbaFile()));
\r
142 * This is a findCBAByID method to find a CBA By the UUID
\r
145 * @return ItemCbaResponse
\r
147 public ItemCbaResponse findCBAByID(@NotNull String id) {
\r
148 ItemCbaResponse response = new ItemCbaResponse();
\r
149 Optional<CbaContent> optionalContent = this.cbaToDatabaseService.findByUUID(id);
\r
151 CbaContent cbaContent = optionalContent.get();
\r
152 response.setName(cbaContent.getCbaName());
\r
153 response.setState(cbaContent.getCbaState());
\r
154 response.setId(cbaContent.getCbaUUID());
\r
155 response.setVersion(cbaContent.getCbaVersion());
\r
156 response.setDescription(cbaContent.getCbaDescription());
\r
161 * This is a findAllCBA method to retrieve all the CBAs in Database
\r
163 * @return List<ItemCbaResponse> list with the controller blueprint archives
\r
165 public List<ItemCbaResponse> findAllCBA() {
\r
166 List<ItemCbaResponse> responseList = new ArrayList<>();
\r
167 List<CbaContent> cbaContents = this.cbaToDatabaseService.listCBAFiles();
\r
169 for(CbaContent content: cbaContents){
\r
170 ItemCbaResponse response = new ItemCbaResponse();
\r
171 response.setName(content.getCbaName());
\r
172 response.setState(content.getCbaState());
\r
173 response.setId(content.getCbaUUID());
\r
174 response.setVersion(content.getCbaVersion());
\r
175 response.setDescription(content.getCbaDescription());
\r
177 responseList.add(response);
\r
179 return responseList;
\r
183 * This is a findCBAByNameAndVersion method to find a CBA by Name and version
\r
185 * @param (name, version)
\r
187 * @throws BluePrintException BluePrintException
\r
189 public ItemCbaResponse findCBAByNameAndVersion(@NotNull String name, @NotNull String version) throws BluePrintException {
\r