9db66202d426c514d1ff9c095666b4f70f8fb919
[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.configuration;
20
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
26 import java.io.BufferedInputStream;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.lang.invoke.MethodHandles;
34 import java.util.Optional;
35
36 import javax.validation.constraints.NotNull;
37
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 public class ConfigurationFile {
45     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46
47     final ApplicationConfig appConfig;
48     final Gson gson = new Gson();
49
50     @Autowired
51     public ConfigurationFile(ApplicationConfig appConfig) {
52         this.appConfig = appConfig;
53     }
54
55     public long getLastModified() {
56         File file = new File(appConfig.getLocalConfigurationFilePath());
57         if (file.exists()) {
58             return file.lastModified();
59         }
60         return 0;
61     }
62
63     public synchronized Optional<JsonObject> readFile() {
64         String filepath = appConfig.getLocalConfigurationFilePath();
65         File file = new File(filepath);
66         if (!file.exists()) {
67             return Optional.empty();
68         }
69
70         try (InputStream inputStream = createInputStream(filepath)) {
71             JsonObject rootObject = getJsonElement(inputStream).getAsJsonObject();
72             logger.debug("Local configuration file read: {}", filepath);
73             return Optional.of(rootObject);
74         } catch (Exception e) {
75             logger.error("Local configuration file not read: {}, {}", filepath, e.getMessage());
76             return Optional.empty();
77         }
78     }
79
80     public synchronized void writeFile(JsonObject content) throws IOException {
81         String filepath = appConfig.getLocalConfigurationFilePath();
82         try (FileWriter fileWriter = getFileWriter(filepath)) {
83             gson.toJson(content, fileWriter);
84             logger.debug("Local configuration file written: {}", filepath);
85         }
86     }
87
88     FileWriter getFileWriter(String filepath) throws IOException {
89         return new FileWriter(filepath);
90     }
91
92     private JsonElement getJsonElement(InputStream inputStream) {
93         return JsonParser.parseReader(new InputStreamReader(inputStream));
94     }
95
96     private InputStream createInputStream(@NotNull String filepath) throws IOException {
97         return new BufferedInputStream(new FileInputStream(filepath));
98     }
99 }