4608b17550309ceb7279be9a3b91c5b05cafd123
[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.rs;\r
19 \r
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
21 import org.onap.ccsdk.apps.controllerblueprints.service.CbaService;\r
22 import org.onap.ccsdk.apps.controllerblueprints.service.model.BlueprintModelResponse;\r
23 import org.onap.ccsdk.apps.controllerblueprints.service.model.ItemCbaResponse;\r
24 import org.springframework.beans.factory.annotation.Autowired;\r
25 import org.springframework.core.io.Resource;\r
26 import org.springframework.http.MediaType;\r
27 import org.springframework.http.ResponseEntity;\r
28 import org.springframework.http.codec.multipart.FilePart;\r
29 import org.springframework.http.codec.multipart.Part;\r
30 import org.springframework.web.bind.annotation.*;\r
31 import reactor.core.publisher.Flux;\r
32 \r
33 import java.util.List;\r
34 \r
35 /**\r
36  * CbaRest.java Purpose: Provide a REST API to upload single and multiple CBA\r
37  *\r
38  * @author Steve Siani\r
39  * @version 1.0\r
40  */\r
41 @RestController\r
42 @RequestMapping(value = "/api/v1/cba")\r
43 public class CbaRest {\r
44 \r
45     @Autowired\r
46     private CbaService cbaService;\r
47 \r
48     @PostMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)\r
49     public  Flux<BlueprintModelResponse> uploadCBA(@RequestBody Flux<Part> parts) {\r
50         return parts.filter(part -> part instanceof FilePart) // only retain file parts\r
51             .ofType(FilePart.class) // convert the flux to FilePart\r
52             .flatMap(filePart -> cbaService.uploadCBAFile(filePart)); // save each file and flatmap it to a flux of results\r
53     }\r
54 \r
55     @DeleteMapping(path = "/{id}")\r
56     public void deleteCBA(@PathVariable(value = "id") Long id) throws BluePrintException {\r
57         this.cbaService.deleteCBA(id);\r
58     }\r
59 \r
60     @GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)\r
61     public @ResponseBody\r
62     ItemCbaResponse getCBA(@PathVariable(value = "id") String id) {\r
63         return this.cbaService.findCBAByID(id);\r
64     }\r
65 \r
66     @GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE)\r
67     public @ResponseBody\r
68     List<ItemCbaResponse> getAllCBA() {\r
69         return this.cbaService.findAllCBA();\r
70     }\r
71 \r
72     @GetMapping(path = "/by-name/{name}/version/{version}", produces = MediaType.APPLICATION_JSON_VALUE)\r
73     public @ResponseBody\r
74     ItemCbaResponse getCBAByNameAndVersion(@PathVariable(value = "name") String name,\r
75                                                           @PathVariable(value = "version") String version) throws BluePrintException {\r
76         return this.cbaService.findCBAByNameAndVersion(name, version);\r
77     }\r
78 \r
79     @GetMapping(path = "/download/{id}", produces = MediaType.APPLICATION_JSON_VALUE)\r
80     public @ResponseBody\r
81     ResponseEntity<Resource> downloadCBA(@PathVariable(value = "id") String id) {\r
82         return this.cbaService.downloadCBAFile(id);\r
83     }\r
84 }\r