Fix onboarding and deleting the chart

Change-Id: Icdef21b2827ea614e4440e40f7b61d89a47d8fe8
Signed-off-by: RehanRaza <muhammad.rehan.raza@est.tech>
diff --git a/rapp-manager/src/main/java/org/oransc/rappmanager/controller/ChartController.java b/rapp-manager/src/main/java/org/oransc/rappmanager/controller/ChartController.java
index 06ff6ba..bcf876b 100644
--- a/rapp-manager/src/main/java/org/oransc/rappmanager/controller/ChartController.java
+++ b/rapp-manager/src/main/java/org/oransc/rappmanager/controller/ChartController.java
@@ -18,9 +18,6 @@
 
 package org.oransc.rappmanager.controller;
 
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiResponse;
@@ -28,6 +25,10 @@
 
 import java.util.Map;
 
+import javax.validation.Valid;
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+
 import org.oransc.rappmanager.exception.ServiceException;
 import org.oransc.rappmanager.service.ChartService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -39,7 +40,7 @@
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RequestPart;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -51,8 +52,6 @@
     @Autowired
     private ChartService chartService;
 
-    private static Gson gson = new GsonBuilder().create();
-
     @GetMapping(path = "/charts", produces = MediaType.APPLICATION_JSON_VALUE)
     @ApiOperation(value = "Return all Charts")
     @ApiResponses(value = {@ApiResponse(code = 200, message = "Chart List")})
@@ -73,24 +72,17 @@
         produces = MediaType.APPLICATION_JSON_VALUE)
     @ApiOperation(value = "Onboard the Chart")
     @ApiResponses(value = {@ApiResponse(code = 201, message = "Chart Onboarded")})
-    public ResponseEntity<String> onboardChart(@RequestParam("file") MultipartFile file,
-        @RequestParam("chart") String chartJson) throws ServiceException {
-        // TODO the json is a string here, but it should be Chart
-        Chart chart = gson.fromJson(chartJson, Chart.class);
-
-        return chartService.saveChart(chart, file).block();
+    public ResponseEntity<String> onboardChart(@RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file,
+        @RequestPart("chart") @Valid Chart chart) throws ServiceException {
+        return chartService.saveChart(chart, file);
     }
 
-    @DeleteMapping(
-        path = "/charts/{id}",
-        consumes = MediaType.APPLICATION_JSON_VALUE,
-        produces = MediaType.APPLICATION_JSON_VALUE)
+    @DeleteMapping(path = "/charts/{name}/{version}")
     @ApiOperation(value = "Delete the Chart")
-    @ApiResponses(value = {@ApiResponse(code = 201, message = "Chart Deleted")})
+    @ApiResponses(value = {@ApiResponse(code = 204, message = "Chart Deleted")})
     public ResponseEntity<Object> deleteChart(@PathVariable("name") String name,
         @PathVariable("version") String version) throws ServiceException {
         chartService.deleteChart(name, version);
-        return new ResponseEntity<>(HttpStatus.CREATED);
+        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
     }
-
 }
diff --git a/rapp-manager/src/main/java/org/oransc/rappmanager/service/ChartService.java b/rapp-manager/src/main/java/org/oransc/rappmanager/service/ChartService.java
index aadc01c..ae8ae42 100644
--- a/rapp-manager/src/main/java/org/oransc/rappmanager/service/ChartService.java
+++ b/rapp-manager/src/main/java/org/oransc/rappmanager/service/ChartService.java
@@ -34,13 +34,13 @@
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.http.client.MultipartBodyBuilder;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.reactive.function.BodyInserters;
 import org.springframework.web.reactive.function.BodyInserters.MultipartInserter;
-import reactor.core.publisher.Mono;
 
 // Need to support multiple Chart registry. So add an interface and use it based on the registry choice.
 @Service
@@ -70,7 +70,7 @@
 
     public Chart[] getChart(String name) throws ServiceException {
         ResponseEntity<String> rsp = chartClient.getForEntity(basePath + "/" + name).block();
-        if (rsp.getStatusCodeValue() != 200) {
+        if (!rsp.getStatusCode().is2xxSuccessful()) {
             throw new ServiceException("Error Getting Chart " + name);
         }
         logger.debug("Sucessfully fetched the chart");
@@ -79,21 +79,25 @@
 
     private MultipartInserter createPostFileBody(MultipartFile chartFile) {
         MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
-        bodyBuilder.part("file", chartFile.getResource());
+        bodyBuilder.part("chart", chartFile.getResource());
         return BodyInserters.fromMultipartData(bodyBuilder.build());
     }
 
-    // Not working. Need to check
-    public Mono<ResponseEntity<String>> saveChart(Chart chart, MultipartFile chartFile) {
-        MultipartInserter body = createPostFileBody(chartFile);
-        return Mono.just(new ResponseEntity<>(chart.getName(), HttpStatus.OK)); // Dummy return for now
-        // return chartClient.postForEntity(basePath, body,
-        // MediaType.MULTIPART_FORM_DATA);
+    public ResponseEntity<String> saveChart(Chart chart, MultipartFile chartFile) {
+        try {
+            ResponseEntity<String> rsp = chartClient
+                .postForEntity(basePath, createPostFileBody(chartFile), MediaType.MULTIPART_FORM_DATA).block();
+            logger.debug("Sucessfully onboarded the chart");
+            return rsp;
+        } catch (Exception ex) {
+            logger.error("Could not onboard the chart, reason: {}", ex.toString());
+            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+        }
     }
 
     public void deleteChart(String name, String version) throws ServiceException {
         ResponseEntity<String> rsp = chartClient.deleteForEntity(basePath + "/" + name + "/" + version).block();
-        if (rsp.getStatusCodeValue() != 200) {
+        if (!rsp.getStatusCode().is2xxSuccessful()) {
             throw new ServiceException("Error in deleting Chart " + name);
         }
         logger.debug("Sucessfully deleted the chart");