2 * ========================LICENSE_START=================================
3 * Copyright (C) 2020 Nordix Foundation. All rights reserved.
4 * ======================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ========================LICENSE_END===================================
19 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
21 import com.google.gson.Gson;
22 import com.google.gson.GsonBuilder;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
26 import io.swagger.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.responses.ApiResponses;
31 import io.swagger.v3.oas.annotations.tags.Tag;
33 import java.io.IOException;
34 import java.util.Optional;
36 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
37 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser;
38 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile;
39 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse;
40 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
41 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.MediaType;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.web.bind.annotation.GetMapping;
49 import org.springframework.web.bind.annotation.PutMapping;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RestController;
53 @RestController("ConfigurationControllerV2")
54 @Tag(name = ConfigurationController.API_NAME)
55 public class ConfigurationController {
56 private static final Logger logger = LoggerFactory.getLogger(ConfigurationController.class);
58 public static final String API_NAME = "Management of configuration";
59 public static final String API_DESCRIPTION = "";
61 private final ConfigurationFile configurationFile;
62 private final RefreshConfigTask refreshConfigTask;
63 private final ApplicationConfig applicationConfig;
65 ConfigurationController(@Autowired ConfigurationFile configurationFile,
66 @Autowired RefreshConfigTask refreshConfigTask, @Autowired ApplicationConfig applicationConfig) {
67 this.configurationFile = configurationFile;
68 this.refreshConfigTask = refreshConfigTask;
69 this.applicationConfig = applicationConfig;
73 private static Gson gson = new GsonBuilder() //
76 @PutMapping(path = Consts.V2_API_ROOT + "/configuration", consumes = MediaType.APPLICATION_JSON_VALUE)
77 @Operation(summary = "Replace the current configuration file with the given configuration", //
78 description = "Note that the file is ignored if the Consul is used.")
79 @ApiResponses(value = { //
80 @ApiResponse(responseCode = "200", //
81 description = "Configuration updated", //
82 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
83 @ApiResponse(responseCode = "400", //
84 description = "Invalid configuration provided", //
85 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
86 @ApiResponse(responseCode = "500", //
87 description = "Something went wrong when replacing the configuration. Try again.", //
88 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
90 public ResponseEntity<Object> putConfiguration(@RequestBody Object configuration) {
92 validateConfigFileIsUsed();
93 String configAsString = gson.toJson(configuration);
94 JsonObject configJson = JsonParser.parseString(configAsString).getAsJsonObject();
95 ApplicationConfigParser configParser = new ApplicationConfigParser(applicationConfig);
96 configParser.parse(configJson);
97 configurationFile.writeFile(configJson);
98 logger.info("Configuration changed through REST call.");
99 return new ResponseEntity<>(HttpStatus.OK);
100 } catch (IOException ioe) {
101 logger.warn("Configuration file not written, {}.", ioe.getMessage());
102 return ErrorResponse.create("Internal error when writing the configuration.",
103 HttpStatus.INTERNAL_SERVER_ERROR);
104 } catch (Exception e) {
105 return ErrorResponse.create(e, HttpStatus.BAD_REQUEST);
109 @GetMapping(path = Consts.V2_API_ROOT + "/configuration", produces = MediaType.APPLICATION_JSON_VALUE)
110 @Operation(summary = "Returns the contents of the configuration file", //
111 description = "Note that the file contents is not relevant if the Consul is used.") //
112 @ApiResponses(value = { //
113 @ApiResponse(responseCode = "200", //
114 description = "Configuration", //
115 content = @Content(schema = @Schema(implementation = Object.class))), //
116 @ApiResponse(responseCode = "404", //
117 description = "File is not found or readable", //
118 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))
121 public ResponseEntity<Object> getConfiguration() {
123 validateConfigFileIsUsed();
124 Optional<JsonObject> rootObject = configurationFile.readFile();
125 if (rootObject.isPresent()) {
126 return new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK);
128 return ErrorResponse.create("File does not exist", HttpStatus.NOT_FOUND);
130 } catch (Exception e) {
131 return ErrorResponse.create(e, HttpStatus.INTERNAL_SERVER_ERROR);
135 private void validateConfigFileIsUsed() throws ServiceException {
136 if (this.refreshConfigTask.isConsulUsed()) {
137 throw new ServiceException("Config file not used (Consul is used)", HttpStatus.FORBIDDEN);