3ea64e71add2ad9a42f8a80b3ab41cd2eef62bac
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / configuration / ApplicationConfig.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 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.dmaapadapter.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
33 import org.oran.dmaapadapter.configuration.WebClientConfig.HttpProxyConfig;
34 import org.oran.dmaapadapter.repository.InfoType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.boot.context.properties.EnableConfigurationProperties;
39
40 @EnableConfigurationProperties
41 public class ApplicationConfig {
42
43     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
44
45     @Getter
46     @Value("${app.configuration-filepath}")
47     private String localConfigurationFilePath;
48
49     @Value("${server.ssl.key-store-type}")
50     private String sslKeyStoreType = "";
51
52     @Value("${server.ssl.key-store-password}")
53     private String sslKeyStorePassword = "";
54
55     @Value("${server.ssl.key-store}")
56     private String sslKeyStore = "";
57
58     @Value("${server.ssl.key-password}")
59     private String sslKeyPassword = "";
60
61     @Value("${app.webclient.trust-store-used}")
62     private boolean sslTrustStoreUsed = false;
63
64     @Value("${app.webclient.trust-store-password}")
65     private String sslTrustStorePassword = "";
66
67     @Value("${app.webclient.trust-store}")
68     private String sslTrustStore = "";
69
70     @Value("${app.webclient.http.proxy-host:\"\"}")
71     private String httpProxyHost = "";
72
73     @Value("${app.webclient.http.proxy-port:0}")
74     private int httpProxyPort = 0;
75
76     @Getter
77     @Setter
78     @Value("${server.port}")
79     private int localServerHttpPort;
80
81     @Getter
82     @Value("${app.ics-base-url}")
83     private String icsBaseUrl;
84
85     @Getter
86     @Value("${app.dmaap-adapter-base-url}")
87     private String selfUrl;
88
89     @Getter
90     @Value("${app.dmaap-base-url}")
91     private String dmaapBaseUrl;
92
93     @Getter
94     @Value("${app.kafka.bootstrap-servers:}")
95     private String kafkaBootStrapServers;
96
97     private WebClientConfig webClientConfig = null;
98
99     public WebClientConfig getWebClientConfig() {
100         if (this.webClientConfig == null) {
101             HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
102                     .httpProxyHost(this.httpProxyHost) //
103                     .httpProxyPort(this.httpProxyPort) //
104                     .build();
105
106             this.webClientConfig = ImmutableWebClientConfig.builder() //
107                     .keyStoreType(this.sslKeyStoreType) //
108                     .keyStorePassword(this.sslKeyStorePassword) //
109                     .keyStore(this.sslKeyStore) //
110                     .keyPassword(this.sslKeyPassword) //
111                     .isTrustStoreUsed(this.sslTrustStoreUsed) //
112                     .trustStore(this.sslTrustStore) //
113                     .trustStorePassword(this.sslTrustStorePassword) //
114                     .httpProxyConfig(httpProxyConfig) //
115                     .build();
116         }
117         return this.webClientConfig;
118     }
119
120     // Adapter to parse the json format of the configuration file.
121     static class ConfigFile {
122         Collection<InfoType> types;
123     }
124
125     public Collection<InfoType> getTypes() {
126         com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
127
128         try {
129             String configJson = Files.readString(Path.of(getLocalConfigurationFilePath()), Charset.defaultCharset());
130             ConfigFile configData = gson.fromJson(configJson, ConfigFile.class);
131             return configData.types;
132         } catch (Exception e) {
133             logger.error("Could not load configuration file {}", getLocalConfigurationFilePath());
134             return Collections.emptyList();
135         }
136
137     }
138
139 }