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.ApplicationConfigParser;
37 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile;
38 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.MediaType;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.web.bind.annotation.GetMapping;
46 import org.springframework.web.bind.annotation.PutMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RestController;
50 @RestController("ConfigurationControllerV2")
51 @Tag(name = ConfigurationController.API_NAME)
52 public class ConfigurationController {
53 private static final Logger logger = LoggerFactory.getLogger(ConfigurationController.class);
55 public static final String API_NAME = "Management of configuration";
56 public static final String API_DESCRIPTION = "";
59 ConfigurationFile configurationFile;
61 private static Gson gson = new GsonBuilder() //
64 @PutMapping(path = Consts.V2_API_ROOT + "/configuration", consumes = MediaType.APPLICATION_JSON_VALUE)
65 @Operation(summary = "Replace the current configuration file with the given configuration", //
66 description = "Note that the file is ignored if the Consul is used.")
67 @ApiResponses(value = { //
68 @ApiResponse(responseCode = "200", //
69 description = "Configuration updated", //
70 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
71 @ApiResponse(responseCode = "400", //
72 description = "Invalid configuration provided", //
73 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
74 @ApiResponse(responseCode = "500", //
75 description = "Something went wrong when replacing the configuration. Try again.", //
76 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
78 public ResponseEntity<Object> putConfiguration(@RequestBody Object configuration) {
80 String configAsString = gson.toJson(configuration);
81 JsonObject configJson = JsonParser.parseString(configAsString).getAsJsonObject();
82 ApplicationConfigParser configParser = new ApplicationConfigParser();
83 configParser.parse(configJson);
84 configurationFile.writeFile(configJson);
85 logger.info("Configuration changed through REST call.");
86 return new ResponseEntity<>(HttpStatus.OK);
87 } catch (IOException ioe) {
88 logger.warn("Configuration file not written, {}.", ioe.getMessage());
89 return ErrorResponse.create("Internal error when writing the configuration. Try again.",
90 HttpStatus.INTERNAL_SERVER_ERROR);
91 } catch (Exception e) {
92 return ErrorResponse.create(String.format("Faulty configuration. %s", e.getMessage()),
93 HttpStatus.BAD_REQUEST);
97 @GetMapping(path = Consts.V2_API_ROOT + "/configuration", produces = MediaType.APPLICATION_JSON_VALUE)
98 @Operation(summary = "Returns the contents of the configuration file", //
99 description = "Note that the file contents is not relevant if the Consul is used.") //
100 @ApiResponses(value = { //
101 @ApiResponse(responseCode = "200", //
102 description = "Configuration", //
103 content = @Content(schema = @Schema(implementation = Object.class))), //
104 @ApiResponse(responseCode = "404", //
105 description = "File is not found or readable", //
106 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))
109 public ResponseEntity<Object> getConfiguration() {
111 Optional<JsonObject> rootObject = configurationFile.readFile();
112 if (rootObject.isPresent()) {
113 return new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK);
115 return ErrorResponse.create("File does not exist", HttpStatus.NOT_FOUND);
117 } catch (Exception e) {
118 return ErrorResponse.create(e, HttpStatus.INTERNAL_SERVER_ERROR);