diff options
Diffstat (limited to 'a1-policy-management/src/main')
5 files changed, 87 insertions, 42 deletions
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ConfigurationFile.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ConfigurationFile.java index 345722cb..26f44bb5 100644 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ConfigurationFile.java +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/configuration/ConfigurationFile.java @@ -22,6 +22,7 @@ import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; + import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; @@ -30,8 +31,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Optional; + import javax.validation.constraints.NotNull; -import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -57,21 +59,19 @@ public class ConfigurationFile { try (InputStream inputStream = createInputStream(filepath)) { JsonObject rootObject = getJsonElement(inputStream).getAsJsonObject(); - logger.debug("Local configuration file loaded: {}", filepath); + logger.debug("Local configuration file read: {}", filepath); return Optional.of(rootObject); } catch (Exception e) { - logger.error("Local configuration file not loaded: {}, {}", filepath, e.getMessage()); + logger.error("Local configuration file not read: {}, {}", filepath, e.getMessage()); return Optional.empty(); } } - public synchronized void writeFile(JsonObject content) throws ServiceException { + public synchronized void writeFile(JsonObject content) throws IOException { String filepath = appConfig.getLocalConfigurationFilePath(); try (FileWriter fileWriter = getFileWriter(filepath)) { gson.toJson(content, fileWriter); - } catch (IOException e) { - logger.error("Local configuration file not written: {}, {}", filepath, e.getMessage()); - throw new ServiceException("Local configuration file not written"); + logger.debug("Local configuration file written: {}", filepath); } } 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 new file mode 100644 index 00000000..5497e0a5 --- /dev/null +++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/ConfigurationController.java @@ -0,0 +1,75 @@ +/*- + * ========================LICENSE_START================================= + * Copyright (C) 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.controllers.v2; + +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; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; + +import java.io.IOException; + +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.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.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController("ConfigurationControllerV2") +@Api(tags = {Consts.V2_CONFIG_API_NAME}) +public class ConfigurationController { + + @Autowired + ConfigurationFile configurationFile; + + @PutMapping(path = Consts.V2_API_ROOT + "/configuration", consumes = MediaType.APPLICATION_JSON_VALUE) + @ApiOperation(value = "Replace the current configuration with the given configuration") + @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) { + try { + JsonObject configJson = JsonParser.parseString(configuration).getAsJsonObject(); + ApplicationConfigParser configParser = new ApplicationConfigParser(); + configParser.parse(configJson); + configurationFile.writeFile(configJson); + } catch (ServiceException | JsonSyntaxException e) { + return ErrorResponse.create(String.format("Faulty configuration. %s", e.getMessage()), + HttpStatus.BAD_REQUEST); + } catch (IOException ioe) { + ErrorResponse.create("Internal error when writing the configuration. Try again.", + HttpStatus.INTERNAL_SERVER_ERROR); + } + return new ResponseEntity<>(HttpStatus.OK); + } +} 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 478b8c27..e1961c17 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,6 +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"; private Consts() {} } diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/JsonObject.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/JsonObject.java deleted file mode 100644 index fea1d097..00000000 --- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/JsonObject.java +++ /dev/null @@ -1,31 +0,0 @@ -/*- - * ========================LICENSE_START================================= - * ONAP : ccsdk oran - * ====================================================================== - * Copyright (C) 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.controllers.v2; - -import io.swagger.annotations.ApiModel; - -import org.immutables.gson.Gson; - -@Gson.TypeAdapters -@ApiModel(value = "json_object", - description = "A JSON object defining the configuration of the policy. The schema is defined by the Policy Type.") -class JsonObject { -} 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 75fd16cf..dfb8d49e 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,9 +71,9 @@ public class RefreshConfigTask { public Properties systemEnvironment; /** - * The time between refreshes of the configuration. + * The time between refreshes of the configuration. Not final so tests can modify it. */ - static final Duration CONFIG_REFRESH_INTERVAL = Duration.ofMinutes(1); + private static Duration configRefreshInterval = Duration.ofMinutes(1); final ConfigurationFile configurationFile; final ApplicationConfig appConfig; @@ -118,14 +118,14 @@ public class RefreshConfigTask { } Flux<RicConfigUpdate.Type> createRefreshTask() { - Flux<JsonObject> loadFromFile = Flux.interval(Duration.ZERO, CONFIG_REFRESH_INTERVAL) // + Flux<JsonObject> loadFromFile = Flux.interval(Duration.ZERO, configRefreshInterval) // .filter(notUsed -> !this.isConsulUsed) // .flatMap(notUsed -> loadConfigurationFromFile()) // .onErrorResume(this::ignoreErrorFlux) // .doOnNext(json -> logger.debug("loadFromFile succeeded")) // .doOnTerminate(() -> logger.error("loadFromFile Terminate")); - Flux<JsonObject> loadFromConsul = Flux.interval(Duration.ZERO, CONFIG_REFRESH_INTERVAL) // + Flux<JsonObject> loadFromConsul = Flux.interval(Duration.ZERO, configRefreshInterval) // .flatMap(i -> getEnvironment(systemEnvironment)) // .flatMap(this::createCbsClient) // .flatMap(this::getFromCbs) // |