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.configuration;
21 import com.google.gson.Gson;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25 import java.io.BufferedInputStream;
27 import java.io.FileInputStream;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.util.Optional;
33 import javax.validation.constraints.NotNull;
34 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
41 public class ConfigurationFile {
42 private static final Logger logger = LoggerFactory.getLogger(ConfigurationFile.class);
44 final ApplicationConfig appConfig;
45 final Gson gson = new Gson();
48 public ConfigurationFile(ApplicationConfig appConfig) {
49 this.appConfig = appConfig;
52 public synchronized Optional<JsonObject> readFile() {
53 String filepath = appConfig.getLocalConfigurationFilePath();
54 if (!fileExists(filepath)) {
55 return Optional.empty();
58 try (InputStream inputStream = createInputStream(filepath)) {
59 JsonObject rootObject = getJsonElement(inputStream).getAsJsonObject();
60 logger.debug("Local configuration file loaded: {}", filepath);
61 return Optional.of(rootObject);
62 } catch (Exception e) {
63 logger.error("Local configuration file not loaded: {}, {}", filepath, e.getMessage());
64 return Optional.empty();
68 public synchronized void writeFile(JsonObject content) throws ServiceException {
69 String filepath = appConfig.getLocalConfigurationFilePath();
70 try (FileWriter fileWriter = getFileWriter(filepath)) {
71 gson.toJson(content, fileWriter);
72 } catch (IOException e) {
73 logger.error("Local configuration file not written: {}, {}", filepath, e.getMessage());
74 throw new ServiceException("Local configuration file not written");
78 FileWriter getFileWriter(String filepath) throws IOException {
79 return new FileWriter(filepath);
82 private boolean fileExists(String filepath) {
83 return (new File(filepath).exists());
86 private JsonElement getJsonElement(InputStream inputStream) {
87 return JsonParser.parseReader(new InputStreamReader(inputStream));
90 private InputStream createInputStream(@NotNull String filepath) throws IOException {
91 return new BufferedInputStream(new FileInputStream(filepath));