ca0e243938a7fa62e06562a9ad0804722d3b2236
[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 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
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     private static EELFLogger log = EELFManager.getInstance().getLogger(BlueprintModelService.class);\r
60 \r
61     @Autowired\r
62     private BluePrintLoadConfiguration bluePrintLoadConfiguration;\r
63 \r
64     @Autowired\r
65     private BluePrintCatalogService bluePrintCatalogService;\r
66 \r
67     @Autowired\r
68     private ControllerBlueprintModelSearchRepository blueprintModelSearchRepository;\r
69 \r
70     @Autowired\r
71     private ControllerBlueprintModelRepository blueprintModelRepository;\r
72 \r
73     @Autowired\r
74     private ControllerBlueprintModelContentRepository blueprintModelContentRepository;\r
75 \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
79 \r
80     /**\r
81      * This is a saveBlueprintModel method\r
82      *\r
83      * @param filePart filePart\r
84      * @return Mono<BlueprintModelSearch>\r
85      * @throws BluePrintException BluePrintException\r
86      */\r
87     public Mono<BlueprintModelSearch> saveBlueprintModel(FilePart filePart) throws BluePrintException {\r
88         try {\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
95             });\r
96 \r
97         } catch (IOException | BluePrintException e) {\r
98             return Mono.error(new BluePrintException("Error uploading the CBA file in channel.", e));\r
99         }\r
100     }\r
101 \r
102     /**\r
103      * This is a publishBlueprintModel method\r
104      *\r
105      * @param id id\r
106      * @return BlueprintModelSearch\r
107      * @throws BluePrintException BluePrintException\r
108      */\r
109     public BlueprintModelSearch publishBlueprintModel(String id) throws BluePrintException {\r
110         // TODO Implement publish Functionality\r
111         return null;\r
112     }\r
113 \r
114     /**\r
115      * This is a searchBlueprintModels method\r
116      *\r
117      * @param tags tags\r
118      * @return List<BlueprintModelSearch>\r
119      */\r
120     public List<BlueprintModelSearch> searchBlueprintModels(String tags) {\r
121         return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags);\r
122     }\r
123 \r
124     /**\r
125      * This is a getBlueprintModelByNameAndVersion method\r
126      *\r
127      * @param name name\r
128      * @param version version\r
129      * @return BlueprintModelSearch\r
130      */\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
138         } else {\r
139             throw new BluePrintException(String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));\r
140         }\r
141 \r
142         return blueprintModelSearch;\r
143     }\r
144 \r
145     /**\r
146      * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource using MONO\r
147      *\r
148      * @return ResponseEntity<Resource>\r
149      */\r
150     public ResponseEntity<Resource> downloadBlueprintModelFile(@NotNull String id) throws BluePrintException {\r
151         BlueprintModel blueprintModel;\r
152         try {\r
153             blueprintModel = getBlueprintModel(id);\r
154         } catch (BluePrintException e) {\r
155             throw new BluePrintException("Error uploading the CBA file in channel.", e);\r
156         }\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
163     }\r
164 \r
165     /**\r
166      * This is a getBlueprintModel method\r
167      *\r
168      * @param id id\r
169      * @return BlueprintModel\r
170      * @throws BluePrintException BluePrintException\r
171      */\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
177         } else {\r
178             throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));\r
179         }\r
180 \r
181         return blueprintModel;\r
182     }\r
183 \r
184     /**\r
185      * This is a getBlueprintModelSearch method\r
186      *\r
187      * @param id id\r
188      * @return BlueprintModelSearch\r
189      * @throws BluePrintException BluePrintException\r
190      */\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
196         } else {\r
197             throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));\r
198         }\r
199 \r
200         return blueprintModelSearch;\r
201     }\r
202 \r
203     /**\r
204      * This is a deleteBlueprintModel method\r
205      *\r
206      * @param id id\r
207      * @throws BluePrintException BluePrintException\r
208      */\r
209     @Transactional\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
215         } else {\r
216             throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));\r
217         }\r
218     }\r
219 \r
220     /**\r
221      * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database\r
222      *\r
223      * @return List<BlueprintModelSearch> list with the controller blueprint archives\r
224      */\r
225     public List<BlueprintModelSearch> getAllBlueprintModel() {\r
226         return blueprintModelSearchRepository.findAll();\r
227     }\r
228 \r
229 }\r