84c50836225c65daa1380ba0ab9d778172d9abe2
[nonrtric.git] / pmlog / src / main / java / org / oran / pmlog / configuration / ApplicationConfig.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 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.oran.pmlog.configuration;
22
23 import java.lang.invoke.MethodHandles;
24 import java.nio.charset.Charset;
25 import java.nio.file.Files;
26
27 import lombok.Getter;
28 import lombok.ToString;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.boot.context.properties.EnableConfigurationProperties;
34
35 @EnableConfigurationProperties
36 @ToString
37 public class ApplicationConfig {
38
39     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40     private static com.google.gson.Gson gson = new com.google.gson.GsonBuilder().disableHtmlEscaping().create();
41     private static final String JOB_DEFINITION_PATH = "./config/jobDefinition.json";
42
43     @Value("${server.ssl.key-store-type}")
44     private String sslKeyStoreType = "";
45
46     @Value("${server.ssl.key-store-password}")
47     private String sslKeyStorePassword = "";
48
49     @Value("${server.ssl.key-store}")
50     private String sslKeyStore = "";
51
52     @Value("${server.ssl.key-password}")
53     private String sslKeyPassword = "";
54
55     @Value("${app.webclient.trust-store-used}")
56     private boolean sslTrustStoreUsed = false;
57
58     @Value("${app.webclient.trust-store-password}")
59     private String sslTrustStorePassword = "";
60
61     @Value("${app.webclient.trust-store}")
62     private String sslTrustStore = "";
63
64     @Value("${app.webclient.http.proxy-host:}")
65     private String httpProxyHost = "";
66
67     @Value("${app.webclient.http.proxy-port:0}")
68     private int httpProxyPort = 0;
69
70     @Getter
71     @Value("${server.port}")
72     private int localServerHttpsPort;
73
74     @Getter
75     @Value("${app.kafka.max-poll-records:300}")
76     private int kafkaMaxPollRecords;
77
78     @Getter
79     @Value("${app.kafka.group-id}")
80     private String kafkaGroupId;
81
82     @Getter
83     @Value("${app.kafka.client-id}")
84     private String kafkaClientId;
85
86     @Value("${app.kafka.bootstrap-servers}")
87     private String kafkaBootstrapServers;
88
89     @Value("${app.kafka.input-topic}")
90     private String kafkaInputTopic;
91
92     @Getter
93     @Value("${app.influx.url}")
94     private String influxUrl;
95
96     @Getter
97     @Value("${app.influx.access-token}")
98     private String influxAccessToken;
99
100     @Getter
101     @Value("${app.ics-base-url}")
102     private String icsBaseUrl;
103
104     @Getter
105     @Value("${app.consumer-job-id:shouldHaveBeenDefinedInYaml}")
106     private String consumerJobId;
107
108     @Getter
109     @Value("${app.influx.user}")
110     private String influxUser;
111
112     @Getter
113     @Value("${app.influx.password}")
114     private String influxPassword;
115
116     @Getter
117     @Value("${app.influx.database}")
118     private String influxDatabase;
119
120     @Getter
121     @Value("${app.influx.bucket}")
122     private String influxBucket;
123
124     @Getter
125     @Value("${app.influx.org}")
126     private String influxOrg;
127
128     private WebClientConfig webClientConfig = null;
129
130     public WebClientConfig getWebClientConfig() {
131         if (this.webClientConfig == null) {
132             WebClientConfig.HttpProxyConfig httpProxyConfig = WebClientConfig.HttpProxyConfig.builder() //
133                     .httpProxyHost(this.httpProxyHost) //
134                     .httpProxyPort(this.httpProxyPort) //
135                     .build();
136
137             this.webClientConfig = WebClientConfig.builder() //
138                     .keyStoreType(this.sslKeyStoreType) //
139                     .keyStorePassword(this.sslKeyStorePassword) //
140                     .keyStore(this.sslKeyStore) //
141                     .keyPassword(this.sslKeyPassword) //
142                     .isTrustStoreUsed(this.sslTrustStoreUsed) //
143                     .trustStore(this.sslTrustStore) //
144                     .trustStorePassword(this.sslTrustStorePassword) //
145                     .httpProxyConfig(httpProxyConfig) //
146                     .build();
147         }
148         return this.webClientConfig;
149     }
150
151     public String getConsumerJobInfo() {
152
153         try {
154             return Files.readString(java.nio.file.Path.of(JOB_DEFINITION_PATH), Charset.defaultCharset());
155         } catch (Exception e) {
156             logger.error("Could not load configuration file: {}, reason: {}", JOB_DEFINITION_PATH, e.getMessage());
157             return "{}";
158         }
159     }
160
161     private ConsumerJobInfo.KafkaDeliveryInfo getKafkaDeliveryInfoFromAplicationYaml() {
162         return ConsumerJobInfo.KafkaDeliveryInfo.builder() //
163                 .bootStrapServers(this.kafkaBootstrapServers) //
164                 .topic(kafkaInputTopic) //
165                 .build();
166     }
167
168     private ConsumerJobInfo.KafkaDeliveryInfo getKafkaDeliveryInfo() {
169         try {
170             ConsumerJobInfo infoFromFile = gson.fromJson(getConsumerJobInfo(), ConsumerJobInfo.class);
171             if (infoFromFile != null && infoFromFile.jobDefinition != null
172                     && infoFromFile.jobDefinition.getDeliveryInfo() != null) {
173                 return infoFromFile.jobDefinition.getDeliveryInfo();
174             }
175
176         } catch (Exception e) {
177             logger.warn("Could not parse file: {}, reason: {}, falling back to parameters in Application.yaml",
178                     JOB_DEFINITION_PATH, e.getMessage());
179         }
180         return getKafkaDeliveryInfoFromAplicationYaml();
181
182     }
183
184     public String getKafkaInputTopic() {
185         return getKafkaDeliveryInfo().getTopic();
186     }
187
188     public String getKafkaBootStrapServers() {
189         return getKafkaDeliveryInfo().getBootStrapServers();
190     }
191
192 }