From 9dcf49ce143dc318442568dd3bb1250e453b437a Mon Sep 17 00:00:00 2001 From: "raviteja.karumuri" Date: Thu, 14 Sep 2023 12:38:49 +0100 Subject: Adding Schema reference in the API definition instead of declaring Object Type Issue-ID: CCSDK-3937 Signed-off-by: raviteja.karumuri Change-Id: Ic301c785a39a0f6d0e5057e67dc592eda01a3d0b --- .../controllers/v2/ConfigurationController.java | 16 ++-- .../controllers/v2/ErrorResponse.java | 2 +- .../controllers/v2/PolicyController.java | 98 +++++++++------------- .../controllers/v2/RicRepositoryController.java | 12 +-- .../controllers/v2/ServiceController.java | 15 ++-- .../controllers/v2/StatusController.java | 16 +--- .../exceptions/GlobalExceptionHandler.java | 11 ++- .../repository/Services.java | 3 +- 8 files changed, 71 insertions(+), 102 deletions(-) (limited to 'a1-policy-management/src/main/java') diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java index 6bf6fbaf..4b0d6b58 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java @@ -27,6 +27,7 @@ import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationCo import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v2.ConfigurationApi; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -78,27 +79,20 @@ public class ConfigurationController implements ConfigurationApi { logger.warn("Configuration file not written, {}.", ioe.getMessage()); return ErrorResponse.createMono("Internal error when writing the configuration.", HttpStatus.INTERNAL_SERVER_ERROR); - } catch (Exception e) { + } catch (ServiceException e) { return ErrorResponse.createMono(e, HttpStatus.BAD_REQUEST); } }) - .onErrorResume(error -> { - return ErrorResponse.createMono(error.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); - }); + .doOnError(error -> logger.error(error.getMessage())); } @Override - public Mono> getConfiguration(final ServerWebExchange exchange) { - try { + public Mono> getConfiguration(final ServerWebExchange exchange) throws ServiceException { Optional rootObject = configurationFile.readFile(); if (rootObject.isPresent()) { return Mono.just(new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK)); } else { - return ErrorResponse.createMono("File does not exist", HttpStatus.NOT_FOUND); + throw new ServiceException("File does not exist", HttpStatus.NOT_FOUND); } - } catch (Exception e) { - return ErrorResponse.createMono(e, HttpStatus.INTERNAL_SERVER_ERROR); - } } - } 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 e79a8217..d4f3bbfb 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 @@ -96,7 +96,7 @@ public class ErrorResponse { return createMono(e.toString(), code); } - static ResponseEntity create(String text, HttpStatusCode code) { + public static ResponseEntity create(String text, HttpStatusCode code) { logger.debug("Error response: {}, {}", code, text); ErrorInfo p = new ErrorInfo(text, code.value()); String json = gson.toJson(p); 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 23dcba71..53cf62a2 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 @@ -33,7 +33,6 @@ import java.time.Instant; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.Map; import lombok.Getter; @@ -105,7 +104,7 @@ public class PolicyController implements A1PolicyManagementApi { .create(); // @Override - public Mono> getPolicyTypeDefinition(String policyTypeId, ServerWebExchange exchange) + public Mono> getPolicyTypeDefinition(String policyTypeId, ServerWebExchange exchange) throws EntityNotFoundException, JsonProcessingException { PolicyType type = policyTypes.getType(policyTypeId); JsonNode node = objectMapper.readTree(type.getSchema()); @@ -114,7 +113,7 @@ public class PolicyController implements A1PolicyManagementApi { } @Override - public Mono> getPolicyTypes(String ricId, String typeName, String compatibleWithVersion, ServerWebExchange exchange) throws Exception { + public Mono> getPolicyTypes(String ricId, String typeName, String compatibleWithVersion, ServerWebExchange exchange) throws Exception { if (compatibleWithVersion != null && typeName == null) { throw new ServiceException("Parameter " + Consts.COMPATIBLE_WITH_VERSION_PARAM + " can only be used when " + Consts.TYPE_NAME_PARAM + " is given", HttpStatus.BAD_REQUEST); @@ -129,12 +128,12 @@ public class PolicyController implements A1PolicyManagementApi { @Override - public Mono> getPolicy(String policyId, final ServerWebExchange exchange) + public Mono> getPolicy(String policyId, final ServerWebExchange exchange) throws EntityNotFoundException { Policy policy = policies.getPolicy(policyId); return authorization.doAccessControl(exchange.getRequest().getHeaders().toSingleValueMap(), policy, AccessType.READ) // - .map(x -> new ResponseEntity<>((Object) toPolicyInfo(policy), HttpStatus.OK)) // - .onErrorResume(this::handleException); + .map(x -> new ResponseEntity<>(toPolicyInfo(policy), HttpStatus.OK)) // + .doOnError(error -> logger.error(error.getMessage())); } @Override @@ -173,7 +172,7 @@ public class PolicyController implements A1PolicyManagementApi { .flatMap(tuple -> { Ric ric = tuple.getT1(); PolicyType type = tuple.getT2(); - + keepServiceAlive(policyInfoValue.getServiceId()); Policy policy = Policy.builder() .id(policyInfoValue.getPolicyId()) .json(jsonString) @@ -265,7 +264,7 @@ public class PolicyController implements A1PolicyManagementApi { } @Override - public Mono> getPolicyInstances(String policyTypeId, String ricId, String serviceId, String typeName, ServerWebExchange exchange) throws Exception { + public Mono> getPolicyInstances(String policyTypeId, String ricId, String serviceId, String typeName, ServerWebExchange exchange) throws Exception { if ((policyTypeId != null && this.policyTypes.get(policyTypeId) == null)) { throw new EntityNotFoundException("Policy type identity not found"); } @@ -279,12 +278,12 @@ public class PolicyController implements A1PolicyManagementApi { .doOnError(e -> logger.debug("Unauthorized to read policy: {}", e.getMessage())) .onErrorResume(e -> Mono.empty()) .collectList() - .map(authPolicies -> new ResponseEntity<>((Object) policiesToJson(authPolicies), HttpStatus.OK)) - .onErrorResume(this::handleException); + .map(authPolicies -> new ResponseEntity<>(policiesToJson(authPolicies), HttpStatus.OK)) + .doOnError(error -> logger.error(error.getMessage())); } @Override - public Mono> getPolicyIds(String policyTypeId, String ricId, String serviceId, String typeName, ServerWebExchange exchange) throws Exception { + public Mono> getPolicyIds(String policyTypeId, String ricId, String serviceId, String typeName, ServerWebExchange exchange) throws Exception { if ((policyTypeId != null && this.policyTypes.get(policyTypeId) == null)) { throw new EntityNotFoundException("Policy type not found"); } @@ -298,32 +297,27 @@ public class PolicyController implements A1PolicyManagementApi { .doOnError(e -> logger.debug("Unauthorized to read policy: {}", e.getMessage())) .onErrorResume(e -> Mono.empty()) .collectList() - .map(authPolicies -> new ResponseEntity<>((Object)toPolicyIdsJson(authPolicies), HttpStatus.OK)) - .onErrorResume(this::handleException); + .map(authPolicies -> new ResponseEntity<>(toPolicyIdsJson(authPolicies), HttpStatus.OK)) + .doOnError(error -> logger.error(error.getMessage())); } @Override - public Mono> getPolicyStatus(String policyId, ServerWebExchange exchange) throws Exception { + public Mono> getPolicyStatus(String policyId, ServerWebExchange exchange) throws Exception { Policy policy = policies.getPolicy(policyId); return authorization.doAccessControl(exchange.getRequest().getHeaders().toSingleValueMap(), policy, AccessType.READ) // .flatMap(notUsed -> a1ClientFactory.createA1Client(policy.getRic())) // .flatMap(client -> client.getPolicyStatus(policy).onErrorResume(e -> Mono.just("{}"))) // .flatMap(status -> createPolicyStatus(policy, status)) - .onErrorResume(this::handleException); + .doOnError(error -> logger.error(error.getMessage())); } - private Mono> createPolicyStatus(Policy policy, String statusFromNearRic) { + private Mono> createPolicyStatus(Policy policy, String statusFromNearRic) { - try { PolicyStatusInfo policyStatusInfo = new PolicyStatusInfo(); policyStatusInfo.setLastModified(policy.getLastModified().toString()); policyStatusInfo.setStatus(fromJson(statusFromNearRic)); - String policyStatusInfoAsString = objectMapper.writeValueAsString(policyStatusInfo); - return Mono.just(new ResponseEntity<>(policyStatusInfoAsString, HttpStatus.OK)); - } catch (JsonProcessingException ex) { - throw new RuntimeException(ex); - } + return Mono.just(new ResponseEntity<>(policyStatusInfo, HttpStatus.OK)); } private void keepServiceAlive(String name) { @@ -334,69 +328,57 @@ public class PolicyController implements A1PolicyManagementApi { } private PolicyInfo toPolicyInfo(Policy policy) { - PolicyInfo policyInfo = new PolicyInfo() - .policyId(policy.getId()) - .policyData(gson.fromJson(policy.getJson(), Map.class)) - .ricId(policy.getRic().id()) - .policytypeId(policy.getType().getId()) - .serviceId(policy.getOwnerServiceId()) - ._transient(policy.isTransient()); - if (!policy.getStatusNotificationUri().isEmpty()) { - policyInfo.setStatusNotificationUri(policy.getStatusNotificationUri()); - } - return policyInfo; - } - - private String toPolicyInfoString(Policy policy) { - - try { - return objectMapper.writeValueAsString(toPolicyInfo(policy)); - } catch (JsonProcessingException ex) { - throw new RuntimeException(ex); - } + try { + PolicyInfo policyInfo = new PolicyInfo() + .policyId(policy.getId()) + .policyData(objectMapper.readTree(policy.getJson())) + .ricId(policy.getRic().id()) + .policytypeId(policy.getType().getId()) + .serviceId(policy.getOwnerServiceId()) + ._transient(policy.isTransient()); + if (!policy.getStatusNotificationUri().isEmpty()) { + policyInfo.setStatusNotificationUri(policy.getStatusNotificationUri()); + } + return policyInfo; + } catch (JsonProcessingException ex) { + throw new RuntimeException(ex); + } } - private String policiesToJson(Collection policies) { + private PolicyInfoList policiesToJson(Collection policies) { - try { List policiesList = new ArrayList<>(policies.size()); PolicyInfoList policyInfoList = new PolicyInfoList(); for (Policy policy : policies) { policiesList.add(toPolicyInfo(policy)); } policyInfoList.setPolicies(policiesList); - return objectMapper.writeValueAsString(policyInfoList); - } catch(JsonProcessingException ex) { - throw new RuntimeException(ex); - } + return policyInfoList; } private Object fromJson(String jsonStr) { return gson.fromJson(jsonStr, Object.class); } - private String toPolicyTypeIdsJson(Collection policyTypes) throws JsonProcessingException { + private PolicyTypeIdList toPolicyTypeIdsJson(Collection policyTypes) { + List policyTypeList = new ArrayList<>(policyTypes.size()); PolicyTypeIdList idList = new PolicyTypeIdList(); for (PolicyType policyType : policyTypes) { - idList.addPolicytypeIdsItem(policyType.getId()); + policyTypeList.add(policyType.getId()); } - - return objectMapper.writeValueAsString(idList); + idList.setPolicytypeIds(policyTypeList); + return idList; } - private String toPolicyIdsJson(Collection policies) { + private PolicyIdList toPolicyIdsJson(Collection policies) { - try { List policyIds = new ArrayList<>(policies.size()); PolicyIdList idList = new PolicyIdList(); for (Policy policy : policies) { policyIds.add(policy.getId()); } idList.setPolicyIds(policyIds); - return objectMapper.writeValueAsString(idList); - } catch (JsonProcessingException ex) { - throw new RuntimeException(ex); - } + return idList; } } 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 63b0560c..8907774b 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 @@ -2,7 +2,7 @@ * ========================LICENSE_START================================= * ONAP : ccsdk oran * ====================================================================== - * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved. + * Copyright (C) 2019-2023 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. @@ -69,17 +69,17 @@ public class RicRepositoryController implements NearRtRicRepositoryApi { "Either a Near-RT RIC identity or a Managed Element identity can be specified.
" // + "The intention with Managed Element identity is the ID used in O1 for accessing the traffical element (such as the ID of CU)."; @Override - public Mono> getRic( + public Mono> getRic( final String managedElementId, final String ricId, final ServerWebExchange exchange) throws Exception { if (managedElementId != null && ricId != null) { throw new InvalidRequestException("Give one query parameter"); } else if (managedElementId != null) { Ric ric = this.rics.lookupRicForManagedElement(managedElementId); - return Mono.just(new ResponseEntity<>(objectMapper.writeValueAsString(toRicInfo(ric)), HttpStatus.OK)); + return Mono.just(new ResponseEntity<>(toRicInfo(ric), HttpStatus.OK)); } else if (ricId != null) { RicInfo info = toRicInfo(this.rics.getRic(ricId)); - return Mono.just(new ResponseEntity<>(objectMapper.writeValueAsString(info), HttpStatus.OK)); + return Mono.just(new ResponseEntity<>(info, HttpStatus.OK)); } else { throw new InvalidRequestException("Give one query parameter"); } @@ -89,7 +89,7 @@ public class RicRepositoryController implements NearRtRicRepositoryApi { "The call returns all Near-RT RICs that supports a given policy type identity"; @Override - public Mono> getRics(final String supportingPolicyType, final ServerWebExchange exchange) + public Mono> getRics(final String supportingPolicyType, final ServerWebExchange exchange) throws Exception { if ((supportingPolicyType != null) && (this.types.get(supportingPolicyType) == null)) { throw new EntityNotFoundException("Policy type not found"); @@ -102,7 +102,7 @@ public class RicRepositoryController implements NearRtRicRepositoryApi { } } - return Mono.just(new ResponseEntity<>(objectMapper.writeValueAsString(new RicInfoList().rics(result)), HttpStatus.OK)); + return Mono.just(new ResponseEntity<>(new RicInfoList().rics(result), HttpStatus.OK)); } private RicInfo.StateEnum toRicState(Ric.RicState state) { diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ServiceController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ServiceController.java index 07b9440c..da157db0 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ServiceController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ServiceController.java @@ -76,9 +76,9 @@ public class ServiceController implements ServiceRegistryAndSupervisionApi { "Either information about a registered service with given identity or all registered services are returned."; @Override - public Mono> getServices(final String name, final ServerWebExchange exchange) throws Exception { + public Mono> getServices(final String name, final ServerWebExchange exchange) throws Exception { if (name != null && this.services.get(name) == null) { - return ErrorResponse.createMono("Service not found", HttpStatus.NOT_FOUND); + throw new ServiceException("Service not found", HttpStatus.NOT_FOUND); } List servicesStatus = new ArrayList<>(); @@ -87,8 +87,7 @@ public class ServiceController implements ServiceRegistryAndSupervisionApi { servicesStatus.add(toServiceStatus(s)); } } - String res = objectMapper.writeValueAsString(new ServiceStatusList().serviceList(servicesStatus)); - return Mono.just(new ResponseEntity<>(res, HttpStatus.OK)); + return Mono.just(new ResponseEntity<>(new ServiceStatusList().serviceList(servicesStatus), HttpStatus.OK)); } private ServiceStatus toServiceStatus(Service s) { @@ -148,14 +147,10 @@ public class ServiceController implements ServiceRegistryAndSupervisionApi { } @Override - @PutMapping(Consts.V2_API_ROOT + "/services/{service_id}/keepalive") - public Mono> keepAliveService(final String serviceId, final ServerWebExchange exchange) { - try { + public Mono> keepAliveService(final String serviceId, final ServerWebExchange exchange) throws ServiceException { + services.getService(serviceId).keepAlive(); return Mono.just(new ResponseEntity<>(HttpStatus.OK)); - } catch (ServiceException e) { - return ErrorResponse.createMono(e, HttpStatus.NOT_FOUND); - } } private Service removeService(String name) throws ServiceException { diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/StatusController.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/StatusController.java index 22200da6..4ec3652d 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/StatusController.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/StatusController.java @@ -20,9 +20,9 @@ package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2; -import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v2.HealthCheckApi; +import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.StatusInfo; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestController; @@ -38,19 +38,9 @@ public class StatusController implements HealthCheckApi{ public static final String API_NAME = "Health Check"; public static final String API_DESCRIPTION = ""; - @Schema(name = "status_info_v2") - class StatusInfo { - @Schema(description = "status text") - public final String status; - - StatusInfo(String status) { - this.status = status; - } - } - @Override - public Mono> getStatus(final ServerWebExchange exchange) { - StatusInfo info = new StatusInfo("success"); + public Mono> getStatus(final ServerWebExchange exchange) { + StatusInfo info = new StatusInfo().status("success"); return Mono.just(new ResponseEntity<>(info, HttpStatus.OK)); } 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 index be2f5b7b..8dae6c0b 100644 --- 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 @@ -2,7 +2,7 @@ * ========================LICENSE_START================================= * ONAP : ccsdk oran * ====================================================================== - * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved. + * Copyright (C) 2019-2023 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. @@ -23,6 +23,7 @@ package org.onap.ccsdk.oran.a1policymanagementservice.exceptions; import java.lang.invoke.MethodHandles; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ErrorResponse; +import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicyController; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -47,4 +48,10 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { loggerx.error("Runtime exception {}", ex.getMessage()); return ErrorResponse.create(ex, HttpStatus.INTERNAL_SERVER_ERROR); } -} + + @ExceptionHandler(PolicyController.RejectionException.class) + public final ResponseEntity handleRejectionException(PolicyController.RejectionException ex) { + loggerx.error("Rejection exception {}", ex.getMessage()); + return ErrorResponse.create(ex, ex.getStatus()); + } +} \ No newline at end of file diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java index d8a902e9..c2f4f876 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Services.java @@ -34,6 +34,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -51,7 +52,7 @@ public class Services { public synchronized Service getService(String name) throws ServiceException { Service service = registeredServices.get(name); if (service == null) { - throw new ServiceException("Could not find service: " + name); + throw new ServiceException("Could not find service: " + name, HttpStatus.NOT_FOUND); } return service; } -- cgit 1.2.3-korg