401db88152f24aa48948c67769a5937cfa14865c
[dcaegen2/services/prh.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
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.onap.dcaegen2.services.prh.configuration;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParser;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.nio.charset.StandardCharsets;
30 import java.util.Optional;
31 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration;
33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableCbsClientConfiguration;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.core.io.Resource;
39 import reactor.core.publisher.Mono;
40
41 @Configuration
42 public class ConsulConfigFileReader {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(ConsulConfigFileReader.class);
45     private static final int DEFAULT_CONSUL_PORT = 8500;
46     private ImmutableCbsClientConfiguration jsonCbsClientConfiguration;
47
48     @Value("classpath:consul_config.json")
49     private Resource consulConfig;
50
51     public Mono<CbsClientConfiguration> evaluate() {
52         initFileStreamReader();
53         CbsClientConfiguration cbsClientConfiguration = ImmutableCbsClientConfiguration.builder()
54                 .consulHost(jsonCbsClientConfiguration.consulHost())
55                 .consulPort(Optional.ofNullable(jsonCbsClientConfiguration.consulPort()).orElse(DEFAULT_CONSUL_PORT))
56                 .cbsName(jsonCbsClientConfiguration.cbsName())
57                 .appName(jsonCbsClientConfiguration.appName())
58                 .build();
59         LOGGER.info("Evaluated variables: {}", cbsClientConfiguration);
60         return Mono.just(cbsClientConfiguration);
61     }
62
63     private void initFileStreamReader() {
64         LOGGER.debug("Loading configuration from configuration file");
65         Gson gson = new Gson();
66         try (InputStream inputStream = consulConfig.getInputStream()) {
67             JsonElement rootElement = getJsonElement(inputStream);
68             if (rootElement.isJsonObject()) {
69                 jsonCbsClientConfiguration = gson.fromJson(rootElement, ImmutableCbsClientConfiguration.class);
70             }
71         } catch (IOException e) {
72             LOGGER.warn("Failed to load/parse file", e);
73         }
74     }
75
76     private JsonElement getJsonElement(InputStream inputStream) {
77         return new JsonParser().parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
78     }
79 }