NONRTRIC - Implement DMaaP mediator producer service in Java
[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     private WebClientConfig webClientConfig = null;
92
93     public WebClientConfig getWebClientConfig() {
94         if (this.webClientConfig == null) {
95             HttpProxyConfig httpProxyConfig = ImmutableHttpProxyConfig.builder() //
96                     .httpProxyHost(this.httpProxyHost) //
97                     .httpProxyPort(this.httpProxyPort) //
98                     .build();
99
100             this.webClientConfig = ImmutableWebClientConfig.builder() //
101                     .keyStoreType(this.sslKeyStoreType) //
102                     .keyStorePassword(this.sslKeyStorePassword) //
103                     .keyStore(this.sslKeyStore) //
104                     .keyPassword(this.sslKeyPassword) //
105                     .isTrustStoreUsed(this.sslTrustStoreUsed) //
106                     .trustStore(this.sslTrustStore) //
107                     .trustStorePassword(this.sslTrustStorePassword) //
108                     .httpProxyConfig(httpProxyConfig) //
109                     .build();
110         }
111         return this.webClientConfig;
112     }
113
114     // Adapter to parse the json format of the configuration file.
115     static class ConfigFile {
116         Collection<InfoType> types;
117     }
118
119     public Collection<InfoType> getTypes() {
120         com.google.gson.Gson gson = new com.google.gson.GsonBuilder().create();
121
122         try {
123             String configJson = Files.readString(Path.of(getLocalConfigurationFilePath()), Charset.defaultCharset());
124             ConfigFile configData = gson.fromJson(configJson, ConfigFile.class);
125             return configData.types;
126         } catch (Exception e) {
127             logger.error("Could not load configuration file {}", getLocalConfigurationFilePath());
128             return Collections.emptyList();
129         }
130
131     }
132
133 }