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.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.MediaType;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.bind.annotation.GetMapping;
47 import org.springframework.web.bind.annotation.PutMapping;
48 import org.springframework.web.bind.annotation.RequestBody;
49 import org.springframework.web.bind.annotation.RestController;
51 @RestController("ConfigurationControllerV2")
52 @Tag(name = ConfigurationController.API_NAME)
53 public class ConfigurationController {
54 private static final Logger logger = LoggerFactory.getLogger(ConfigurationController.class);
56 public static final String API_NAME = "Management of configuration";
57 public static final String API_DESCRIPTION = "";
59 private final ConfigurationFile configurationFile;
60 private final ApplicationConfig applicationConfig;
62 ConfigurationController(@Autowired ConfigurationFile configurationFile,
63 @Autowired ApplicationConfig applicationConfig) {
64 this.configurationFile = configurationFile;
65 this.applicationConfig = applicationConfig;
68 private static Gson gson = new GsonBuilder() //
71 @PutMapping(path = Consts.V2_API_ROOT + "/configuration", consumes = MediaType.APPLICATION_JSON_VALUE)
72 @Operation(summary = "Replace the current configuration file with the given configuration")
73 @ApiResponses(value = { //
74 @ApiResponse(responseCode = "200", //
75 description = "Configuration updated", //
76 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
77 @ApiResponse(responseCode = "400", //
78 description = "Invalid configuration provided", //
79 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
80 @ApiResponse(responseCode = "500", //
81 description = "Something went wrong when replacing the configuration. Try again.", //
82 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
84 public ResponseEntity<Object> putConfiguration(@RequestBody Object configuration) {
86 String configAsString = gson.toJson(configuration);
87 JsonObject configJson = JsonParser.parseString(configAsString).getAsJsonObject();
88 ApplicationConfigParser configParser = new ApplicationConfigParser(applicationConfig);
89 configParser.parse(configJson);
90 configurationFile.writeFile(configJson);
91 logger.info("Configuration changed through REST call.");
92 return new ResponseEntity<>(HttpStatus.OK);
93 } catch (IOException ioe) {
94 logger.warn("Configuration file not written, {}.", ioe.getMessage());
95 return ErrorResponse.create("Internal error when writing the configuration.",
96 HttpStatus.INTERNAL_SERVER_ERROR);
97 } catch (Exception e) {
98 return ErrorResponse.create(e, HttpStatus.BAD_REQUEST);
102 @GetMapping(path = Consts.V2_API_ROOT + "/configuration", produces = MediaType.APPLICATION_JSON_VALUE)
103 @Operation(summary = "Returns the contents of the application configuration file") //
104 @ApiResponses(value = { //
105 @ApiResponse(responseCode = "200", //
106 description = "Configuration", //
107 content = @Content(schema = @Schema(implementation = Object.class))), //
108 @ApiResponse(responseCode = "404", //
109 description = "File is not found or readable", //
110 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))
113 public ResponseEntity<Object> getConfiguration() {
115 Optional<JsonObject> rootObject = configurationFile.readFile();
116 if (rootObject.isPresent()) {
117 return new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK);
119 return ErrorResponse.create("File does not exist", HttpStatus.NOT_FOUND);
121 } catch (Exception e) {
122 return ErrorResponse.create(e, HttpStatus.INTERNAL_SERVER_ERROR);