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