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")
53 name = ConfigurationController.API_NAME, //
54 description = ConfigurationController.API_DESCRIPTION //
56 public class ConfigurationController {
57 private static final Logger logger = LoggerFactory.getLogger(ConfigurationController.class);
59 public static final String API_NAME = "Management of configuration";
60 public static final String API_DESCRIPTION = "";
62 private final ConfigurationFile configurationFile;
63 private final ApplicationConfig applicationConfig;
65 ConfigurationController(@Autowired ConfigurationFile configurationFile,
66 @Autowired ApplicationConfig applicationConfig) {
67 this.configurationFile = configurationFile;
68 this.applicationConfig = applicationConfig;
71 private static Gson gson = new GsonBuilder() //
74 @PutMapping(path = Consts.V2_API_ROOT + "/configuration", consumes = MediaType.APPLICATION_JSON_VALUE)
75 @Operation(summary = "Replace the current configuration file with the given configuration")
76 @ApiResponses(value = { //
77 @ApiResponse(responseCode = "200", //
78 description = "Configuration updated", //
79 content = @Content(schema = @Schema(implementation = VoidResponse.class))), //
80 @ApiResponse(responseCode = "400", //
81 description = "Invalid configuration provided", //
82 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))), //
83 @ApiResponse(responseCode = "500", //
84 description = "Something went wrong when replacing the configuration. Try again.", //
85 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class))) //
87 public ResponseEntity<Object> putConfiguration(@RequestBody Object configuration) {
89 String configAsString = gson.toJson(configuration);
90 JsonObject configJson = JsonParser.parseString(configAsString).getAsJsonObject();
91 ApplicationConfigParser configParser = new ApplicationConfigParser(applicationConfig);
92 configParser.parse(configJson);
93 configurationFile.writeFile(configJson);
94 logger.info("Configuration changed through REST call.");
95 return new ResponseEntity<>(HttpStatus.OK);
96 } catch (IOException ioe) {
97 logger.warn("Configuration file not written, {}.", ioe.getMessage());
98 return ErrorResponse.create("Internal error when writing the configuration.",
99 HttpStatus.INTERNAL_SERVER_ERROR);
100 } catch (Exception e) {
101 return ErrorResponse.create(e, HttpStatus.BAD_REQUEST);
105 @GetMapping(path = Consts.V2_API_ROOT + "/configuration", produces = MediaType.APPLICATION_JSON_VALUE)
106 @Operation(summary = "Returns the contents of the application configuration file") //
107 @ApiResponses(value = { //
108 @ApiResponse(responseCode = "200", //
109 description = "Configuration", //
110 content = @Content(schema = @Schema(implementation = Object.class))), //
111 @ApiResponse(responseCode = "404", //
112 description = "File is not found or readable", //
113 content = @Content(schema = @Schema(implementation = ErrorResponse.ErrorInfo.class)))
116 public ResponseEntity<Object> getConfiguration() {
118 Optional<JsonObject> rootObject = configurationFile.readFile();
119 if (rootObject.isPresent()) {
120 return new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK);
122 return ErrorResponse.create("File does not exist", HttpStatus.NOT_FOUND);
124 } catch (Exception e) {
125 return ErrorResponse.create(e, HttpStatus.INTERNAL_SERVER_ERROR);