7d616a76f0bb803e6b9bc8141254d6b05860d065
[ccsdk/cds.git] /
1 /*\r
2  * Copyright © 2018 IBM Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\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.utils.BluePrintFileUtils;\r
25 import org.onap.ccsdk.apps.controllerblueprints.service.domain.CbaContent;\r
26 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel;\r
27 import org.onap.ccsdk.apps.controllerblueprints.service.model.BlueprintModelResponse;\r
28 import org.onap.ccsdk.apps.controllerblueprints.service.model.ItemCbaResponse;\r
29 import org.onap.ccsdk.apps.controllerblueprints.service.utils.CbaStateEnum;\r
30 import org.springframework.beans.factory.annotation.Autowired;\r
31 import org.springframework.beans.factory.annotation.Value;\r
32 import org.springframework.boot.context.event.ApplicationReadyEvent;\r
33 import org.springframework.context.event.EventListener;\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 reactor.core.publisher.Mono;\r
42 import java.io.IOException;\r
43 import java.nio.file.Path;\r
44 import java.util.ArrayList;\r
45 import java.util.List;\r
46 import java.util.Optional;\r
47 \r
48 /**\r
49  * CbaService.java Purpose: Provide Service Template Service processing CbaService\r
50  *\r
51  * @author Steve Siani\r
52  * @version 1.0\r
53  */\r
54 \r
55 @Service\r
56 public class CbaService {\r
57 \r
58     private static EELFLogger log = EELFManager.getInstance().getLogger(CbaService.class);\r
59 \r
60     @Value("${controllerblueprints.blueprintArchivePath}")\r
61     private String cbaArchivePath;\r
62     private Path cbaLocation;\r
63 \r
64     @Autowired\r
65     private CbaFileManagementService cbaFileManagementService;\r
66 \r
67     @Autowired\r
68     private CbaToDatabaseService cbaToDatabaseService;\r
69 \r
70 \r
71     /**\r
72      * This method would be used by SpringBoot to initialize the cba location\r
73      */\r
74     @EventListener(ApplicationReadyEvent.class)\r
75     private void initCbaService() {\r
76         this.cbaLocation = BluePrintFileUtils.Companion.getCbaStorageDirectory(cbaArchivePath);\r
77         log.info("CBA service Initiated...");\r
78     }\r
79 \r
80     /**\r
81      * This is a uploadCBAFile method\r
82      * take a {@link FilePart}, transfer it to disk using WebFlux and return a {@link Mono} representing the result\r
83      *\r
84      * @param filePart - the request part containing the file to be saved\r
85      * @return a {@link Mono<  BlueprintModelResponse  >} representing the result of the operation\r
86      */\r
87     public Mono<BlueprintModelResponse> uploadCBAFile(FilePart filePart) {\r
88 \r
89         try {\r
90             return this.cbaFileManagementService.saveCBAFile(filePart, cbaLocation).map(fileName -> {\r
91                 ConfigModel configModel;\r
92                 BlueprintModelResponse blueprintModelResponse = null;\r
93 \r
94                 try {\r
95                     String cbaDirectory = this.cbaFileManagementService.decompressCBAFile(fileName, cbaLocation);\r
96                     configModel = this.cbaToDatabaseService.storeBluePrints(cbaDirectory, fileName, cbaLocation.resolve(fileName));\r
97                     blueprintModelResponse = new BlueprintModelResponse(configModel.getId(), configModel.getArtifactName(), configModel.getArtifactVersion(), configModel.getArtifactDescription(), configModel.getConfigModelCBA().getCbaUUID());\r
98                 } catch (BluePrintException be) {\r
99                     Mono.error(new BluePrintException("Error loading CBA in database.", be));\r
100                 } finally {\r
101                     try {\r
102                         this.cbaFileManagementService.cleanupSavedCBA(fileName, cbaLocation);\r
103                     } catch (BluePrintException be) {\r
104                         Mono.error(new BluePrintException("Error while cleaning up.", be));\r
105                     }\r
106                 }\r
107                 return blueprintModelResponse;\r
108             });\r
109         } catch (IOException | BluePrintException e) {\r
110             return Mono.error(new BluePrintException("Error uploading the CBA file in channel.", e));\r
111         }\r
112     }\r
113 \r
114     /**\r
115      * This is a deleteCba method\r
116      *\r
117      * @param id id\r
118      * @throws BluePrintException BluePrintException\r
119      */\r
120     public void deleteCBA(@NotNull Long id) throws BluePrintException {\r
121         this.cbaToDatabaseService.deleteCBA(id);\r
122     }\r
123 \r
124     /**\r
125      * This is a downloadCBAFile method to find the target file to download and return a file ressource using MONO\r
126      *\r
127      * @param (id)\r
128      * @return ResponseEntity<Resource>\r
129      */\r
130     public ResponseEntity<Resource> downloadCBAFile(@NotNull String id) {\r
131         Optional<CbaContent> optionalContent = this.cbaToDatabaseService.findByUUID(id);\r
132 \r
133         CbaContent cbaContent = optionalContent.get();\r
134 \r
135         return ResponseEntity.ok()\r
136                 .contentType(MediaType.parseMediaType("text/plain"))\r
137                 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + cbaContent.getCbaName() + "\"")\r
138                 .body(new ByteArrayResource(cbaContent.getCbaFile()));\r
139     }\r
140 \r
141     /**\r
142      * This is a findCBAByID method to find a CBA By the UUID\r
143      *\r
144      * @param (id)\r
145      * @return ItemCbaResponse\r
146      */\r
147     public ItemCbaResponse findCBAByID(@NotNull String id) {\r
148         ItemCbaResponse response = new ItemCbaResponse();\r
149         Optional<CbaContent> optionalContent = this.cbaToDatabaseService.findByUUID(id);\r
150 \r
151         CbaContent cbaContent = optionalContent.get();\r
152         response.setName(cbaContent.getCbaName());\r
153         response.setState(cbaContent.getCbaState());\r
154         response.setId(cbaContent.getCbaUUID());\r
155         response.setVersion(cbaContent.getCbaVersion());\r
156         response.setDescription(cbaContent.getCbaDescription());\r
157         return response;\r
158     }\r
159 \r
160     /**\r
161      * This is a findAllCBA method to retrieve all the CBAs in Database\r
162      *\r
163      * @return List<ItemCbaResponse> list with the controller blueprint archives\r
164      */\r
165     public List<ItemCbaResponse> findAllCBA() {\r
166         List<ItemCbaResponse> responseList = new ArrayList<>();\r
167         List<CbaContent> cbaContents = this.cbaToDatabaseService.listCBAFiles();\r
168 \r
169         for(CbaContent content: cbaContents){\r
170             ItemCbaResponse response = new ItemCbaResponse();\r
171             response.setName(content.getCbaName());\r
172             response.setState(content.getCbaState());\r
173             response.setId(content.getCbaUUID());\r
174             response.setVersion(content.getCbaVersion());\r
175             response.setDescription(content.getCbaDescription());\r
176 \r
177             responseList.add(response);\r
178         }\r
179         return responseList;\r
180     }\r
181 \r
182     /**\r
183      * This is a findCBAByNameAndVersion method to find a CBA by Name and version\r
184      *\r
185      * @param (name, version)\r
186      * @return\r
187      * @throws BluePrintException BluePrintException\r
188      */\r
189     public ItemCbaResponse findCBAByNameAndVersion(@NotNull String name, @NotNull String version) throws BluePrintException {\r
190         return null;\r
191     }\r
192 }