86ae273402b10b85e98ea19d55ea83772188106d
[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.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30
31 import java.io.IOException;
32 import java.util.Optional;
33
34 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser;
35 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile;
36 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse;
37 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ErrorResponse.ErrorInfo;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.MediaType;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PutMapping;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RestController;
48
49 @RestController("ConfigurationControllerV2")
50 @Api(tags = {Consts.V2_CONFIG_API_NAME})
51 public class ConfigurationController {
52     private static final Logger logger = LoggerFactory.getLogger(ConfigurationController.class);
53
54     @Autowired
55     ConfigurationFile configurationFile;
56
57     private static Gson gson = new GsonBuilder() //
58             .create(); //
59
60     @PutMapping(path = Consts.V2_API_ROOT + "/configuration", consumes = MediaType.APPLICATION_JSON_VALUE)
61     @ApiOperation(value = "Replace the current configuration file with the given configuration", //
62             notes = "Note that the file is ignored if the Consul is used.")
63     @ApiResponses(value = { //
64             @ApiResponse(code = 200, message = "Configuration updated", response = VoidResponse.class), //
65             @ApiResponse(code = 400, message = "Invalid configuration provided", response = ErrorInfo.class), //
66             @ApiResponse(code = 500, message = "Something went wrong when replacing the configuration. Try again.",
67                     response = ErrorResponse.ErrorInfo.class) //
68     })
69     public ResponseEntity<Object> putConfiguration(@RequestBody Object configuration) {
70         try {
71             String configAsString = gson.toJson(configuration);
72             JsonObject configJson = JsonParser.parseString(configAsString).getAsJsonObject();
73             ApplicationConfigParser configParser = new ApplicationConfigParser();
74             configParser.parse(configJson);
75             configurationFile.writeFile(configJson);
76             logger.info("Configuration changed through REST call.");
77             return new ResponseEntity<>(HttpStatus.OK);
78         } catch (IOException ioe) {
79             logger.warn("Configuration file not written, {}.", ioe.getMessage());
80             return ErrorResponse.create("Internal error when writing the configuration. Try again.",
81                     HttpStatus.INTERNAL_SERVER_ERROR);
82         } catch (Exception e) {
83             return ErrorResponse.create(String.format("Faulty configuration. %s", e.getMessage()),
84                     HttpStatus.BAD_REQUEST);
85         }
86     }
87
88     @GetMapping(path = Consts.V2_API_ROOT + "/configuration", produces = MediaType.APPLICATION_JSON_VALUE)
89     @ApiOperation(value = "Returns the contents of the configuration file", //
90             notes = "Note that the file contents is not relevant if the Consul is used.") //
91     @ApiResponses(value = { //
92             @ApiResponse(code = 200, message = "Configuration", response = Object.class), //
93             @ApiResponse(code = 404, message = "File is not found or readable",
94                     response = ErrorResponse.ErrorInfo.class)} //
95     )
96     public ResponseEntity<Object> getConfiguration() {
97         try {
98             Optional<JsonObject> rootObject = configurationFile.readFile();
99             if (rootObject.isPresent()) {
100                 return new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK);
101             } else {
102                 return ErrorResponse.create("File does not exist", HttpStatus.NOT_FOUND);
103             }
104         } catch (Exception e) {
105             return ErrorResponse.create(e, HttpStatus.INTERNAL_SERVER_ERROR);
106         }
107     }
108
109 }