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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.services.prh.configuration;
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.EnvProperties;
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.context.annotation.Configuration;
37 import org.springframework.core.io.Resource;
38 import reactor.core.publisher.Mono;
41 public class ConsulConfigFileReader {
43 private static final Logger LOGGER = LoggerFactory.getLogger(ConsulConfigFileReader.class);
44 private static final int DEFAULT_CONSUL_PORT = 8500;
45 private ImmutableEnvProperties jsonEnvProperties;
47 @Value("classpath:consul_config.json")
48 private Resource consulConfig;
50 public Mono<EnvProperties> evaluate() {
51 initFileStreamReader();
52 EnvProperties envProperties = ImmutableEnvProperties.builder()
53 .consulHost(jsonEnvProperties.consulHost())
54 .consulPort(Optional.ofNullable(jsonEnvProperties.consulPort()).orElse(DEFAULT_CONSUL_PORT))
55 .cbsName(jsonEnvProperties.cbsName())
56 .appName(jsonEnvProperties.appName())
58 LOGGER.info("Evaluated variables: {}", envProperties);
59 return Mono.just(envProperties);
62 private void initFileStreamReader() {
63 LOGGER.debug("Loading configuration from configuration file");
64 Gson gson = new Gson();
65 try (InputStream inputStream = consulConfig.getInputStream()) {
66 JsonElement rootElement = getJsonElement(inputStream);
67 if (rootElement.isJsonObject()) {
68 jsonEnvProperties = gson.fromJson(rootElement, ImmutableEnvProperties.class);
70 } catch (IOException e) {
71 LOGGER.warn("Failed to load/parse file", e);
75 private JsonElement getJsonElement(InputStream inputStream) {
76 return new JsonParser().parse(new InputStreamReader(inputStream, StandardCharsets.UTF_8));