79c82a789844d21d058ea9b97c83084111aa2cfa
[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.Optional;
36 import java.util.Properties;
37 import java.util.ServiceLoader;
38 import java.util.Vector;
39
40 import javax.validation.constraints.NotEmpty;
41 import javax.validation.constraints.NotNull;
42
43 import org.oransc.policyagent.exceptions.ServiceException;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.beans.factory.annotation.Value;
48 import org.springframework.boot.context.properties.ConfigurationProperties;
49 import org.springframework.boot.context.properties.EnableConfigurationProperties;
50 import org.springframework.stereotype.Component;
51 import reactor.core.publisher.Mono;
52
53 @Component
54 @EnableConfigurationProperties
55 @ConfigurationProperties("app")
56 public class ApplicationConfig {
57     private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class);
58
59     @Value("#{systemEnvironment}")
60     Properties systemEnvironment;
61
62     @NotEmpty
63     private String filepath;
64
65     private Vector<RicConfig> ricConfigs;
66
67     @Autowired
68     public ApplicationConfig() {
69     }
70
71     public synchronized void setFilepath(String filepath) {
72         this.filepath = filepath;
73     }
74
75     public Vector<RicConfig> getRicConfigs() {
76         return this.ricConfigs;
77     }
78
79     public Optional<RicConfig> getRicConfig(String managedElementId) {
80         for (RicConfig ricConfig : getRicConfigs()) {
81             if (ricConfig.managedElementIds().contains(managedElementId)) {
82                 return Optional.of(ricConfig);
83
84             }
85         }
86         return Optional.empty();
87     }
88
89     public void initialize() {
90         loadConfigurationFromFile();
91     }
92
93     Mono<Environment.Variables> getEnvironment(Properties systemEnvironment) {
94         return Environment.readEnvironmentVariables(systemEnvironment);
95     }
96
97     /**
98      * Reads the configuration from file.
99      */
100     void loadConfigurationFromFile() {
101         GsonBuilder gsonBuilder = new GsonBuilder();
102         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
103
104         try (InputStream inputStream = createInputStream(filepath)) {
105             JsonParser parser = new JsonParser();
106             JsonObject rootObject = getJsonElement(parser, inputStream).getAsJsonObject();
107             if (rootObject == null) {
108                 throw new JsonSyntaxException("Root is not a json object");
109             }
110             ApplicationConfigParser appParser = new ApplicationConfigParser();
111             appParser.parse(rootObject);
112             this.ricConfigs = appParser.getRicConfigs();
113             logger.info("Local configuration file loaded: {}", filepath);
114         } catch (JsonSyntaxException | ServiceException | IOException e) {
115             logger.trace("Local configuration file not loaded: {}", filepath, e);
116         }
117     }
118
119     JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
120         return parser.parse(new InputStreamReader(inputStream));
121     }
122
123     InputStream createInputStream(@NotNull String filepath) throws IOException {
124         return new BufferedInputStream(new FileInputStream(filepath));
125     }
126
127 }