e80fa8cdc1c16190656e86d1814f66a7b06ea6e1
[ccsdk/apps.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 org.jetbrains.annotations.NotNull;\r
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants;\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration;\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode;\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
43 \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
48 \r
49 /**\r
50  * BlueprintModelService.java Purpose: Provide Service Template Service processing BlueprintModelService\r
51  *\r
52  * @author Brinda Santh\r
53  * @version 1.0\r
54  */\r
55 \r
56 @Service\r
57 public class BlueprintModelService {\r
58 \r
59     @Autowired\r
60     private BluePrintLoadConfiguration bluePrintLoadConfiguration;\r
61 \r
62     @Autowired\r
63     private BluePrintCatalogService bluePrintCatalogService;\r
64 \r
65     @Autowired\r
66     private ControllerBlueprintModelSearchRepository blueprintModelSearchRepository;\r
67 \r
68     @Autowired\r
69     private ControllerBlueprintModelRepository blueprintModelRepository;\r
70 \r
71     @Autowired\r
72     private ControllerBlueprintModelContentRepository blueprintModelContentRepository;\r
73 \r
74     private static final String BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%s) from repo";\r
75     private static final String BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%s)" +\r
76                                                                     " and version(%s) from repo";\r
77 \r
78     /**\r
79      * This is a saveBlueprintModel method\r
80      *\r
81      * @param filePart filePart\r
82      * @return Mono<BlueprintModelSearch>\r
83      * @throws BluePrintException BluePrintException\r
84      */\r
85     public Mono<BlueprintModelSearch> saveBlueprintModel(FilePart filePart) throws BluePrintException {\r
86         try {\r
87             Path cbaLocation = BluePrintFileUtils.Companion\r
88                     .getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath);\r
89             return BluePrintEnhancerUtils.Companion.saveCBAFile(filePart, cbaLocation).map(fileName -> {\r
90                 String blueprintId = bluePrintCatalogService\r
91                         .uploadToDataBase(cbaLocation.resolve(fileName).toString(), false);\r
92                 return blueprintModelSearchRepository.findById(blueprintId).get();\r
93             });\r
94         } catch (IOException e) {\r
95             throw new BluePrintException(ErrorCode.IO_FILE_INTERRUPT.getValue(),\r
96                     String.format("I/O Error while uploading the CBA file: %s", e.getMessage()), e);\r
97         }\r
98     }\r
99 \r
100     /**\r
101      * This is a publishBlueprintModel method to change the status published to YES\r
102      *\r
103      * @param id id\r
104      * @return BlueprintModelSearch\r
105      * @throws BluePrintException BluePrintException\r
106      */\r
107     public BlueprintModelSearch publishBlueprintModel(String id) throws BluePrintException {\r
108         BlueprintModelSearch blueprintModelSearch;\r
109         Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository.findById(id);\r
110         if (dbBlueprintModel.isPresent()) {\r
111             blueprintModelSearch = dbBlueprintModel.get();\r
112         } else {\r
113             String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
114             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
115         }\r
116         blueprintModelSearch.setPublished(ApplicationConstants.ACTIVE_Y);\r
117         return blueprintModelSearchRepository.saveAndFlush(blueprintModelSearch);\r
118     }\r
119 \r
120     /**\r
121      * This is a searchBlueprintModels method\r
122      *\r
123      * @param tags tags\r
124      * @return List<BlueprintModelSearch>\r
125      */\r
126     public List<BlueprintModelSearch> searchBlueprintModels(String tags) {\r
127         return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags);\r
128     }\r
129 \r
130     /**\r
131      * This is a getBlueprintModelSearchByNameAndVersion method\r
132      *\r
133      * @param name name\r
134      * @param version version\r
135      * @return BlueprintModelSearch\r
136      * @throws BluePrintException BluePrintException\r
137      */\r
138     public BlueprintModelSearch getBlueprintModelSearchByNameAndVersion(@NotNull String name, @NotNull String version)\r
139             throws BluePrintException {\r
140         BlueprintModelSearch blueprintModelSearch;\r
141         Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository\r
142                 .findByArtifactNameAndArtifactVersion(name, version);\r
143         if (dbBlueprintModel.isPresent()) {\r
144             blueprintModelSearch = dbBlueprintModel.get();\r
145         } else {\r
146             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(),\r
147                     String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));\r
148         }\r
149         return blueprintModelSearch;\r
150     }\r
151 \r
152     /**\r
153      * This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version\r
154      *\r
155      * @param name    name\r
156      * @param version version\r
157      * @return ResponseEntity<Resource>\r
158      * @throws BluePrintException BluePrintException\r
159      */\r
160     public ResponseEntity<Resource> downloadBlueprintModelFileByNameAndVersion(@NotNull String name, @NotNull String version)\r
161             throws BluePrintException {\r
162         BlueprintModel blueprintModel;\r
163         try {\r
164             blueprintModel = getBlueprintModelByNameAndVersion(name, version);\r
165         } catch (BluePrintException e) {\r
166             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +\r
167                     "downloading the CBA file: %s", e.getMessage()), e);\r
168         }\r
169         String fileName = blueprintModel.getId() + ".zip";\r
170         byte[] file = blueprintModel.getBlueprintModelContent().getContent();\r
171         return prepareResourceEntity(fileName, file);\r
172     }\r
173 \r
174     /**\r
175      * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource\r
176      *\r
177      * @return ResponseEntity<Resource>\r
178      * @throws BluePrintException BluePrintException\r
179      */\r
180     public ResponseEntity<Resource> downloadBlueprintModelFile(@NotNull String id) throws BluePrintException {\r
181         BlueprintModel blueprintModel;\r
182         try {\r
183             blueprintModel = getBlueprintModel(id);\r
184         } catch (BluePrintException e) {\r
185             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +\r
186                     "downloading the CBA file: %s", e.getMessage()), e);\r
187         }\r
188         String fileName = blueprintModel.getId() + ".zip";\r
189         byte[] file = blueprintModel.getBlueprintModelContent().getContent();\r
190         return prepareResourceEntity(fileName, file);\r
191     }\r
192 \r
193     /**\r
194      *\r
195      * @param (fileName, file)\r
196      * @return ResponseEntity<Resource>\r
197      */\r
198     private ResponseEntity<Resource> prepareResourceEntity(String fileName, byte[] file) {\r
199         return ResponseEntity.ok()\r
200                 .contentType(MediaType.parseMediaType("text/plain"))\r
201                 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")\r
202                 .body(new ByteArrayResource(file));\r
203     }\r
204 \r
205     /**\r
206      * This is a getBlueprintModel method\r
207      *\r
208      * @param id id\r
209      * @return BlueprintModel\r
210      * @throws BluePrintException BluePrintException\r
211      */\r
212     private BlueprintModel getBlueprintModel(@NotNull String id) throws BluePrintException {\r
213         BlueprintModel blueprintModel;\r
214         Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findById(id);\r
215         if (dbBlueprintModel.isPresent()) {\r
216             blueprintModel = dbBlueprintModel.get();\r
217         } else {\r
218             String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
219             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
220         }\r
221         return blueprintModel;\r
222     }\r
223 \r
224     /**\r
225      * This is a getBlueprintModelByNameAndVersion method\r
226      *\r
227      * @param name    name\r
228      * @param version version\r
229      * @return BlueprintModel\r
230      * @throws BluePrintException BluePrintException\r
231      */\r
232     private BlueprintModel getBlueprintModelByNameAndVersion(@NotNull String name, @NotNull String version)\r
233             throws BluePrintException {\r
234         BlueprintModel blueprintModel;\r
235         Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version);\r
236         if (dbBlueprintModel.isPresent()) {\r
237             blueprintModel = dbBlueprintModel.get();\r
238         } else {\r
239             String msg = String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version);\r
240             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
241         }\r
242         return blueprintModel;\r
243     }\r
244 \r
245     /**\r
246      * This is a getBlueprintModelSearch method\r
247      *\r
248      * @param id id\r
249      * @return BlueprintModelSearch\r
250      * @throws BluePrintException BluePrintException\r
251      */\r
252     public BlueprintModelSearch getBlueprintModelSearch(@NotNull String id) throws BluePrintException {\r
253         BlueprintModelSearch blueprintModelSearch;\r
254         Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository.findById(id);\r
255         if (dbBlueprintModel.isPresent()) {\r
256             blueprintModelSearch = dbBlueprintModel.get();\r
257         } else {\r
258             String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
259             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
260         }\r
261 \r
262         return blueprintModelSearch;\r
263     }\r
264 \r
265     /**\r
266      * This is a deleteBlueprintModel method\r
267      *\r
268      * @param id id\r
269      * @throws BluePrintException BluePrintException\r
270      */\r
271     @Transactional\r
272     public void deleteBlueprintModel(@NotNull String id) throws BluePrintException {\r
273         Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findById(id);\r
274         if (dbBlueprintModel.isPresent()) {\r
275             blueprintModelContentRepository.deleteByBlueprintModel(dbBlueprintModel.get());\r
276             blueprintModelRepository.delete(dbBlueprintModel.get());\r
277         } else {\r
278             String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);\r
279             throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);\r
280         }\r
281     }\r
282 \r
283     /**\r
284      * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database\r
285      *\r
286      * @return List<BlueprintModelSearch> list of the controller blueprint archives\r
287      */\r
288     public List<BlueprintModelSearch> getAllBlueprintModel() {\r
289         return blueprintModelSearchRepository.findAll();\r
290     }\r
291 }\r