diff options
7 files changed, 49 insertions, 9 deletions
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 e6b938a0..b677a405 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 @@ -36,6 +36,8 @@ import java.util.Optional; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser; import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile; import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; +import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -58,6 +60,9 @@ public class ConfigurationController { @Autowired ConfigurationFile configurationFile; + @Autowired + RefreshConfigTask refreshConfigTask; + private static Gson gson = new GsonBuilder() // .create(); // @@ -77,6 +82,7 @@ public class ConfigurationController { }) public ResponseEntity<Object> putConfiguration(@RequestBody Object configuration) { try { + validateConfigFileIsUsed(); String configAsString = gson.toJson(configuration); JsonObject configJson = JsonParser.parseString(configAsString).getAsJsonObject(); ApplicationConfigParser configParser = new ApplicationConfigParser(); @@ -86,11 +92,10 @@ public class ConfigurationController { return new ResponseEntity<>(HttpStatus.OK); } catch (IOException ioe) { logger.warn("Configuration file not written, {}.", ioe.getMessage()); - return ErrorResponse.create("Internal error when writing the configuration. Try again.", + return ErrorResponse.create("Internal error when writing the configuration.", HttpStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { - return ErrorResponse.create(String.format("Faulty configuration. %s", e.getMessage()), - HttpStatus.BAD_REQUEST); + return ErrorResponse.create(e, HttpStatus.BAD_REQUEST); } } @@ -108,6 +113,7 @@ public class ConfigurationController { }) public ResponseEntity<Object> getConfiguration() { try { + validateConfigFileIsUsed(); Optional<JsonObject> rootObject = configurationFile.readFile(); if (rootObject.isPresent()) { return new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK); @@ -119,4 +125,11 @@ public class ConfigurationController { } } + private void validateConfigFileIsUsed() throws ServiceException { + if (this.refreshConfigTask.isConsulUsed()) { + throw new ServiceException("Config file not used (Consul is used)", HttpStatus.FORBIDDEN); + } + + } + } 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 c3ee1952..78ba6b69 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 @@ -26,6 +26,7 @@ import com.google.gson.annotations.SerializedName; import io.swagger.v3.oas.annotations.media.Schema; +import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -96,7 +97,15 @@ public class ErrorResponse { return new ResponseEntity<>(json, headers, code); } - public static ResponseEntity<Object> create(Exception e, HttpStatus code) { + public static ResponseEntity<Object> create(Throwable e, HttpStatus code) { + if (e instanceof RuntimeException) { + code = HttpStatus.INTERNAL_SERVER_ERROR; + } else if (e instanceof ServiceException) { + ServiceException se = (ServiceException) e; + if (se.getHttpStatus() != null) { + code = se.getHttpStatus(); + } + } return create(e.toString(), code); } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/ServiceException.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/ServiceException.java index 17e651aa..8f5956ce 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/ServiceException.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/exceptions/ServiceException.java @@ -20,15 +20,30 @@ package org.onap.ccsdk.oran.a1policymanagementservice.exceptions; +import lombok.Getter; + +import org.springframework.http.HttpStatus; + public class ServiceException extends Exception { private static final long serialVersionUID = 1L; + @Getter + private final HttpStatus httpStatus; + public ServiceException(String message) { super(message); + httpStatus = null; } public ServiceException(String message, Exception originalException) { super(message, originalException); + httpStatus = null; } + + public ServiceException(String message, HttpStatus httpStatus) { + super(message); + this.httpStatus = httpStatus; + } + } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java index e84c9838..30279815 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/tasks/RefreshConfigTask.java @@ -80,8 +80,11 @@ public class RefreshConfigTask { final ConfigurationFile configurationFile; final ApplicationConfig appConfig; + @Getter(AccessLevel.PROTECTED) private Disposable refreshTask = null; + + @Getter private boolean isConsulUsed = false; private final Rics rics; diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java index 91eb6333..9c9331da 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ApplicationTest.java @@ -283,13 +283,12 @@ class ApplicationTest { } @Test - void testAddingRicFromConfiguration_nonRespondingRic() throws ServiceException { + void testAddingRicFromConfiguration_nonRespondingRic() throws Exception { putService("service"); final String RIC = "NonRespondingRic"; MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC); - WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null); - doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyTypeIdentities(); + doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities(); refreshConfigTask.handleUpdatedRicConfig( // new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) // diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java index 6192c932..030b831f 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationControllerTest.java @@ -135,7 +135,8 @@ class ConfigurationControllerTest { String url = "a1-policy/v2/configuration"; // Valid JSON but invalid configuration. - testErrorCode(restClient().put(url, "{\"error\":\"error\"}"), HttpStatus.BAD_REQUEST, "Faulty configuration"); + testErrorCode(restClient().put(url, "{\"error\":\"error\"}"), HttpStatus.BAD_REQUEST, + "Missing root configuration"); } private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) { diff --git a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1Client.java b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1Client.java index 22ac2437..2a3b28ef 100644 --- a/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1Client.java +++ b/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/utils/MockA1Client.java @@ -124,7 +124,7 @@ public class MockA1Client implements A1Client { } } - Mono<String> monoError(String responseBody, HttpStatus status) { + public static Mono<String> monoError(String responseBody, HttpStatus status) { byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8); WebClientResponseException a1Exception = new WebClientResponseException(status.value(), status.getReasonPhrase(), null, responseBodyBytes, StandardCharsets.UTF_8, null); |