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.config.BluePrintLoadConfiguration;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModelSearch;
\r
29 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelContentRepository;
\r
30 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelRepository;
\r
31 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ControllerBlueprintModelSearchRepository;
\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 ControllerBlueprintModelSearchRepository blueprintModelSearchRepository;
\r
71 private ControllerBlueprintModelRepository blueprintModelRepository;
\r
74 private ControllerBlueprintModelContentRepository 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
\r
90 .getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath);
\r
91 return BluePrintEnhancerUtils.Companion.saveCBAFile(filePart, cbaLocation).map(fileName -> {
\r
92 String blueprintId = bluePrintCatalogService
\r
93 .uploadToDataBase(cbaLocation.resolve(fileName).toString(), false);
\r
94 return blueprintModelSearchRepository.findById(blueprintId).get();
\r
97 } catch (IOException | BluePrintException e) {
\r
98 return Mono.error(new BluePrintException("Error uploading the CBA file in channel.", e));
\r
103 * This is a publishBlueprintModel method
\r
106 * @return BlueprintModelSearch
\r
107 * @throws BluePrintException BluePrintException
\r
109 public BlueprintModelSearch publishBlueprintModel(String id) throws BluePrintException {
\r
110 // TODO Implement publish Functionality
\r
115 * This is a searchBlueprintModels method
\r
118 * @return List<BlueprintModelSearch>
\r
120 public List<BlueprintModelSearch> searchBlueprintModels(String tags) {
\r
121 return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags);
\r
125 * This is a getBlueprintModelByNameAndVersion method
\r
128 * @param version version
\r
129 * @return BlueprintModelSearch
\r
131 public BlueprintModelSearch getBlueprintModelByNameAndVersion(@NotNull String name, @NotNull String version)
\r
132 throws BluePrintException {
\r
133 BlueprintModelSearch blueprintModelSearch;
\r
134 Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository
\r
135 .findByArtifactNameAndArtifactVersion(name, version);
\r
136 if (dbBlueprintModel.isPresent()) {
\r
137 blueprintModelSearch = dbBlueprintModel.get();
\r
139 throw new BluePrintException(String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));
\r
142 return blueprintModelSearch;
\r
146 * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource using MONO
\r
148 * @return ResponseEntity<Resource>
\r
150 public ResponseEntity<Resource> downloadBlueprintModelFile(@NotNull String id) throws BluePrintException {
\r
151 BlueprintModel blueprintModel;
\r
153 blueprintModel = getBlueprintModel(id);
\r
154 } catch (BluePrintException e) {
\r
155 throw new BluePrintException("Error uploading the CBA file in channel.", e);
\r
157 String fileName = blueprintModel.getId() + ".zip";
\r
158 byte[] file = blueprintModel.getBlueprintModelContent().getContent();
\r
159 return ResponseEntity.ok()
\r
160 .contentType(MediaType.parseMediaType("text/plain"))
\r
161 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
\r
162 .body(new ByteArrayResource(file));
\r
166 * This is a getBlueprintModel method
\r
169 * @return BlueprintModel
\r
170 * @throws BluePrintException BluePrintException
\r
172 private BlueprintModel getBlueprintModel(@NotNull String id) throws BluePrintException {
\r
173 BlueprintModel blueprintModel;
\r
174 Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findById(id);
\r
175 if (dbBlueprintModel.isPresent()) {
\r
176 blueprintModel = dbBlueprintModel.get();
\r
178 throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));
\r
181 return blueprintModel;
\r
185 * This is a getBlueprintModelSearch method
\r
188 * @return BlueprintModelSearch
\r
189 * @throws BluePrintException BluePrintException
\r
191 public BlueprintModelSearch getBlueprintModelSearch(@NotNull String id) throws BluePrintException {
\r
192 BlueprintModelSearch blueprintModelSearch;
\r
193 Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository.findById(id);
\r
194 if (dbBlueprintModel.isPresent()) {
\r
195 blueprintModelSearch = dbBlueprintModel.get();
\r
197 throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));
\r
200 return blueprintModelSearch;
\r
204 * This is a deleteBlueprintModel method
\r
207 * @throws BluePrintException BluePrintException
\r
210 public void deleteBlueprintModel(@NotNull String id) throws BluePrintException {
\r
211 Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findById(id);
\r
212 if (dbBlueprintModel.isPresent()) {
\r
213 blueprintModelContentRepository.deleteByBlueprintModel(dbBlueprintModel.get());
\r
214 blueprintModelRepository.delete(dbBlueprintModel.get());
\r
216 throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));
\r
221 * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database
\r
223 * @return List<BlueprintModelSearch> list with the controller blueprint archives
\r
225 public List<BlueprintModelSearch> getAllBlueprintModel() {
\r
226 return blueprintModelSearchRepository.findAll();
\r