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