diff options
author | PatrikBuhr <patrik.buhr@est.tech> | 2020-11-23 13:24:45 +0100 |
---|---|---|
committer | PatrikBuhr <patrik.buhr@est.tech> | 2020-11-24 14:47:16 +0100 |
commit | 3b56b4ff40eba05108651d04df4b9a2ef3a4eb1d (patch) | |
tree | b071a923bd9295617f79e3cd95d943e04ab34a8a /a1-policy-management/src/main | |
parent | 50fe65ea5daca6935cdbc1155adbfb53f5a28c40 (diff) |
Added GET config
Corrected config so that PUT config receives an object intestead of a string
Change-Id: Id784cc2840a2e2e981b0c4e874b8a848964e00ea
Issue-ID: CCSDK-2966
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
Diffstat (limited to 'a1-policy-management/src/main')
3 files changed, 42 insertions, 12 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 7a432f83..86ae2734 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 @@ -18,9 +18,10 @@ package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -28,18 +29,19 @@ import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import java.io.IOException; +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.controllers.v2.ErrorResponse.ErrorInfo; -import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @@ -52,29 +54,56 @@ public class ConfigurationController { @Autowired ConfigurationFile configurationFile; + private static Gson gson = new GsonBuilder() // + .create(); // + @PutMapping(path = Consts.V2_API_ROOT + "/configuration", consumes = MediaType.APPLICATION_JSON_VALUE) - @ApiOperation(value = "Replace the current configuration with the given configuration") + @ApiOperation(value = "Replace the current configuration file with the given configuration", // + notes = "Note that the file is ignored if the Consul is used.") @ApiResponses(value = { // @ApiResponse(code = 200, message = "Configuration updated", response = VoidResponse.class), // @ApiResponse(code = 400, message = "Invalid configuration provided", response = ErrorInfo.class), // @ApiResponse(code = 500, message = "Something went wrong when replacing the configuration. Try again.", response = ErrorResponse.ErrorInfo.class) // }) - public ResponseEntity<Object> putConfiguration(@RequestBody String configuration) { + public ResponseEntity<Object> putConfiguration(@RequestBody Object configuration) { try { - JsonObject configJson = JsonParser.parseString(configuration).getAsJsonObject(); + String configAsString = gson.toJson(configuration); + JsonObject configJson = JsonParser.parseString(configAsString).getAsJsonObject(); ApplicationConfigParser configParser = new ApplicationConfigParser(); configParser.parse(configJson); configurationFile.writeFile(configJson); logger.info("Configuration changed through REST call."); - } catch (ServiceException | JsonSyntaxException e) { - return ErrorResponse.create(String.format("Faulty configuration. %s", e.getMessage()), - HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(HttpStatus.OK); } catch (IOException ioe) { logger.warn("Configuration file not written, {}.", ioe.getMessage()); - ErrorResponse.create("Internal error when writing the configuration. Try again.", + return ErrorResponse.create("Internal error when writing the configuration. Try again.", HttpStatus.INTERNAL_SERVER_ERROR); + } catch (Exception e) { + return ErrorResponse.create(String.format("Faulty configuration. %s", e.getMessage()), + HttpStatus.BAD_REQUEST); } - return new ResponseEntity<>(HttpStatus.OK); } + + @GetMapping(path = Consts.V2_API_ROOT + "/configuration", produces = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation(value = "Returns the contents of the configuration file", // + notes = "Note that the file contents is not relevant if the Consul is used.") // + @ApiResponses(value = { // + @ApiResponse(code = 200, message = "Configuration", response = Object.class), // + @ApiResponse(code = 404, message = "File is not found or readable", + response = ErrorResponse.ErrorInfo.class)} // + ) + public ResponseEntity<Object> getConfiguration() { + try { + Optional<JsonObject> rootObject = configurationFile.readFile(); + if (rootObject.isPresent()) { + return new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK); + } else { + return ErrorResponse.create("File does not exist", HttpStatus.NOT_FOUND); + } + } catch (Exception e) { + return ErrorResponse.create(e, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java index e1961c17..81021b15 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java @@ -30,7 +30,7 @@ public class Consts { public static final String V2_API_ROOT = "/v2"; public static final String V2_API_NAME = "A1 Policy Management Version 2.0 (in progress)"; - public static final String V2_CONFIG_API_NAME = "A1 Policy Management Configuration"; + public static final String V2_CONFIG_API_NAME = "Component Configuration"; private Consts() {} } 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 dfb8d49e..29d2e4eb 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 @@ -71,7 +71,8 @@ public class RefreshConfigTask { public Properties systemEnvironment; /** - * The time between refreshes of the configuration. Not final so tests can modify it. + * The time between refreshes of the configuration. Not final so tests can + * modify it. */ private static Duration configRefreshInterval = Duration.ofMinutes(1); |