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 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.interfaces.BluePrintCatalogService;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.service.load.BluePrintLoadConfiguration;
\r
29 import org.onap.ccsdk.apps.controllerblueprints.service.repository.BlueprintModelContentRepository;
\r
30 import org.onap.ccsdk.apps.controllerblueprints.service.repository.BlueprintModelRepository;
\r
31 import org.onap.ccsdk.apps.controllerblueprints.service.repository.BlueprintModelSearchRepository;
\r
32 import org.onap.ccsdk.apps.controllerblueprints.service.utils.BluePrintEnhancerUtils;
\r
33 import org.springframework.beans.factory.annotation.Autowired;
\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 org.springframework.transaction.annotation.Transactional;
\r
42 import reactor.core.publisher.Mono;
\r
44 import java.io.IOException;
\r
45 import java.nio.file.Path;
\r
46 import java.util.List;
\r
47 import java.util.Optional;
\r
50 * BlueprintModelService.java Purpose: Provide Service Template Service processing BlueprintModelService
\r
52 * @author Brinda Santh
\r
57 public class BlueprintModelService {
\r
59 private static EELFLogger log = EELFManager.getInstance().getLogger(BlueprintModelService.class);
\r
62 private BluePrintLoadConfiguration bluePrintLoadConfiguration;
\r
65 private BluePrintCatalogService bluePrintCatalogService;
\r
68 private BlueprintModelSearchRepository blueprintModelSearchRepository;
\r
71 private BlueprintModelRepository blueprintModelRepository;
\r
74 private BlueprintModelContentRepository blueprintModelContentRepository;
\r
76 private static final String BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%d) from repo";
\r
77 private static final String BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%d)" +
\r
78 " and version(%d) from repo";
\r
81 * This is a saveBlueprintModel method
\r
83 * @param filePart filePart
\r
84 * @return Mono<BlueprintModelSearch>
\r
85 * @throws BluePrintException BluePrintException
\r
87 public Mono<BlueprintModelSearch> saveBlueprintModel(FilePart filePart) throws BluePrintException {
\r
89 Path cbaLocation = BluePrintFileUtils.Companion.getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath);
\r
90 return BluePrintEnhancerUtils.Companion.saveCBAFile(filePart, cbaLocation).map(fileName -> {
\r
91 String blueprintId = bluePrintCatalogService.uploadToDataBase(cbaLocation.resolve(fileName).toString(), false);
\r
92 return blueprintModelSearchRepository.findById(blueprintId).get();
\r
95 } catch (IOException | BluePrintException e) {
\r
96 return Mono.error(new BluePrintException("Error uploading the CBA file in channel.", e));
\r
101 * This is a publishBlueprintModel method
\r
104 * @return BlueprintModelSearch
\r
105 * @throws BluePrintException BluePrintException
\r
107 public BlueprintModelSearch publishBlueprintModel(String id) throws BluePrintException {
\r
108 // TODO Implement publish Functionality
\r
113 * This is a searchBlueprintModels method
\r
116 * @return List<BlueprintModelSearch>
\r
118 public List<BlueprintModelSearch> searchBlueprintModels(String tags) {
\r
119 return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags);
\r
123 * This is a getBlueprintModelByNameAndVersion method
\r
126 * @param version version
\r
127 * @return BlueprintModelSearch
\r
129 public BlueprintModelSearch getBlueprintModelByNameAndVersion(@NotNull String name, @NotNull String version) throws BluePrintException {
\r
130 BlueprintModelSearch blueprintModelSearch;
\r
131 Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository
\r
132 .findByArtifactNameAndArtifactVersion(name, version);
\r
133 if (dbBlueprintModel.isPresent()) {
\r
134 blueprintModelSearch = dbBlueprintModel.get();
\r
136 throw new BluePrintException(String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));
\r
139 return blueprintModelSearch;
\r
143 * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource using MONO
\r
146 * @return ResponseEntity<Resource>
\r
148 public ResponseEntity<Resource> downloadBlueprintModelFile(@NotNull String id) throws BluePrintException {
\r
149 BlueprintModel blueprintModel;
\r
151 blueprintModel = getBlueprintModel(id);
\r
152 } catch (BluePrintException e) {
\r
153 throw new BluePrintException("Error uploading the CBA file in channel.", e);
\r
155 String fileName = blueprintModel.getId() + ".zip";
\r
156 byte[] file = blueprintModel.getBlueprintModelContent().getContent();
\r
157 return ResponseEntity.ok()
\r
158 .contentType(MediaType.parseMediaType("text/plain"))
\r
159 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
\r
160 .body(new ByteArrayResource(file));
\r
164 * This is a getBlueprintModel method
\r
167 * @return BlueprintModel
\r
168 * @throws BluePrintException BluePrintException
\r
170 private BlueprintModel getBlueprintModel(@NotNull String id) throws BluePrintException {
\r
171 BlueprintModel blueprintModel;
\r
172 Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findById(id);
\r
173 if (dbBlueprintModel.isPresent()) {
\r
174 blueprintModel = dbBlueprintModel.get();
\r
176 throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));
\r
179 return blueprintModel;
\r
183 * This is a getBlueprintModelSearch method
\r
186 * @return BlueprintModelSearch
\r
187 * @throws BluePrintException BluePrintException
\r
189 public BlueprintModelSearch getBlueprintModelSearch(@NotNull String id) throws BluePrintException {
\r
190 BlueprintModelSearch blueprintModelSearch;
\r
191 Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository.findById(id);
\r
192 if (dbBlueprintModel.isPresent()) {
\r
193 blueprintModelSearch = dbBlueprintModel.get();
\r
195 throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));
\r
198 return blueprintModelSearch;
\r
202 * This is a deleteBlueprintModel method
\r
205 * @throws BluePrintException BluePrintException
\r
208 public void deleteBlueprintModel(@NotNull String id) throws BluePrintException {
\r
209 Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findById(id);
\r
210 if (dbBlueprintModel.isPresent()) {
\r
211 blueprintModelContentRepository.deleteByBlueprintModel(dbBlueprintModel.get());
\r
212 blueprintModelRepository.delete(dbBlueprintModel.get());
\r
214 throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));
\r
219 * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database
\r
221 * @return List<BlueprintModelSearch> list with the controller blueprint archives
\r
223 public List<BlueprintModelSearch> getAllBlueprintModel() {
\r
224 return blueprintModelSearchRepository.findAll();
\r