ba27742afb3b48b42a1c5db19680abb77a00ff62
[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.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
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 BlueprintModelSearchRepository blueprintModelSearchRepository;\r
69 \r
70     @Autowired\r
71     private BlueprintModelRepository blueprintModelRepository;\r
72 \r
73     @Autowired\r
74     private BlueprintModelContentRepository 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.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
93             });\r
94 \r
95         } catch (IOException | BluePrintException e) {\r
96             return Mono.error(new BluePrintException("Error uploading the CBA file in channel.", e));\r
97         }\r
98     }\r
99 \r
100     /**\r
101      * This is a publishBlueprintModel method\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         // TODO Implement publish Functionality\r
109         return null;\r
110     }\r
111 \r
112     /**\r
113      * This is a searchBlueprintModels method\r
114      *\r
115      * @param tags tags\r
116      * @return List<BlueprintModelSearch>\r
117      */\r
118     public List<BlueprintModelSearch> searchBlueprintModels(String tags) {\r
119         return blueprintModelSearchRepository.findByTagsContainingIgnoreCase(tags);\r
120     }\r
121 \r
122     /**\r
123      * This is a getBlueprintModelByNameAndVersion method\r
124      *\r
125      * @param name    name\r
126      * @param version version\r
127      * @return BlueprintModelSearch\r
128      */\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
135         } else {\r
136             throw new BluePrintException(String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));\r
137         }\r
138 \r
139         return blueprintModelSearch;\r
140     }\r
141 \r
142     /**\r
143      * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource using MONO\r
144      *\r
145      * @param (id)\r
146      * @return ResponseEntity<Resource>\r
147      */\r
148     public ResponseEntity<Resource> downloadBlueprintModelFile(@NotNull String id) throws BluePrintException {\r
149         BlueprintModel blueprintModel;\r
150         try {\r
151             blueprintModel = getBlueprintModel(id);\r
152         } catch (BluePrintException e) {\r
153             throw new BluePrintException("Error uploading the CBA file in channel.", e);\r
154         }\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
161     }\r
162 \r
163     /**\r
164      * This is a getBlueprintModel method\r
165      *\r
166      * @param id id\r
167      * @return BlueprintModel\r
168      * @throws BluePrintException BluePrintException\r
169      */\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
175         } else {\r
176             throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));\r
177         }\r
178 \r
179         return blueprintModel;\r
180     }\r
181 \r
182     /**\r
183      * This is a getBlueprintModelSearch method\r
184      *\r
185      * @param id id\r
186      * @return BlueprintModelSearch\r
187      * @throws BluePrintException BluePrintException\r
188      */\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
194         } else {\r
195             throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));\r
196         }\r
197 \r
198         return blueprintModelSearch;\r
199     }\r
200 \r
201     /**\r
202      * This is a deleteBlueprintModel method\r
203      *\r
204      * @param id id\r
205      * @throws BluePrintException BluePrintException\r
206      */\r
207     @Transactional\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
213         } else {\r
214             throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));\r
215         }\r
216     }\r
217 \r
218     /**\r
219      * This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database\r
220      *\r
221      * @return List<BlueprintModelSearch> list with the controller blueprint archives\r
222      */\r
223     public List<BlueprintModelSearch> getAllBlueprintModel() {\r
224         return blueprintModelSearchRepository.findAll();\r
225     }\r
226 \r
227 }\r