diff options
author | Lathish <lathishbabu.ganesan@est.tech> | 2020-12-11 14:47:43 +0000 |
---|---|---|
committer | Lathish <lathishbabu.ganesan@est.tech> | 2021-01-07 11:45:00 +0000 |
commit | 157f8cb05752a6bff2b7b9ad192acba4b6def6c3 (patch) | |
tree | b932bfe23ebfb6d0fd6fcd67f68a24219901b987 /a1-policy-management/src | |
parent | d7cc059bfce52da57e8cc12e17280641878bce19 (diff) |
Global Exception Handler
Issue-ID: CCSDK-3054
Change-Id: I0281e616b58bb405b4f79d046af5c30b82753ea0
Signed-off-by: Lathish <lathishbabu.ganesan@est.tech>
Diffstat (limited to 'a1-policy-management/src')
11 files changed, 235 insertions, 171 deletions
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v1/PolicyController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v1/PolicyController.java index e1d60de3..6c5ebd90 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v1/PolicyController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v1/PolicyController.java @@ -39,7 +39,8 @@ import lombok.Getter; import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse; -import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ErrorResponse; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException; import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; @@ -97,38 +98,30 @@ public class PolicyController { @ApiOperation(value = "Returns policy type schema definitions") @ApiResponses(value = { @ApiResponse(code = 200, message = "Policy schemas", response = Object.class, responseContainer = "List"), // - @ApiResponse(code = 404, message = "Near-RT RIC is not found", response = String.class)}) + @ApiResponse(code = 404, message = "Near-RT RIC is not found", response = ErrorResponse.ErrorInfo.class)}) public ResponseEntity<String> getPolicySchemas( // @ApiParam(name = "ric", required = false, value = "The name of the Near-RT RIC to get the definitions for.") // - @RequestParam(name = "ric", required = false) String ricName) { + @RequestParam(name = "ric", required = false) String ricName) throws EntityNotFoundException { if (ricName == null) { Collection<PolicyType> types = this.policyTypes.getAll(); return new ResponseEntity<>(toPolicyTypeSchemasJson(types), HttpStatus.OK); } else { - try { - Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes(); - return new ResponseEntity<>(toPolicyTypeSchemasJson(types), HttpStatus.OK); - } catch (ServiceException e) { - return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND); - } + Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes(); + return new ResponseEntity<>(toPolicyTypeSchemasJson(types), HttpStatus.OK); } } @GetMapping("/policy_schema") @ApiOperation(value = "Returns one policy type schema definition") @ApiResponses(value = { // - @ApiResponse(code = 200, message = "Policy schema", response = Object.class), - @ApiResponse(code = 404, message = "The policy type is not found", response = String.class)}) + @ApiResponse(code = 200, message = "Policy schema", response = Object.class), @ApiResponse(code = 404, + message = "The policy type is not found", response = ErrorResponse.ErrorInfo.class)}) public ResponseEntity<String> getPolicySchema( // @ApiParam(name = "id", required = true, value = "The identity of the policy type to get the definition for.") // - @RequestParam(name = "id", required = true) String id) { - try { - PolicyType type = policyTypes.getType(id); - return new ResponseEntity<>(type.schema(), HttpStatus.OK); - } catch (ServiceException e) { - return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND); - } + @RequestParam(name = "id", required = true) String id) throws EntityNotFoundException { + PolicyType type = policyTypes.getType(id); + return new ResponseEntity<>(type.schema(), HttpStatus.OK); } @GetMapping("/policy_types") @@ -136,20 +129,16 @@ public class PolicyController { @ApiResponses(value = { @ApiResponse(code = 200, message = "Policy type names", response = String.class, responseContainer = "List"), - @ApiResponse(code = 404, message = "Near-RT RIC is not found", response = String.class)}) + @ApiResponse(code = 404, message = "Near-RT RIC is not found", response = ErrorResponse.ErrorInfo.class)}) public ResponseEntity<String> getPolicyTypes( // @ApiParam(name = "ric", required = false, value = "The name of the Near-RT RIC to get types for.") // - @RequestParam(name = "ric", required = false) String ricName) { + @RequestParam(name = "ric", required = false) String ricName) throws EntityNotFoundException { if (ricName == null) { Collection<PolicyType> types = this.policyTypes.getAll(); return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK); } else { - try { - Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes(); - return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK); - } catch (ServiceException e) { - return new ResponseEntity<>(e.toString(), HttpStatus.NOT_FOUND); - } + Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes(); + return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK); } } @@ -157,17 +146,13 @@ public class PolicyController { @ApiOperation(value = "Returns a policy configuration") // @ApiResponses(value = { // @ApiResponse(code = 200, message = "Policy found", response = Object.class), // - @ApiResponse(code = 404, message = "Policy is not found")} // + @ApiResponse(code = 404, message = "Policy is not found", response = ErrorResponse.ErrorInfo.class)} // ) public ResponseEntity<String> getPolicy( // @ApiParam(name = "id", required = true, value = "The identity of the policy instance.") // - @RequestParam(name = "id", required = true) String id) { - try { - Policy p = policies.getPolicy(id); - return new ResponseEntity<>(p.json(), HttpStatus.OK); - } catch (ServiceException e) { - return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND); - } + @RequestParam(name = "id", required = true) String id) throws EntityNotFoundException { + Policy p = policies.getPolicy(id); + return new ResponseEntity<>(p.json(), HttpStatus.OK); } @DeleteMapping("/policy") @@ -179,23 +164,19 @@ public class PolicyController { @ApiResponse(code = 423, message = "Near-RT RIC is not operational", response = String.class)}) public Mono<ResponseEntity<Object>> deletePolicy( // @ApiParam(name = "id", required = true, value = "The identity of the policy instance.") // - @RequestParam(name = "id", required = true) String id) { - try { - Policy policy = policies.getPolicy(id); - keepServiceAlive(policy.ownerServiceId()); - Ric ric = policy.ric(); - return ric.getLock().lock(LockType.SHARED) // - .flatMap(notUsed -> assertRicStateIdle(ric)) // - .flatMap(notUsed -> a1ClientFactory.createA1Client(policy.ric())) // - .doOnNext(notUsed -> policies.remove(policy)) // - .flatMap(client -> client.deletePolicy(policy)) // - .doOnNext(notUsed -> ric.getLock().unlockBlocking()) // - .doOnError(notUsed -> ric.getLock().unlockBlocking()) // - .flatMap(notUsed -> Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT))) - .onErrorResume(this::handleException); - } catch (ServiceException e) { - return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND)); - } + @RequestParam(name = "id", required = true) String id) throws EntityNotFoundException { + Policy policy = policies.getPolicy(id); + keepServiceAlive(policy.ownerServiceId()); + Ric ric = policy.ric(); + return ric.getLock().lock(LockType.SHARED) // + .flatMap(notUsed -> assertRicStateIdle(ric)) // + .flatMap(notUsed -> a1ClientFactory.createA1Client(policy.ric())) // + .doOnNext(notUsed -> policies.remove(policy)) // + .flatMap(client -> client.deletePolicy(policy)) // + .doOnNext(notUsed -> ric.getLock().unlockBlocking()) // + .doOnError(notUsed -> ric.getLock().unlockBlocking()) // + .flatMap(notUsed -> Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT))) + .onErrorResume(this::handleException); } @PutMapping(path = "/policy") @@ -364,17 +345,14 @@ public class PolicyController { ) public Mono<ResponseEntity<String>> getPolicyStatus( // @ApiParam(name = "id", required = true, value = "The identity of the policy.") @RequestParam(name = "id", // - required = true) String id) { - try { - Policy policy = policies.getPolicy(id); - - return a1ClientFactory.createA1Client(policy.ric()) // - .flatMap(client -> client.getPolicyStatus(policy)) // - .flatMap(status -> Mono.just(new ResponseEntity<>(status, HttpStatus.OK))) - .onErrorResume(this::handleException); - } catch (ServiceException e) { - return Mono.just(new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND)); - } + required = true) String id) + throws EntityNotFoundException { + Policy policy = policies.getPolicy(id); + + return a1ClientFactory.createA1Client(policy.ric()) // + .flatMap(client -> client.getPolicyStatus(policy)) // + .flatMap(status -> Mono.just(new ResponseEntity<>(status, HttpStatus.OK))) + .onErrorResume(this::handleException); } private void keepServiceAlive(String name) { diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v1/RicRepositoryController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v1/RicRepositoryController.java index 4e57ada8..224935e3 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v1/RicRepositoryController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v1/RicRepositoryController.java @@ -31,8 +31,8 @@ import io.swagger.annotations.ApiResponses; import java.util.ArrayList; import java.util.List; -import java.util.Optional; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException; import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; @@ -58,6 +58,8 @@ public class RicRepositoryController { /** * Example: http://localhost:8081/rics?managedElementId=kista_1 + * + * @throws EntityNotFoundException */ @GetMapping("/ric") @ApiOperation(value = "Returns the name of a RIC managing one Mananged Element") @@ -67,14 +69,10 @@ public class RicRepositoryController { }) public ResponseEntity<String> getRic( // @ApiParam(name = "managedElementId", required = true, value = "The identity of the Managed Element") // - @RequestParam(name = "managedElementId", required = true) String managedElementId) { - Optional<Ric> ric = this.rics.lookupRicForManagedElement(managedElementId); - - if (ric.isPresent()) { - return new ResponseEntity<>(ric.get().id(), HttpStatus.OK); - } else { - return new ResponseEntity<>("No RIC found", HttpStatus.NOT_FOUND); - } + @RequestParam(name = "managedElementId", required = true) String managedElementId) + throws EntityNotFoundException { + Ric ric = this.rics.lookupRicForManagedElement(managedElementId); + return new ResponseEntity<>(ric.id(), HttpStatus.OK); } /** diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ErrorResponse.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ErrorResponse.java index 988fbc2a..da321655 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ErrorResponse.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ErrorResponse.java @@ -33,7 +33,7 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import reactor.core.publisher.Mono; -class ErrorResponse { +public class ErrorResponse { private static Gson gson = new GsonBuilder() // .create(); // @@ -97,7 +97,7 @@ class ErrorResponse { return new ResponseEntity<>(json, headers, code); } - static ResponseEntity<Object> create(Exception e, HttpStatus code) { + public static ResponseEntity<Object> create(Exception e, HttpStatus code) { return create(e.toString(), code); } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/PolicyController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/PolicyController.java index ea047cb5..850635f6 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/PolicyController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/PolicyController.java @@ -39,7 +39,7 @@ import lombok.Getter; import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse; -import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException; import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies; @@ -104,14 +104,10 @@ public class PolicyController { @ApiResponse(code = 200, message = "Policy type", response = PolicyTypeInfo.class), // @ApiResponse(code = 404, message = "Policy type is not found", response = ErrorResponse.ErrorInfo.class)}) public ResponseEntity<Object> getPolicyType( // - @PathVariable("policytype_id") String policyTypeId) { - try { - PolicyType type = policyTypes.getType(policyTypeId); - PolicyTypeInfo info = new PolicyTypeInfo(type.schema()); - return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK); - } catch (ServiceException e) { - return ErrorResponse.create(e, HttpStatus.NOT_FOUND); - } + @PathVariable("policytype_id") String policyTypeId) throws EntityNotFoundException { + PolicyType type = policyTypes.getType(policyTypeId); + PolicyTypeInfo info = new PolicyTypeInfo(type.schema()); + return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK); } @GetMapping(path = Consts.V2_API_ROOT + "/policy-types", produces = MediaType.APPLICATION_JSON_VALUE) @@ -121,17 +117,13 @@ public class PolicyController { public ResponseEntity<Object> getPolicyTypes( // @ApiParam(name = Consts.RIC_ID_PARAM, required = false, value = "The identity of the Near-RT RIC to get types for.") // - @RequestParam(name = Consts.RIC_ID_PARAM, required = false) String ricId) { + @RequestParam(name = Consts.RIC_ID_PARAM, required = false) String ricId) throws EntityNotFoundException { if (ricId == null) { Collection<PolicyType> types = this.policyTypes.getAll(); return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK); } else { - try { - Collection<PolicyType> types = rics.getRic(ricId).getSupportedPolicyTypes(); - return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK); - } catch (ServiceException e) { - return ErrorResponse.create(e, HttpStatus.NOT_FOUND); - } + Collection<PolicyType> types = rics.getRic(ricId).getSupportedPolicyTypes(); + return new ResponseEntity<>(toPolicyTypeIdsJson(types), HttpStatus.OK); } } @@ -142,13 +134,9 @@ public class PolicyController { @ApiResponse(code = 404, message = "Policy is not found", response = ErrorResponse.ErrorInfo.class)} // ) public ResponseEntity<Object> getPolicy( // - @PathVariable(name = Consts.POLICY_ID_PARAM, required = true) String id) { - try { - Policy p = policies.getPolicy(id); - return new ResponseEntity<>(gson.toJson(toPolicyInfo(p)), HttpStatus.OK); - } catch (ServiceException e) { - return ErrorResponse.create(e, HttpStatus.NOT_FOUND); - } + @PathVariable(name = Consts.POLICY_ID_PARAM, required = true) String id) throws EntityNotFoundException { + Policy p = policies.getPolicy(id); + return new ResponseEntity<>(gson.toJson(toPolicyInfo(p)), HttpStatus.OK); } @DeleteMapping(Consts.V2_API_ROOT + "/policies/{policy_id:.+}") @@ -160,23 +148,19 @@ public class PolicyController { @ApiResponse(code = 423, message = "Near-RT RIC is not operational", response = ErrorResponse.ErrorInfo.class)}) public Mono<ResponseEntity<Object>> deletePolicy( // - @PathVariable(Consts.POLICY_ID_PARAM) String policyId) { - try { - Policy policy = policies.getPolicy(policyId); - keepServiceAlive(policy.ownerServiceId()); - Ric ric = policy.ric(); - return ric.getLock().lock(LockType.SHARED) // - .flatMap(notUsed -> assertRicStateIdle(ric)) // - .flatMap(notUsed -> a1ClientFactory.createA1Client(policy.ric())) // - .doOnNext(notUsed -> policies.remove(policy)) // - .flatMap(client -> client.deletePolicy(policy)) // - .doOnNext(notUsed -> ric.getLock().unlockBlocking()) // - .doOnError(notUsed -> ric.getLock().unlockBlocking()) // - .flatMap(notUsed -> Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT))) - .onErrorResume(this::handleException); - } catch (ServiceException e) { - return ErrorResponse.createMono(e, HttpStatus.NOT_FOUND); - } + @PathVariable(Consts.POLICY_ID_PARAM) String policyId) throws EntityNotFoundException { + Policy policy = policies.getPolicy(policyId); + keepServiceAlive(policy.ownerServiceId()); + Ric ric = policy.ric(); + return ric.getLock().lock(LockType.SHARED) // + .flatMap(notUsed -> assertRicStateIdle(ric)) // + .flatMap(notUsed -> a1ClientFactory.createA1Client(policy.ric())) // + .doOnNext(notUsed -> policies.remove(policy)) // + .flatMap(client -> client.deletePolicy(policy)) // + .doOnNext(notUsed -> ric.getLock().unlockBlocking()) // + .doOnError(notUsed -> ric.getLock().unlockBlocking()) // + .flatMap(notUsed -> Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT))) + .onErrorResume(this::handleException); } @PutMapping(path = Consts.V2_API_ROOT + "/policies", produces = MediaType.APPLICATION_JSON_VALUE) @@ -189,7 +173,7 @@ public class PolicyController { @ApiResponse(code = 404, message = "Near-RT RIC or policy type is not found", response = ErrorResponse.ErrorInfo.class) // }) - public Mono<ResponseEntity<Object>> putPolicy(@RequestBody PolicyInfo policyInfo) { + public Mono<ResponseEntity<Object>> putPolicy(@RequestBody PolicyInfo policyInfo) throws EntityNotFoundException { if (!policyInfo.validate()) { return ErrorResponse.createMono("Missing required parameter in body", HttpStatus.BAD_REQUEST); @@ -199,7 +183,7 @@ public class PolicyController { PolicyType type = policyTypes.get(policyInfo.policyTypeId); keepServiceAlive(policyInfo.serviceId); if (ric == null || type == null) { - return ErrorResponse.createMono("Near-RT RIC or policy type not found", HttpStatus.NOT_FOUND); + throw new EntityNotFoundException("Near-RT RIC or policy type not found"); } Policy policy = ImmutablePolicy.builder() // .id(policyInfo.policyId) // @@ -293,13 +277,14 @@ public class PolicyController { @RequestParam(name = Consts.RIC_ID_PARAM, required = false) String ric, // @ApiParam(name = Consts.SERVICE_ID_PARAM, required = false, value = "The identity of the service to get policies for.") // - @RequestParam(name = Consts.SERVICE_ID_PARAM, required = false) String service) // + @RequestParam(name = Consts.SERVICE_ID_PARAM, required = false) String service) + throws EntityNotFoundException // { if ((type != null && this.policyTypes.get(type) == null)) { - return ErrorResponse.create("Policy type not found", HttpStatus.NOT_FOUND); + throw new EntityNotFoundException("Policy type not found"); } if ((ric != null && this.rics.get(ric) == null)) { - return ErrorResponse.create("Near-RT RIC not found", HttpStatus.NOT_FOUND); + throw new EntityNotFoundException("Near-RT RIC not found"); } String filteredPolicies = policiesToJson(filter(type, ric, service)); @@ -320,13 +305,14 @@ public class PolicyController { @RequestParam(name = Consts.RIC_ID_PARAM, required = false) String ricId, // @ApiParam(name = Consts.SERVICE_ID_PARAM, required = false, value = "The identity of the service to get policies for.") // - @RequestParam(name = Consts.SERVICE_ID_PARAM, required = false) String serviceId) // + @RequestParam(name = Consts.SERVICE_ID_PARAM, required = false) String serviceId) + throws EntityNotFoundException // { if ((policyTypeId != null && this.policyTypes.get(policyTypeId) == null)) { - return ErrorResponse.create("Policy type not found", HttpStatus.NOT_FOUND); + throw new EntityNotFoundException("Policy type not found"); } if ((ricId != null && this.rics.get(ricId) == null)) { - return ErrorResponse.create("Near-RT RIC not found", HttpStatus.NOT_FOUND); + throw new EntityNotFoundException("Near-RT RIC not found"); } String policyIdsJson = toPolicyIdsJson(filter(policyTypeId, ricId, serviceId)); @@ -340,23 +326,20 @@ public class PolicyController { @ApiResponse(code = 404, message = "Policy is not found", response = ErrorResponse.ErrorInfo.class)} // ) public Mono<ResponseEntity<Object>> getPolicyStatus( // - @PathVariable(Consts.POLICY_ID_PARAM) String policyId) { - try { - Policy policy = policies.getPolicy(policyId); - - return a1ClientFactory.createA1Client(policy.ric()) // - .flatMap(client -> client.getPolicyStatus(policy).onErrorResume(e -> Mono.just("{}"))) // - .flatMap(status -> createPolicyStatus(policy, status)) // - .onErrorResume(this::handleException); - } catch (ServiceException e) { - return ErrorResponse.createMono(e, HttpStatus.NOT_FOUND); - } + @PathVariable(Consts.POLICY_ID_PARAM) String policyId) throws EntityNotFoundException { + Policy policy = policies.getPolicy(policyId); + + return a1ClientFactory.createA1Client(policy.ric()) // + .flatMap(client -> client.getPolicyStatus(policy).onErrorResume(e -> Mono.just("{}"))) // + .flatMap(status -> createPolicyStatus(policy, status)) // + .onErrorResume(this::handleException); + } private Mono<ResponseEntity<Object>> createPolicyStatus(Policy policy, String statusFromNearRic) { PolicyStatusInfo info = new PolicyStatusInfo(policy.lastModified(), fromJson(statusFromNearRic)); String str = gson.toJson(info); - return Mono.just(new ResponseEntity<>((Object) str, HttpStatus.OK)); + return Mono.just(new ResponseEntity<>(str, HttpStatus.OK)); } private void keepServiceAlive(String name) { diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/RicRepositoryController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/RicRepositoryController.java index a77a7e7e..f07efa2c 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/RicRepositoryController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/RicRepositoryController.java @@ -31,9 +31,9 @@ import io.swagger.annotations.ApiResponses; import java.util.ArrayList; import java.util.List; -import java.util.Optional; -import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.InvalidRequestException; import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric; import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics; @@ -65,6 +65,8 @@ public class RicRepositoryController { /** * Example: http://localhost:8081/v2/rics/ric?managed_element_id=kista_1 + * + * @throws EntityNotFoundException */ @GetMapping(path = Consts.V2_API_ROOT + "/rics/ric", produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = GET_RIC_BRIEF, notes = GET_RIC_DETAILS) @@ -78,25 +80,18 @@ public class RicRepositoryController { @RequestParam(name = Consts.MANAGED_ELEMENT_ID_PARAM, required = false) String managedElementId, @ApiParam(name = Consts.RIC_ID_PARAM, required = false, value = "The identity of a Near-RT RIC to get information for.") // - @RequestParam(name = Consts.RIC_ID_PARAM, required = false) String ricId) { - try { - if (managedElementId != null && ricId != null) { - return ErrorResponse.create("Give one query parameter", HttpStatus.BAD_REQUEST); - } else if (managedElementId != null) { - Optional<Ric> ric = this.rics.lookupRicForManagedElement(managedElementId); - if (ric.isPresent()) { - return new ResponseEntity<>(gson.toJson(toRicInfo(ric.get())), HttpStatus.OK); - } else { - return ErrorResponse.create("No Near-RT RIC managing the ME is found", HttpStatus.NOT_FOUND); - } - } else if (ricId != null) { - RicInfo info = toRicInfo(this.rics.getRic(ricId)); - return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK); - } else { - return ErrorResponse.create("Give one query parameter", HttpStatus.BAD_REQUEST); - } - } catch (ServiceException e) { - return ErrorResponse.create(e, HttpStatus.NOT_FOUND); + @RequestParam(name = Consts.RIC_ID_PARAM, required = false) String ricId) + throws EntityNotFoundException, InvalidRequestException { + if (managedElementId != null && ricId != null) { + throw new InvalidRequestException("Give one query parameter"); + } else if (managedElementId != null) { + Ric ric = this.rics.lookupRicForManagedElement(managedElementId); + return new ResponseEntity<>(gson.toJson(toRicInfo(ric)), HttpStatus.OK); + } else if (ricId != null) { + RicInfo info = toRicInfo(this.rics.getRic(ricId)); + return new ResponseEntity<>(gson.toJson(info), HttpStatus.OK); + } else { + throw new InvalidRequestException("Give one query parameter"); } } @@ -105,6 +100,7 @@ public class RicRepositoryController { /** * @return a Json array of all RIC data Example: http://localhost:8081/v2/ric + * @throws EntityNotFoundException */ @GetMapping(path = Consts.V2_API_ROOT + "/rics", produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = "Query Near-RT RIC information", notes = QUERY_RIC_INFO_DETAILS) @@ -114,11 +110,10 @@ public class RicRepositoryController { public ResponseEntity<Object> getRics( // @ApiParam(name = Consts.POLICY_TYPE_ID_PARAM, required = false, value = "The identity of a policy type. If given, all Near-RT RICs supporteing the policy type are returned") // - @RequestParam(name = Consts.POLICY_TYPE_ID_PARAM, required = false) String supportingPolicyType - - ) { + @RequestParam(name = Consts.POLICY_TYPE_ID_PARAM, required = false) String supportingPolicyType) + throws EntityNotFoundException { if ((supportingPolicyType != null) && (this.types.get(supportingPolicyType) == null)) { - return ErrorResponse.create("Policy type not found", HttpStatus.NOT_FOUND); + throw new EntityNotFoundException("Policy type not found"); } List<RicInfo> result = new ArrayList<>(); diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/EntityNotFoundException.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/EntityNotFoundException.java new file mode 100644 index 00000000..ee9b7bf2 --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/EntityNotFoundException.java @@ -0,0 +1,34 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2019-2020 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.exceptions; + +public class EntityNotFoundException extends ServiceException { + + private static final long serialVersionUID = 1L; + + public EntityNotFoundException(String message) { + super(message); + } + + public EntityNotFoundException(String message, Exception originalException) { + super(message, originalException); + } +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/GlobalExceptionHandler.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/GlobalExceptionHandler.java new file mode 100644 index 00000000..9d20d33f --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/GlobalExceptionHandler.java @@ -0,0 +1,43 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2019-2020 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.exceptions; + +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ErrorResponse; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@ControllerAdvice(annotations = RestController.class) +public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { + + @ExceptionHandler(EntityNotFoundException.class) + public final ResponseEntity<Object> handleNotFoundException(EntityNotFoundException ex) { + return ErrorResponse.create(ex, HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(InvalidRequestException.class) + public final ResponseEntity<Object> handleInvalidRequestException(InvalidRequestException ex) { + return ErrorResponse.create(ex, HttpStatus.BAD_REQUEST); + } +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/InvalidRequestException.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/InvalidRequestException.java new file mode 100644 index 00000000..6ffd216b --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/InvalidRequestException.java @@ -0,0 +1,34 @@ +/*- + * ========================LICENSE_START================================= + * ONAP : ccsdk oran + * ====================================================================== + * Copyright (C) 2019-2020 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ========================LICENSE_END=================================== + */ + +package org.onap.ccsdk.oran.a1policymanagementservice.exceptions; + +public class InvalidRequestException extends ServiceException { + + private static final long serialVersionUID = 1L; + + public InvalidRequestException(String message) { + super(message); + } + + public InvalidRequestException(String message, Exception originalException) { + super(message, originalException); + } +} diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Policies.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Policies.java index 4df2504b..882d3368 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Policies.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Policies.java @@ -27,7 +27,7 @@ import java.util.Map; import java.util.Set; import java.util.Vector; -import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException; public class Policies { private Map<String, Policy> policiesId = new HashMap<>(); @@ -72,10 +72,10 @@ public class Policies { return policiesId.get(id); } - public synchronized Policy getPolicy(String id) throws ServiceException { + public synchronized Policy getPolicy(String id) throws EntityNotFoundException { Policy p = policiesId.get(id); if (p == null) { - throw new ServiceException("Could not find policy: " + id); + throw new EntityNotFoundException("Could not find policy: " + id); } return p; } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java index db25d9cc..7bf03782 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/PolicyTypes.java @@ -25,15 +25,15 @@ import java.util.HashMap; import java.util.Map; import java.util.Vector; -import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException; public class PolicyTypes { private Map<String, PolicyType> types = new HashMap<>(); - public synchronized PolicyType getType(String name) throws ServiceException { + public synchronized PolicyType getType(String name) throws EntityNotFoundException { PolicyType t = types.get(name); if (t == null) { - throw new ServiceException("Could not find type: " + name); + throw new EntityNotFoundException("Could not find type: " + name); } return t; } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Rics.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Rics.java index b6ec7491..e4918e76 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Rics.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Rics.java @@ -23,10 +23,9 @@ package org.onap.ccsdk.oran.a1policymanagementservice.repository; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.Optional; import java.util.Vector; -import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException; import org.springframework.lang.Nullable; /** @@ -43,10 +42,10 @@ public class Rics { return new Vector<>(registeredRics.values()); } - public synchronized Ric getRic(String ricId) throws ServiceException { + public synchronized Ric getRic(String ricId) throws EntityNotFoundException { Ric ric = registeredRics.get(ricId); if (ric == null) { - throw new ServiceException("Could not find ric: " + ricId); + throw new EntityNotFoundException("Could not find ric: " + ricId); } return ric; } @@ -67,12 +66,12 @@ public class Rics { this.registeredRics.clear(); } - public synchronized Optional<Ric> lookupRicForManagedElement(String managedElementId) { + public synchronized Ric lookupRicForManagedElement(String managedElementId) throws EntityNotFoundException { for (Ric ric : this.registeredRics.values()) { if (ric.getManagedElementIds().contains(managedElementId)) { - return Optional.of(ric); + return ric; } } - return Optional.empty(); + throw new EntityNotFoundException("No Near-RT RIC managing the ME is found"); } } |