7a65bacbedfbc8b7225b35f5880ba59df91d91f7
[ccsdk/oran.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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===================================
17  */
18
19 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
20
21 import com.google.gson.Gson;
22 import com.google.gson.GsonBuilder;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25
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;
32
33 import java.io.IOException;
34 import java.util.Optional;
35
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;
50
51 @RestController("ConfigurationControllerV2")
52 @Tag(name = ConfigurationController.API_NAME)
53 public class ConfigurationController {
54     private static final Logger logger = LoggerFactory.getLogger(ConfigurationController.class);
55
56     public static final String API_NAME = "Management of configuration";
57     public static final String API_DESCRIPTION = "";
58
59     private final ConfigurationFile configurationFile;
60     private final ApplicationConfig applicationConfig;
61
62     ConfigurationController(@Autowired ConfigurationFile configurationFile,
63             @Autowired ApplicationConfig applicationConfig) {
64         this.configurationFile = configurationFile;
65         this.applicationConfig = applicationConfig;
66     }
67
68     private static Gson gson = new GsonBuilder() //
69             .create(); //
70
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))) //
83     })
84     public ResponseEntity<Object> putConfiguration(@RequestBody Object configuration) {
85         try {
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);
99         }
100     }
101
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)))
111
112     })
113     public ResponseEntity<Object> getConfiguration() {
114         try {
115             Optional<JsonObject> rootObject = configurationFile.readFile();
116             if (rootObject.isPresent()) {
117                 return new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK);
118             } else {
119                 return ErrorResponse.create("File does not exist", HttpStatus.NOT_FOUND);
120             }
121         } catch (Exception e) {
122             return ErrorResponse.create(e, HttpStatus.INTERNAL_SERVER_ERROR);
123         }
124     }
125
126 }