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.JsonObject;
22 import com.google.gson.JsonParser;
23 import com.google.gson.JsonSyntaxException;
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
30 import java.io.IOException;
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.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.MediaType;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.PutMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RestController;
45 @RestController("ConfigurationControllerV2")
46 @Api(tags = {Consts.V2_CONFIG_API_NAME})
47 public class ConfigurationController {
50 ConfigurationFile configurationFile;
52 @PutMapping(path = Consts.V2_API_ROOT + "/configuration", consumes = MediaType.APPLICATION_JSON_VALUE)
53 @ApiOperation(value = "Replace the current configuration with the given configuration")
54 @ApiResponses(value = { //
55 @ApiResponse(code = 200, message = "Configuration updated", response = VoidResponse.class), //
56 @ApiResponse(code = 400, message = "Invalid configuration provided", response = ErrorInfo.class), //
57 @ApiResponse(code = 500, message = "Something went wrong when replacing the configuration. Try again.",
58 response = ErrorResponse.ErrorInfo.class) //
60 public ResponseEntity<Object> putConfiguration(@RequestBody String configuration) {
62 JsonObject configJson = JsonParser.parseString(configuration).getAsJsonObject();
63 ApplicationConfigParser configParser = new ApplicationConfigParser();
64 configParser.parse(configJson);
65 configurationFile.writeFile(configJson);
66 } catch (ServiceException | JsonSyntaxException e) {
67 return ErrorResponse.create(String.format("Faulty configuration. %s", e.getMessage()),
68 HttpStatus.BAD_REQUEST);
69 } catch (IOException ioe) {
70 ErrorResponse.create("Internal error when writing the configuration. Try again.",
71 HttpStatus.INTERNAL_SERVER_ERROR);
73 return new ResponseEntity<>(HttpStatus.OK);