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.exception.RappHandlerException;
import com.oransc.rappmanager.models.rapp.PrimeOrder;
import com.oransc.rappmanager.models.rapp.Rapp;
import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
+import java.util.Collection;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.cache.Cache;
+import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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<Cache> getRapps() {
+ public ResponseEntity<Collection<Rapp>> getRapps() {
return ResponseEntity.ok(rappCacheService.getAllRapp());
}
@GetMapping("{rapp_id}")
public ResponseEntity<Rapp> getRapp(@PathVariable("rapp_id") String rappId) {
- return rappCacheService.getRapp(rappId).map(ResponseEntity::ok).orElse(ResponseEntity.badRequest().build());
+ return rappCacheService.getRapp(rappId).map(ResponseEntity::ok).orElseThrow(
+ () -> new RappHandlerException(HttpStatus.NOT_FOUND, String.format(RAPP_NOT_FOUND, rappId)));
}
@PostMapping("{rapp_id}")
return ResponseEntity.accepted().build();
} else {
logger.info("Invalid Rapp package for {}", rappId);
- return ResponseEntity.badRequest().build();
+ throw new RappHandlerException(HttpStatus.BAD_REQUEST, "Invalid rApp package.");
}
}
.filter(primeOrder -> primeOrder.equals(PrimeOrder.PRIME))
.map(primeOrder -> rappService.primeRapp(rapp))
.orElseGet(() -> rappService.deprimeRapp(rapp)))
- .orElse(ResponseEntity.notFound().build());
+ .orElseThrow(() -> new RappHandlerException(HttpStatus.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();
- })
- .orElse(ResponseEntity.notFound().build());
+ .map(rappService::deleteRapp)
+ .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
+ String.format(RAPP_NOT_FOUND, rappId)));
// @formatter:on
}
}