2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.dcaegen2.collectors.datafile.service;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonObject;
23 import org.onap.dcaegen2.collectors.datafile.model.EnvProperties;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.stereotype.Service;
27 import org.springframework.web.util.DefaultUriBuilderFactory;
28 import reactor.core.publisher.Mono;
31 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
34 public class DatafileConfigurationProvider {
36 private static final Logger logger = LoggerFactory.getLogger(DatafileConfigurationProvider.class);
38 private final HttpGetClient httpGetClient;
40 DatafileConfigurationProvider() {
41 this(new HttpGetClient());
44 DatafileConfigurationProvider(HttpGetClient httpGetClient) {
45 this.httpGetClient = httpGetClient;
48 public Mono<JsonObject> callForDataFileCollectorConfiguration(EnvProperties envProperties) {
49 return callConsulForConfigBindingServiceEndpoint(envProperties)
50 .flatMap(this::callConfigBindingServiceForDatafileConfiguration);
53 private Mono<String> callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) {
54 logger.info("Retrieving Config Binding Service endpoint from Consul");
55 return httpGetClient.callHttpGet(getConsulUrl(envProperties), JsonArray.class)
56 .flatMap(jsonArray -> this.createConfigBindingServiceUrl(jsonArray, envProperties.appName()));
60 private String getConsulUrl(EnvProperties envProperties) {
61 return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service",
62 envProperties.cbsName());
65 private Mono<JsonObject> callConfigBindingServiceForDatafileConfiguration(String configBindingServiceUri) {
66 logger.info("Retrieving Datafile configuration");
67 return httpGetClient.callHttpGet(configBindingServiceUri, JsonObject.class);
71 private Mono<String> createConfigBindingServiceUrl(JsonArray jsonArray, String appName) {
72 return getConfigBindingObject(jsonArray)
73 .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName));
76 private Mono<String> buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) {
77 return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(),
78 jsonObject.get("ServicePort").getAsInt(), "/service_component", appName));
81 private Mono<JsonObject> getConfigBindingObject(JsonArray jsonArray) {
83 if (jsonArray.size() > 0) {
84 return Mono.just(jsonArray.get(0).getAsJsonObject());
86 throw new IllegalStateException("JSON Array was empty");
88 } catch (IllegalStateException e) {
89 logger.warn("Failed to retrieve JSON Object from array", e);
94 private String getUri(String host, Integer port, String... paths) {
95 return new DefaultUriBuilderFactory().builder()
99 .path(String.join("/", paths))