Merge "Continue work with PolicyControl"
[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 reactor.core.publisher.Mono;
51
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     private Vector<RicConfig> ricConfigs;
64
65     @Autowired
66     public ApplicationConfig() {
67     }
68
69     public synchronized void setFilepath(String filepath) {
70         this.filepath = filepath;
71     }
72
73     public Vector<RicConfig> getRicConfigs() {
74         return this.ricConfigs;
75     }
76
77     public Optional<RicConfig> lookupRicConfigForManagedElement(String managedElementId) {
78         for (RicConfig ricConfig : getRicConfigs()) {
79             if (ricConfig.managedElementIds().contains(managedElementId)) {
80                 return Optional.of(ricConfig);
81             }
82         }
83         return Optional.empty();
84     }
85
86     public RicConfig getRic(String ricName) throws ServiceException {
87         for (RicConfig ricConfig : getRicConfigs()) {
88             if (ricConfig.name().equals(ricName)) {
89                 return ricConfig;
90             }
91         }
92         throw new ServiceException("Could not find ric: " + ricName);
93     }
94
95     public void initialize() {
96         loadConfigurationFromFile(this.filepath);
97     }
98
99     Mono<Environment.Variables> getEnvironment(Properties systemEnvironment) {
100         return Environment.readEnvironmentVariables(systemEnvironment);
101     }
102
103     /**
104      * Reads the configuration from file.
105      */
106     protected void loadConfigurationFromFile(String filepath) {
107         GsonBuilder gsonBuilder = new GsonBuilder();
108         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
109
110         try (InputStream inputStream = createInputStream(filepath)) {
111             JsonParser parser = new JsonParser();
112             JsonObject rootObject = getJsonElement(parser, inputStream).getAsJsonObject();
113             if (rootObject == null) {
114                 throw new JsonSyntaxException("Root is not a json object");
115             }
116             ApplicationConfigParser appParser = new ApplicationConfigParser();
117             appParser.parse(rootObject);
118             this.ricConfigs = appParser.getRicConfigs();
119             logger.info("Local configuration file loaded: {}", filepath);
120         } catch (JsonSyntaxException | ServiceException | IOException e) {
121             logger.trace("Local configuration file not loaded: {}", filepath, e);
122         }
123     }
124
125     JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
126         return parser.parse(new InputStreamReader(inputStream));
127     }
128
129     InputStream createInputStream(@NotNull String filepath) throws IOException {
130         return new BufferedInputStream(new FileInputStream(filepath));
131     }
132
133 }