Merge "Policy-agent skeleton"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / configuration / ApplicationConfig.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.policyagent.configuration;
22
23 import com.google.gson.GsonBuilder;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import com.google.gson.JsonSyntaxException;
28 import com.google.gson.TypeAdapterFactory;
29
30 import java.io.BufferedInputStream;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.util.Properties;
36 import java.util.ServiceLoader;
37
38 import javax.validation.constraints.NotEmpty;
39 import javax.validation.constraints.NotNull;
40
41 import org.oransc.policyagent.exceptions.ServiceException;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.beans.factory.annotation.Value;
46 import org.springframework.boot.context.properties.ConfigurationProperties;
47 import org.springframework.boot.context.properties.EnableConfigurationProperties;
48 import org.springframework.stereotype.Component;
49 import reactor.core.publisher.Mono;
50
51 @Component
52 @EnableConfigurationProperties
53 @ConfigurationProperties("app")
54 public class ApplicationConfig {
55     private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class);
56
57     @Value("#{systemEnvironment}")
58     Properties systemEnvironment;
59
60     @NotEmpty
61     private String filepath;
62
63     @Autowired
64     public ApplicationConfig() {
65     }
66
67     public synchronized void setFilepath(String filepath) {
68         this.filepath = filepath;
69     }
70
71     /**
72      * Reads the cloud configuration.
73      */
74     public void initialize() {
75         loadConfigurationFromFile();
76     }
77
78     Mono<Environment.Variables> getEnvironment(Properties systemEnvironment) {
79         return Environment.readEnvironmentVariables(systemEnvironment);
80     }
81
82     /**
83      * Reads the configuration from file.
84      */
85     void loadConfigurationFromFile() {
86         GsonBuilder gsonBuilder = new GsonBuilder();
87         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
88
89         try (InputStream inputStream = createInputStream(filepath)) {
90             JsonParser parser = new JsonParser();
91             JsonObject rootObject = getJsonElement(parser, inputStream).getAsJsonObject();
92             if (rootObject == null) {
93                 throw new JsonSyntaxException("Root is not a json object");
94             }
95             ApplicationConfigParser appParser = new ApplicationConfigParser();
96             appParser.parse(rootObject);
97             logger.info("Local configuration file loaded: {}", filepath);
98         } catch (JsonSyntaxException | ServiceException | IOException e) {
99             logger.trace("Local configuration file not loaded: {}", filepath, e);
100         }
101     }
102
103     JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
104         return parser.parse(new InputStreamReader(inputStream));
105     }
106
107     InputStream createInputStream(@NotNull String filepath) throws IOException {
108         return new BufferedInputStream(new FileInputStream(filepath));
109     }
110
111 }