2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2019 Bell Canada.
\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 java.io.IOException;
21 import java.nio.file.Path;
22 import java.util.List;
23 import java.util.Optional;
24 import org.jetbrains.annotations.NotNull;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode;
\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService;
\r
30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils;
\r
31 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel;
\r
32 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch;
\r
33 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelContentRepository;
\r
34 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelRepository;
\r
35 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelSearchRepository;
\r
36 import org.onap.ccsdk.apps.controllerblueprints.service.utils.BluePrintEnhancerUtils;
\r
37 import org.springframework.beans.factory.annotation.Autowired;
\r
38 import org.springframework.core.io.ByteArrayResource;
\r
39 import org.springframework.core.io.Resource;
\r
40 import org.springframework.http.HttpHeaders;
\r
41 import org.springframework.http.MediaType;
\r
42 import org.springframework.http.ResponseEntity;
\r
43 import org.springframework.http.codec.multipart.FilePart;
\r
44 import org.springframework.stereotype.Service;
\r
45 import org.springframework.transaction.annotation.Transactional;
\r
46 import reactor.core.publisher.Mono;
\r
49 * BlueprintModelService.java Purpose: Provide Service Template Service processing BlueprintModelService
\r
51 * @author Brinda Santh
\r
56 public class BlueprintModelService {
\r
59 private BluePrintLoadConfiguration bluePrintLoadConfiguration;
\r
62 private BluePrintCatalogService bluePrintCatalogService;
\r
65 private ControllerBlueprintModelSearchRepository blueprintModelSearchRepository;
\r
68 private ControllerBlueprintModelRepository blueprintModelRepository;
\r
71 private ControllerBlueprintModelContentRepository blueprintModelContentRepository;
\r
73 private static final String BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%s) from repo";
\r
74 private static final String BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%s)" +
\r
75 " and version(%s) from repo";
78 * This is a saveBlueprintModel method
\r
80 * @param filePart filePart
\r
81 * @return Mono<BlueprintModelSearch>
\r
82 * @throws BluePrintException BluePrintException
\r
84 public Mono<BlueprintModelSearch> saveBlueprintModel(FilePart filePart) throws BluePrintException {
\r
86 Path cbaLocation = BluePrintFileUtils.Companion
\r
87 .getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath);
88 return BluePrintEnhancerUtils.Companion.saveCBAFile(filePart, cbaLocation).map(fileName -> {
\r
89 String blueprintId = null;
91 blueprintId = bluePrintCatalogService
92 .saveToDatabase(cbaLocation.toFile(), false);
93 } catch (BluePrintException e) {
94 // FIXME handle expection
96 return blueprintModelSearchRepository.findById(blueprintId).get();
\r
98 } catch (IOException e) {
\r
99 throw new BluePrintException(ErrorCode.IO_FILE_INTERRUPT.getValue(),
\r
100 String.format("I/O Error while uploading the CBA file: %s", e.getMessage()), e);
105 * This is a publishBlueprintModel method to change the status published to YES
\r
108 * @return BlueprintModelSearch
\r
109 * @throws BluePrintException BluePrintException
\r
111 public BlueprintModelSearch publishBlueprintModel(String id) throws BluePrintException {
\r
112 BlueprintModelSearch blueprintModelSearch;
\r
113 Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository.findById(id);
\r
114 if (dbBlueprintModel.isPresent()) {
\r
115 blueprintModelSearch = dbBlueprintModel.get();
\r
117 String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
\r
118 throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
\r
120 blueprintModelSearch.setPublished(ApplicationConstants.ACTIVE_Y);
\r
121 return blueprintModelSearchRepository.saveAndFlush(blueprintModelSearch);
\r
125 * This is a searchBlueprintModels method
\r
128 * @return List<BlueprintModelSearch>
\r
130 public List<BlueprintModelSearch> searchBlueprintModels(String tags) {
\r
131 return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags);
\r
135 * This is a getBlueprintModelSearchByNameAndVersion method
\r
138 * @param version version
\r
139 * @return BlueprintModelSearch
\r
140 * @throws BluePrintException BluePrintException
\r
142 public BlueprintModelSearch getBlueprintModelSearchByNameAndVersion(@NotNull String name, @NotNull String version)
\r
143 throws BluePrintException {
144 BlueprintModelSearch blueprintModelSearch;
\r
145 Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository
\r
146 .findByArtifactNameAndArtifactVersion(name, version);
147 if (dbBlueprintModel.isPresent()) {
\r
148 blueprintModelSearch = dbBlueprintModel.get();
\r
150 throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(),
\r
151 String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));
153 return blueprintModelSearch;
\r
157 * This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version
\r
160 * @param version version
\r
161 * @return ResponseEntity<Resource>
\r
162 * @throws BluePrintException BluePrintException
\r
164 public ResponseEntity<Resource> downloadBlueprintModelFileByNameAndVersion(@NotNull String name,
165 @NotNull String version)
166 throws BluePrintException {
167 BlueprintModel blueprintModel;
\r
169 blueprintModel = getBlueprintModelByNameAndVersion(name, version);
\r
170 } catch (BluePrintException e) {
\r
171 throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +
\r
172 "downloading the CBA file: %s", e.getMessage()), e);
174 String fileName = blueprintModel.getId() + ".zip";
\r
175 byte[] file = blueprintModel.getBlueprintModelContent().getContent();
\r
176 return prepareResourceEntity(fileName, file);
\r
180 * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource
\r
182 * @return ResponseEntity<Resource>
\r
183 * @throws BluePrintException BluePrintException
\r
185 public ResponseEntity<Resource> downloadBlueprintModelFile(@NotNull String id) throws BluePrintException {
\r
186 BlueprintModel blueprintModel;
\r
188 blueprintModel = getBlueprintModel(id);
\r
189 } catch (BluePrintException e) {
\r
190 throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +
\r
191 "downloading the CBA file: %s", e.getMessage()), e);
193 String fileName = blueprintModel.getId() + ".zip";
\r
194 byte[] file = blueprintModel.getBlueprintModelContent().getContent();
\r
195 return prepareResourceEntity(fileName, file);
\r
199 * @return ResponseEntity<Resource>
\r
201 private ResponseEntity<Resource> prepareResourceEntity(String fileName, byte[] file) {
\r
202 return ResponseEntity.ok()
\r
203 .contentType(MediaType.parseMediaType("text/plain"))
204 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
205 .body(new ByteArrayResource(file));
209 * This is a getBlueprintModel method
\r
212 * @return BlueprintModel
\r
213 * @throws BluePrintException BluePrintException
\r
215 private BlueprintModel getBlueprintModel(@NotNull String id) throws BluePrintException {
\r
216 BlueprintModel blueprintModel;
\r
217 Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findById(id);
\r
218 if (dbBlueprintModel.isPresent()) {
\r
219 blueprintModel = dbBlueprintModel.get();
\r
221 String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
\r
222 throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
\r
224 return blueprintModel;
\r
228 * This is a getBlueprintModelByNameAndVersion method
\r
231 * @param version version
\r
232 * @return BlueprintModel
\r
233 * @throws BluePrintException BluePrintException
\r
235 private BlueprintModel getBlueprintModelByNameAndVersion(@NotNull String name, @NotNull String version)
\r
236 throws BluePrintException {
237 BlueprintModel blueprintModel = blueprintModelRepository
238 .findByArtifactNameAndArtifactVersion(name, version);
239 if (blueprintModel != null) {
240 return blueprintModel;
242 String msg = String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version);
\r
243 throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
\r
248 * This is a getBlueprintModelSearch method
\r
251 * @return BlueprintModelSearch
\r
252 * @throws BluePrintException BluePrintException
\r
254 public BlueprintModelSearch getBlueprintModelSearch(@NotNull String id) throws BluePrintException {
\r
255 BlueprintModelSearch blueprintModelSearch;
\r
256 Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository.findById(id);
\r
257 if (dbBlueprintModel.isPresent()) {
\r
258 blueprintModelSearch = dbBlueprintModel.get();
\r
260 String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
\r
261 throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
\r
264 return blueprintModelSearch;
\r
268 * This is a deleteBlueprintModel method
\r
271 * @throws BluePrintException BluePrintException
\r
274 public void deleteBlueprintModel(@NotNull String id) throws BluePrintException {
\r
275 Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findById(id);
\r
276 if (dbBlueprintModel.isPresent()) {
\r
277 blueprintModelContentRepository.deleteByBlueprintModel(dbBlueprintModel.get());
\r
278 blueprintModelRepository.delete(dbBlueprintModel.get());
\r
280 String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
\r
281 throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
\r
286 * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database
\r
288 * @return List<BlueprintModelSearch> list of the controller blueprint archives
\r
290 public List<BlueprintModelSearch> getAllBlueprintModel() {
\r
291 return blueprintModelSearchRepository.findAll();
\r