package com.oransc.rappmanager.rest;
-import com.oransc.rappmanager.models.rappinstance.DeployOrder;
+import com.oransc.rappmanager.models.cache.RappCacheService;
+import com.oransc.rappmanager.models.exception.RappHandlerException;
import com.oransc.rappmanager.models.rapp.Rapp;
+import com.oransc.rappmanager.models.rappinstance.DeployOrder;
import com.oransc.rappmanager.models.rappinstance.RappInstance;
import com.oransc.rappmanager.models.rappinstance.RappInstanceDeployOrder;
-import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
-import com.oransc.rappmanager.models.cache.RappCacheService;
import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
import com.oransc.rappmanager.service.RappService;
import java.util.Map;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;
+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 RappCacheService rappCacheService;
private final RappInstanceStateMachine rappInstanceStateMachine;
private final RappService rappService;
+ private static final String RAPP_INSTANCE_NOT_FOUND = "rApp instance %s not found.";
@GetMapping
public ResponseEntity<Map<UUID, RappInstance>> getAllRappInstances(@PathVariable("rapp_id") String rappId) {
- return rappCacheService.getRapp(rappId).map(Rapp::getRappInstances).map(ResponseEntity::ok)
- .orElse(ResponseEntity.notFound().build());
+ return rappCacheService.getRapp(rappId).map(Rapp::getRappInstances).map(ResponseEntity::ok).orElseThrow(
+ () -> new RappHandlerException(HttpStatus.NOT_FOUND, "No instance found for rApp '" + rappId + "'."));
}
@PostMapping
rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
rapp.getRappInstances().put(rappInstance.getRappInstanceId(), rappInstance);
return ResponseEntity.ok(rappInstance);
- }).orElse(ResponseEntity.notFound().build());
+ }).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND, "rApp '" + rappId + "' not found."));
}
@GetMapping("{rapp_instance_id}")
RappInstance rappInstance = rappPair.getLeft().getRappInstances().get(rappInstanceId);
rappInstance.setState(rappInstanceStateMachine.getRappInstanceState(rappInstanceId));
return rappInstance;
- }).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
+ }).map(ResponseEntity::ok).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
+ String.format(RAPP_INSTANCE_NOT_FOUND, rappInstanceId)));
}
@PutMapping("{rapp_instance_id}")
.filter(deployOrder -> deployOrder.equals(DeployOrder.DEPLOY))
.map(primeOrder -> rappService.deployRappInstance(rappPair.getLeft(), rappPair.getRight()))
.orElseGet(() -> rappService.undeployRappInstance(rappPair.getLeft(), rappPair.getRight())))
- .orElse(ResponseEntity.notFound().build());
+ .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
+ String.format(RAPP_INSTANCE_NOT_FOUND, rappId)));
//@formatter:on
}
@DeleteMapping("{rapp_instance_id}")
- public ResponseEntity<Object> deleteRappInstance(@PathVariable("rapp_id") String rappId,
+ public ResponseEntity<String> deleteRappInstance(@PathVariable("rapp_id") String rappId,
@PathVariable("rapp_instance_id") UUID rappInstanceId) {
- return rappCacheService.getRapp(rappId).map(rapp -> Pair.of(rapp, rapp.getRappInstances()))
- .filter(rappPair -> rappPair.getRight().containsKey(rappInstanceId) && rappPair.getRight()
- .get(rappInstanceId)
- .getState()
- .equals(RappInstanceState.UNDEPLOYED))
- .map(rappPair -> {
- rappInstanceStateMachine.deleteRappInstance(
- rappPair.getLeft().getRappInstances().get(rappInstanceId));
- rappPair.getLeft().getRappInstances().remove(rappInstanceId);
- return ResponseEntity.noContent().build();
- }).orElse(ResponseEntity.notFound().build());
+ return rappCacheService.getRapp(rappId).filter(rApp -> rApp.getRappInstances().containsKey(rappInstanceId))
+ .map(rApp -> rappService.deleteRappInstance(rApp, rappInstanceId)).orElseThrow(
+ () -> new RappHandlerException(HttpStatus.NOT_FOUND,
+ String.format(RAPP_INSTANCE_NOT_FOUND, rappId)));
}
}