4b0d6b58ab165a19cb65bf5a80d93f27b47875af
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * Copyright (C) 2020-2023 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 import io.swagger.v3.oas.annotations.tags.Tag;
26 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
27 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfigParser;
28 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ConfigurationFile;
29 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v2.ConfigurationApi;
30 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.RestController;
37 import org.springframework.web.server.ServerWebExchange;
38 import reactor.core.publisher.Mono;
39 import java.io.IOException;
40 import java.util.Optional;
41
42 @RestController("ConfigurationControllerV2")
43 @Tag( //
44         name = ConfigurationController.API_NAME, //
45         description = ConfigurationController.API_DESCRIPTION //
46 )
47 public class ConfigurationController implements ConfigurationApi {
48     private static final Logger logger = LoggerFactory.getLogger(ConfigurationController.class);
49
50     public static final String API_NAME = "Management of configuration";
51     public static final String API_DESCRIPTION = "";
52
53     private final ConfigurationFile configurationFile;
54     private final ApplicationConfig applicationConfig;
55
56     ConfigurationController(@Autowired ConfigurationFile configurationFile,
57             @Autowired ApplicationConfig applicationConfig) {
58         this.configurationFile = configurationFile;
59         this.applicationConfig = applicationConfig;
60     }
61
62     private static Gson gson = new GsonBuilder() //
63             .create(); //
64
65     @Override
66     public Mono<ResponseEntity<Object>> putConfiguration(final Mono<Object> configuration,
67                                                          final ServerWebExchange exchange) {
68         return configuration
69                 .flatMap(configObject -> {
70                     try {
71                         String configAsString = gson.toJson(configObject);
72                         JsonObject configJson = JsonParser.parseString(configAsString).getAsJsonObject();
73                         ApplicationConfigParser configParser = new ApplicationConfigParser(applicationConfig);
74                         configParser.parse(configJson);
75                         configurationFile.writeFile(configJson);
76                         logger.info("Configuration changed through REST call.");
77                         return Mono.just(new ResponseEntity<>(HttpStatus.OK));
78                     } catch (IOException ioe) {
79                         logger.warn("Configuration file not written, {}.", ioe.getMessage());
80                         return ErrorResponse.createMono("Internal error when writing the configuration.",
81                                 HttpStatus.INTERNAL_SERVER_ERROR);
82                     } catch (ServiceException e) {
83                         return ErrorResponse.createMono(e, HttpStatus.BAD_REQUEST);
84                     }
85                 })
86                 .doOnError(error -> logger.error(error.getMessage()));
87     }
88
89     @Override
90     public Mono<ResponseEntity<String>> getConfiguration(final ServerWebExchange exchange) throws ServiceException {
91             Optional<JsonObject> rootObject = configurationFile.readFile();
92             if (rootObject.isPresent()) {
93                 return Mono.just(new ResponseEntity<>(rootObject.get().toString(), HttpStatus.OK));
94             } else {
95                 throw new ServiceException("File does not exist", HttpStatus.NOT_FOUND);
96             }
97     }
98 }