summary |
shortlog |
log |
commit | commitdiff |
review |
tree
raw |
patch |
inline | side by side (from parent 1:
c128f98)
Include messages on the http response in the controller.
Issue-ID: NONRTRIC-905
Signed-off-by: aravind.est <aravindhan.a@est.tech>
Change-Id: Icd0e5079a7f3eda38da725fff4d3e75bf41dcce1
import com.oransc.rappmanager.configuration.RappManagerConfiguration;
import com.oransc.rappmanager.models.cache.RappCacheService;
import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
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 com.oransc.rappmanager.models.rapp.PrimeOrder;
import com.oransc.rappmanager.models.rapp.Rapp;
import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@GetMapping("{rapp_id}")
public ResponseEntity<Rapp> getRapp(@PathVariable("rapp_id") String rappId) {
@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, "rApp '" + rappId + "' not found."));
}
@PostMapping("{rapp_id}")
}
@PostMapping("{rapp_id}")
return ResponseEntity.accepted().build();
} else {
logger.info("Invalid Rapp package for {}", rappId);
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)))
.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,
+ "rApp '" + rappId + "' not found."));
rappCacheService.deleteRapp(rapp);
return ResponseEntity.ok().build();
})
rappCacheService.deleteRapp(rapp);
return ResponseEntity.ok().build();
})
- .orElse(ResponseEntity.notFound().build());
+ .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
+ "rApp '" + rappId + "' not found."));
package com.oransc.rappmanager.rest;
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.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.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 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 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;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@GetMapping
public ResponseEntity<Map<UUID, RappInstance>> getAllRappInstances(@PathVariable("rapp_id") String rappId) {
@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 + "'."));
rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
rapp.getRappInstances().put(rappInstance.getRappInstanceId(), rappInstance);
return ResponseEntity.ok(rappInstance);
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}")
}
@GetMapping("{rapp_instance_id}")
RappInstance rappInstance = rappPair.getLeft().getRappInstances().get(rappInstanceId);
rappInstance.setState(rappInstanceStateMachine.getRappInstanceState(rappInstanceId));
return rappInstance;
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,
+ "No instance found for rApp '" + rappId + "'."));
}
@PutMapping("{rapp_instance_id}")
}
@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())))
.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,
+ "rApp instance '" + rappInstanceId + "' not found."));
rappPair.getLeft().getRappInstances().get(rappInstanceId));
rappPair.getLeft().getRappInstances().remove(rappInstanceId);
return ResponseEntity.noContent().build();
rappPair.getLeft().getRappInstances().get(rappInstanceId));
rappPair.getLeft().getRappInstances().remove(rappInstanceId);
return ResponseEntity.noContent().build();
- }).orElse(ResponseEntity.notFound().build());
+ }).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
+ "rApp instance '" + rappInstanceId + "' not found."));
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oransc.rappmanager.acm.service.AcmDeployer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oransc.rappmanager.acm.service.AcmDeployer;
+import com.oransc.rappmanager.models.cache.RappCacheService;
import com.oransc.rappmanager.models.rapp.PrimeOrder;
import com.oransc.rappmanager.models.rapp.Rapp;
import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
import com.oransc.rappmanager.models.rapp.RappState;
import com.oransc.rappmanager.models.rapp.PrimeOrder;
import com.oransc.rappmanager.models.rapp.Rapp;
import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
import com.oransc.rappmanager.models.rapp.RappState;
-import com.oransc.rappmanager.models.cache.RappCacheService;
import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
import java.io.File;
import java.io.FileInputStream;
import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
import java.io.File;
import java.io.FileInputStream;
@Test
void testGetInvalidRapp() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", UUID.randomUUID()))
@Test
void testGetInvalidRapp() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", UUID.randomUUID()))
- .andExpect(status().isBadRequest());
+ .andExpect(status().isNotFound());
--- /dev/null
+package com.oransc.rappmanager.models.exception;
+
+import lombok.Value;
+
+@Value
+public class ErrorResponse {
+
+ String message;
+}
--- /dev/null
+package com.oransc.rappmanager.models.exception;
+
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+@RestControllerAdvice
+public class ExceptionControllerHandler {
+
+ @ExceptionHandler(RappHandlerException.class)
+ public ResponseEntity<ErrorResponse> handleRappHandlerException(RappHandlerException rappHandlerException) {
+ return ResponseEntity.status(rappHandlerException.getStatusCode())
+ .body(new ErrorResponse(rappHandlerException.getMessage()));
+ }
+}
--- /dev/null
+package com.oransc.rappmanager.models.exception;
+
+import lombok.Getter;
+import org.springframework.http.HttpStatusCode;
+
+@Getter
+public class RappHandlerException extends RuntimeException {
+
+ private final HttpStatusCode statusCode;
+
+ public RappHandlerException(HttpStatusCode statusCode, String message) {
+ super(message);
+ this.statusCode = statusCode;
+
+ }
+
+}