2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
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=========================================================
22 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonObject;
26 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.CloudHttpClient;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.http.configuration.EnvProperties;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import reactor.core.publisher.Mono;
33 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
35 public final class ReactiveCloudConfigurationProvider implements CloudConfigurationProvider {
37 private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveCloudConfigurationProvider.class);
38 private static final String EXCEPTION_MESSAGE = "Unsupported method call: ";
40 private final CloudHttpClient cloudHttpClient;
42 public ReactiveCloudConfigurationProvider() {
43 this(new CloudHttpClient());
46 ReactiveCloudConfigurationProvider(
47 CloudHttpClient cloudHttpClient) {
48 this.cloudHttpClient = cloudHttpClient;
52 public Mono<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties) {
53 return callConsulForConfigBindingServiceEndpoint(envProperties)
54 .flatMap(this::callConfigBindingServiceForConfiguration);
58 public Mono<JsonObject> callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName,
60 throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this);
64 public JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName) {
65 throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this);
69 public JsonObject callForServiceConfiguration(EnvProperties envProperties) {
70 throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this);
73 private Mono<String> callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) {
74 LOGGER.info("Retrieving Config Binding Service endpoint from Consul");
75 return cloudHttpClient.callHttpGet(getConsulUrl(envProperties), JsonArray.class)
76 .flatMap(jsonArray -> this.createConfigBindingServiceUrl(jsonArray, envProperties.appName()));
79 private String getConsulUrl(EnvProperties envProperties) {
80 return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service",
81 envProperties.cbsName());
84 private Mono<JsonObject> callConfigBindingServiceForConfiguration(String configBindingServiceUri) {
85 LOGGER.info("Retrieving configuration");
86 return cloudHttpClient.callHttpGet(configBindingServiceUri, JsonObject.class);
90 private Mono<String> createConfigBindingServiceUrl(JsonArray jsonArray, String appName) {
91 return getConfigBindingObject(jsonArray)
92 .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName));
95 private Mono<String> buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) {
96 return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(),
97 jsonObject.get("ServicePort").getAsInt(), "/service_component", appName));
100 private Mono<JsonObject> getConfigBindingObject(JsonArray jsonArray) {
102 if (jsonArray.size() > 0) {
103 return Mono.just(jsonArray.get(0).getAsJsonObject());
105 throw new IllegalStateException("JSON Array was empty");
107 } catch (IllegalStateException e) {
108 LOGGER.warn("Failed to retrieve JSON Object from array", e);
109 return Mono.error(e);
113 private String getUri(String host, Integer port, String... paths) {
114 return new URI.URIBuilder()
118 .path(String.join("/", paths))