2048b4261b323406866d6489593e4b6da12782f7
[nonrtric/plt/ranpm.git] / pmproducer / src / main / java / org / oran / pmproducer / 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.pmproducer.configuration;
22
23 import java.lang.invoke.MethodHandles;
24 import java.nio.charset.Charset;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.Collection;
28 import java.util.Collections;
29
30 import lombok.Getter;
31 import lombok.Setter;
32 import lombok.ToString;
33
34 import org.oran.pmproducer.configuration.WebClientConfig.HttpProxyConfig;
35 import org.oran.pmproducer.repository.InfoType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.boot.context.properties.EnableConfigurationProperties;
40
41 @EnableConfigurationProperties
42 @ToString
43 public class ApplicationConfig {
44
45     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46
47     @Getter
48     @Value("${app.configuration-filepath}")
49     private String localConfigurationFilePath;
50
51     @Value("${server.ssl.key-store-type}")
52     private String sslKeyStoreType = "";
53
54     @Value("${server.ssl.key-store-password}")
55     private String sslKeyStorePassword = "";
56
57     @Value("${server.ssl.key-store}")
58     private String sslKeyStore = "";
59
60     @Value("${server.ssl.key-password}")
61     private String sslKeyPassword = "";
62
63     @Value("${app.webclient.trust-store-used}")
64     private boolean sslTrustStoreUsed = false;
65
66     @Value("${app.webclient.trust-store-password}")
67     private String sslTrustStorePassword = "";
68
69     @Value("${app.webclient.trust-store}")
70     private String sslTrustStore = "";
71
72     @Value("${app.webclient.http.proxy-host:}")
73     private String httpProxyHost = "";
74
75     @Value("${app.webclient.http.proxy-port:0}")
76     private int httpProxyPort = 0;
77
78     @Getter
79     @Setter
80     @Value("${server.port}")
81     private int localServerHttpPort;
82
83     @Getter
84     @Value("${app.ics-base-url}")
85     private String icsBaseUrl;
86
87     @Getter
88     @Value("${app.pm_producer-base-url}")
89     private String selfUrl;
90
91     @Getter
92     @Value("${app.dmaap-base-url}")
93     private String dmaapBaseUrl;
94
95     @Getter
96     @Value("${app.kafka.bootstrap-servers:}")
97     private String kafkaBootStrapServers;
98
99     @Getter
100     @Value("${app.kafka.max-poll-records:300}")
101     private int kafkaMaxPollRecords;
102
103     @Getter
104     @Value("${app.pm-files-path:}")
105     private String pmFilesPath;
106
107     @Getter
108     @Value("${app.s3.endpointOverride:}")
109     private String s3EndpointOverride;
110
111     @Getter
112     @Value("${app.s3.accessKeyId:}")
113     private String s3AccessKeyId;
114
115     @Getter
116     @Value("${app.s3.secretAccessKey:}")
117     private String s3SecretAccessKey;
118
119     @Getter
120     @Value("${app.s3.locksBucket:}")
121     private String s3LocksBucket;
122
123     @Getter
124     @Value("${app.s3.bucket:}")
125     private String s3Bucket;
126
127     @Getter
128     @Setter
129     @Value("${app.zip-output:}")
130     private boolean zipOutput;
131
132     private WebClientConfig webClientConfig = null;
133
134     public WebClientConfig getWebClientConfig() {
135         if (this.webClientConfig == null) {
136             HttpProxyConfig httpProxyConfig = HttpProxyConfig.builder() //
137                     .httpProxyHost(this.httpProxyHost) //
138                     .httpProxyPort(this.httpProxyPort) //
139                     .build();
140
141             this.webClientConfig = WebClientConfig.builder() //
142                     .keyStoreType(this.sslKeyStoreType) //
143                     .keyStorePassword(this.sslKeyStorePassword) //
144                     .keyStore(this.sslKeyStore) //
145                     .keyPassword(this.sslKeyPassword) //
146                     .isTrustStoreUsed(this.sslTrustStoreUsed) //
147                     .trustStore(this.sslTrustStore) //
148                     .trustStorePassword(this.sslTrustStorePassword) //
149                     .httpProxyConfig(httpProxyConfig) //
150                     .build();
151         }
152         return this.webClientConfig;
153     }
154
155     public boolean isS3Enabled() {
156         return !(s3EndpointOverride.isBlank() || s3Bucket.isBlank());
157     }
158
159     // Adapter to parse the json format of the configuration file.
160     static class ConfigFile {
161         Collection<InfoType> types;
162     }
163
164     public Collection<InfoType> getTypes() {
165         com.google.gson.Gson gson = new com.google.gson.GsonBuilder().disableHtmlEscaping().create();
166         try {
167             String configJson = Files.readString(Path.of(getLocalConfigurationFilePath()), Charset.defaultCharset());
168             ConfigFile configData = gson.fromJson(configJson, ConfigFile.class);
169             return configData.types;
170         } catch (Exception e) {
171             logger.error("Could not load configuration file {}", getLocalConfigurationFilePath());
172             return Collections.emptyList();
173         }
174
175     }
176
177 }