3cf144f175131cc47300acd644a107c8f127db27
[ccsdk/cds.git] /
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2019 Bell Canada.\r
4  *\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
8  *\r
9  *     http://www.apache.org/licenses/LICENSE-2.0\r
10  *\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
16  */\r
17 \r
18 package org.onap.ccsdk.apps.controllerblueprints.service;\r
19 \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
47 \r
48 /**\r
49  * BlueprintModelService.java Purpose: Provide Service Template Service processing BlueprintModelService\r
50  *\r
51  * @author Brinda Santh\r
52  * @version 1.0\r
53  */\r
54 \r
55 @Service\r
56 public class BlueprintModelService {\r
57 \r
58     @Autowired\r
59     private BluePrintLoadConfiguration bluePrintLoadConfiguration;\r
60 \r
61     @Autowired\r
62     private BluePrintCatalogService bluePrintCatalogService;\r
63 \r
64     @Autowired\r
65     private ControllerBlueprintModelSearchRepository blueprintModelSearchRepository;\r
66 \r
67     @Autowired\r
68     private ControllerBlueprintModelRepository blueprintModelRepository;\r
69 \r
70     @Autowired\r
71     private ControllerBlueprintModelContentRepository blueprintModelContentRepository;\r
72 \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";
76 \r
77     /**\r
78      * This is a saveBlueprintModel method\r
79      *\r
80      * @param filePart filePart\r
81      * @return Mono<BlueprintModelSearch>\r
82      * @throws BluePrintException BluePrintException\r
83      */\r
84     public Mono<BlueprintModelSearch> saveBlueprintModel(FilePart filePart) throws BluePrintException {\r
85         try {\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;
90                 try {
91                     blueprintId = bluePrintCatalogService
92                         .saveToDatabase(cbaLocation.toFile(), false);
93                 } catch (BluePrintException e) {
94                     // FIXME handle expection
95                 }
96                 return blueprintModelSearchRepository.findById(blueprintId).get();\r
97             });\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);
101         }\r
102     }\r
103 \r
104     /**\r
105      * This is a publishBlueprintModel method to change the status published to YES\r
106      *\r
107      * @param id id\r
108      * @return BlueprintModelSearch\r
109      * @throws BluePrintException BluePrintException\r
110      */\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
116         } else {\r
117             String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
118             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
119         }\r
120         blueprintModelSearch.setPublished(ApplicationConstants.ACTIVE_Y);\r
121         return blueprintModelSearchRepository.saveAndFlush(blueprintModelSearch);\r
122     }\r
123 \r
124     /**\r
125      * This is a searchBlueprintModels method\r
126      *\r
127      * @param tags tags\r
128      * @return List<BlueprintModelSearch>\r
129      */\r
130     public List<BlueprintModelSearch> searchBlueprintModels(String tags) {\r
131         return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags);\r
132     }\r
133 \r
134     /**\r
135      * This is a getBlueprintModelSearchByNameAndVersion method\r
136      *\r
137      * @param name name\r
138      * @param version version\r
139      * @return BlueprintModelSearch\r
140      * @throws BluePrintException BluePrintException\r
141      */\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
149         } else {\r
150             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(),\r
151                 String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));
152         }\r
153         return blueprintModelSearch;\r
154     }\r
155 \r
156     /**\r
157      * This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version\r
158      *\r
159      * @param name name
160      * @param version version\r
161      * @return ResponseEntity<Resource>\r
162      * @throws BluePrintException BluePrintException\r
163      */\r
164     public ResponseEntity<Resource> downloadBlueprintModelFileByNameAndVersion(@NotNull String name,
165         @NotNull String version)
166         throws BluePrintException {
167         BlueprintModel blueprintModel;\r
168         try {\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);
173         }\r
174         String fileName = blueprintModel.getId() + ".zip";\r
175         byte[] file = blueprintModel.getBlueprintModelContent().getContent();\r
176         return prepareResourceEntity(fileName, file);\r
177     }\r
178 \r
179     /**\r
180      * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource\r
181      *\r
182      * @return ResponseEntity<Resource>\r
183      * @throws BluePrintException BluePrintException\r
184      */\r
185     public ResponseEntity<Resource> downloadBlueprintModelFile(@NotNull String id) throws BluePrintException {\r
186         BlueprintModel blueprintModel;\r
187         try {\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);
192         }\r
193         String fileName = blueprintModel.getId() + ".zip";\r
194         byte[] file = blueprintModel.getBlueprintModelContent().getContent();\r
195         return prepareResourceEntity(fileName, file);\r
196     }\r
197 \r
198     /**\r
199      * @return ResponseEntity<Resource>\r
200      */\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));
206     }\r
207 \r
208     /**\r
209      * This is a getBlueprintModel method\r
210      *\r
211      * @param id id\r
212      * @return BlueprintModel\r
213      * @throws BluePrintException BluePrintException\r
214      */\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
220         } else {\r
221             String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
222             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
223         }\r
224         return blueprintModel;\r
225     }\r
226 \r
227     /**\r
228      * This is a getBlueprintModelByNameAndVersion method\r
229      *\r
230      * @param name name
231      * @param version version\r
232      * @return BlueprintModel\r
233      * @throws BluePrintException BluePrintException\r
234      */\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;
241         } else {\r
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
244         }\r
245     }\r
246 \r
247     /**\r
248      * This is a getBlueprintModelSearch method\r
249      *\r
250      * @param id id\r
251      * @return BlueprintModelSearch\r
252      * @throws BluePrintException BluePrintException\r
253      */\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
259         } else {\r
260             String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
261             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
262         }\r
263 \r
264         return blueprintModelSearch;\r
265     }\r
266 \r
267     /**\r
268      * This is a deleteBlueprintModel method\r
269      *\r
270      * @param id id\r
271      * @throws BluePrintException BluePrintException\r
272      */\r
273     @Transactional\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
279         } else {\r
280             String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
281             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
282         }\r
283     }\r
284 \r
285     /**\r
286      * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database\r
287      *\r
288      * @return List<BlueprintModelSearch> list of the controller blueprint archives\r
289      */\r
290     public List<BlueprintModelSearch> getAllBlueprintModel() {\r
291         return blueprintModelSearchRepository.findAll();\r
292     }\r
293 }\r