7a432f83113c6c8b53f1cc04c72661b680dca85f
[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.JsonObject;
22 import com.google.gson.JsonParser;
23 import com.google.gson.JsonSyntaxException;
24
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29
30 import java.io.IOException;
31
32 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser;
33 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile;
34 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse;
35 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ErrorResponse.ErrorInfo;
36 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.PutMapping;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RestController;
46
47 @RestController("ConfigurationControllerV2")
48 @Api(tags = {Consts.V2_CONFIG_API_NAME})
49 public class ConfigurationController {
50     private static final Logger logger = LoggerFactory.getLogger(ConfigurationController.class);
51
52     @Autowired
53     ConfigurationFile configurationFile;
54
55     @PutMapping(path = Consts.V2_API_ROOT + "/configuration", consumes = MediaType.APPLICATION_JSON_VALUE)
56     @ApiOperation(value = "Replace the current configuration with the given configuration")
57     @ApiResponses(value = { //
58             @ApiResponse(code = 200, message = "Configuration updated", response = VoidResponse.class), //
59             @ApiResponse(code = 400, message = "Invalid configuration provided", response = ErrorInfo.class), //
60             @ApiResponse(code = 500, message = "Something went wrong when replacing the configuration. Try again.",
61                     response = ErrorResponse.ErrorInfo.class) //
62     })
63     public ResponseEntity<Object> putConfiguration(@RequestBody String configuration) {
64         try {
65             JsonObject configJson = JsonParser.parseString(configuration).getAsJsonObject();
66             ApplicationConfigParser configParser = new ApplicationConfigParser();
67             configParser.parse(configJson);
68             configurationFile.writeFile(configJson);
69             logger.info("Configuration changed through REST call.");
70         } catch (ServiceException | JsonSyntaxException e) {
71             return ErrorResponse.create(String.format("Faulty configuration. %s", e.getMessage()),
72                     HttpStatus.BAD_REQUEST);
73         } catch (IOException ioe) {
74             logger.warn("Configuration file not written, {}.", ioe.getMessage());
75             ErrorResponse.create("Internal error when writing the configuration. Try again.",
76                     HttpStatus.INTERNAL_SERVER_ERROR);
77         }
78         return new ResponseEntity<>(HttpStatus.OK);
79     }
80 }