2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.configuration;
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;
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;
40 import javax.validation.constraints.NotEmpty;
41 import javax.validation.constraints.NotNull;
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;
54 @EnableConfigurationProperties
55 @ConfigurationProperties("app")
56 public class ApplicationConfig {
57 private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class);
59 @Value("#{systemEnvironment}")
60 Properties systemEnvironment;
63 private String filepath;
65 private Vector<RicConfig> ricConfigs;
68 public ApplicationConfig() {
71 public synchronized void setFilepath(String filepath) {
72 this.filepath = filepath;
75 public Vector<RicConfig> getRicConfigs() {
76 return this.ricConfigs;
79 public Optional<RicConfig> getRicConfig(String managedElementId) {
80 for (RicConfig ricConfig : getRicConfigs()) {
81 if (ricConfig.managedElementIds().contains(managedElementId)) {
82 return Optional.of(ricConfig);
86 return Optional.empty();
89 public void initialize() {
90 loadConfigurationFromFile();
93 Mono<Environment.Variables> getEnvironment(Properties systemEnvironment) {
94 return Environment.readEnvironmentVariables(systemEnvironment);
98 * Reads the configuration from file.
100 void loadConfigurationFromFile() {
101 GsonBuilder gsonBuilder = new GsonBuilder();
102 ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
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");
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);
119 JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
120 return parser.parse(new InputStreamReader(inputStream));
123 InputStream createInputStream(@NotNull String filepath) throws IOException {
124 return new BufferedInputStream(new FileInputStream(filepath));