rApp package validation code refactor
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / main / java / com / oransc / rappmanager / rest / RappController.java
index 8cfbddc..85e1a44 100755 (executable)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START======================================================================
- * Copyright (C) 2023 Nordix Foundation. All rights reserved.
+ * Copyright (C) 2023-2024 Nordix Foundation. All rights reserved.
  * ===============================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ package com.oransc.rappmanager.rest;
 import com.oransc.rappmanager.configuration.RappManagerConfiguration;
 import com.oransc.rappmanager.models.cache.RappCacheService;
 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
+import com.oransc.rappmanager.models.csar.validator.RappValidationHandler;
 import com.oransc.rappmanager.models.exception.RappHandlerException;
 import com.oransc.rappmanager.models.rapp.PrimeOrder;
 import com.oransc.rappmanager.models.rapp.Rapp;
@@ -56,9 +57,11 @@ public class RappController {
 
     Logger logger = LoggerFactory.getLogger(RappController.class);
     private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
+    private final RappValidationHandler rappValidationHandler;
     private final RappManagerConfiguration rappManagerConfiguration;
     private final RappCacheService rappCacheService;
     private final RappService rappService;
+    private static final String RAPP_NOT_FOUND = "rApp %s not found.";
 
     @GetMapping
     public ResponseEntity<Collection<Rapp>> getRapps() {
@@ -68,13 +71,13 @@ public class RappController {
     @GetMapping("{rapp_id}")
     public ResponseEntity<Rapp> getRapp(@PathVariable("rapp_id") String rappId) {
         return rappCacheService.getRapp(rappId).map(ResponseEntity::ok).orElseThrow(
-                () -> new RappHandlerException(HttpStatus.NOT_FOUND, "rApp '" + rappId + "' not found."));
+                () -> new RappHandlerException(HttpStatus.NOT_FOUND, String.format(RAPP_NOT_FOUND, rappId)));
     }
 
     @PostMapping("{rapp_id}")
     public ResponseEntity<Rapp> createRapp(@PathVariable("rapp_id") String rappId,
             @RequestPart("file") MultipartFile csarFilePart) throws IOException {
-        if (rappCsarConfigurationHandler.isValidRappPackage(csarFilePart)) {
+        if (rappValidationHandler.isValidRappPackage(csarFilePart)) {
             File csarFile = new File(
                     rappCsarConfigurationHandler.getRappPackageLocation(rappManagerConfiguration.getCsarLocation(),
                             rappId, csarFilePart.getOriginalFilename()).toUri());
@@ -102,21 +105,17 @@ public class RappController {
                             .map(primeOrder -> rappService.primeRapp(rapp))
                             .orElseGet(() -> rappService.deprimeRapp(rapp)))
                        .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
-                               "rApp '" + rappId + "' not found."));
+                               String.format(RAPP_NOT_FOUND, rappId)));
         // @formatter:on
     }
 
     @DeleteMapping("{rapp_id}")
-    public ResponseEntity<Object> deleteRapp(@PathVariable("rapp_id") String rappId) {
+    public ResponseEntity<String> deleteRapp(@PathVariable("rapp_id") String rappId) {
         // @formatter:off
         return rappCacheService.getRapp(rappId)
-               .filter(rapp -> rapp.getRappInstances().isEmpty() && rapp.getState().equals(RappState.COMMISSIONED))
-               .map(rapp -> {
-                   rappCacheService.deleteRapp(rapp);
-                   return ResponseEntity.ok().build();
-               })
+               .map(rappService::deleteRapp)
                .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
-                               "rApp '" + rappId + "' not found."));
+                       String.format(RAPP_NOT_FOUND, rappId)));
         // @formatter:on
     }
 }