a0b4a6f0c1c77585b0566909d2f0dc53af3e2083
[dcaegen2/services/sdk.git] /
1 /*
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
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
22 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.providers;
23
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;
31
32 /**
33  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
34  */
35 public final class ReactiveCloudConfigurationProvider implements CloudConfigurationProvider {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveCloudConfigurationProvider.class);
38     private static final String EXCEPTION_MESSAGE = "Unsupported method call: ";
39
40     private final CloudHttpClient cloudHttpClient;
41
42     public ReactiveCloudConfigurationProvider() {
43         this(new CloudHttpClient());
44     }
45
46     ReactiveCloudConfigurationProvider(
47         CloudHttpClient cloudHttpClient) {
48         this.cloudHttpClient = cloudHttpClient;
49     }
50
51     @Override
52     public Mono<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties) {
53         return callConsulForConfigBindingServiceEndpoint(envProperties)
54             .flatMap(this::callConfigBindingServiceForConfiguration);
55     }
56
57     @Override
58     public Mono<JsonObject> callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName,
59         String appName) {
60         throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this);
61     }
62
63     @Override
64     public JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName) {
65         throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this);
66     }
67
68     @Override
69     public JsonObject callForServiceConfiguration(EnvProperties envProperties) {
70         throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this);
71     }
72
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()));
77     }
78
79     private String getConsulUrl(EnvProperties envProperties) {
80         return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service",
81             envProperties.cbsName());
82     }
83
84     private Mono<JsonObject> callConfigBindingServiceForConfiguration(String configBindingServiceUri) {
85         LOGGER.info("Retrieving configuration");
86         return cloudHttpClient.callHttpGet(configBindingServiceUri, JsonObject.class);
87     }
88
89
90     private Mono<String> createConfigBindingServiceUrl(JsonArray jsonArray, String appName) {
91         return getConfigBindingObject(jsonArray)
92             .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName));
93     }
94
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));
98     }
99
100     private Mono<JsonObject> getConfigBindingObject(JsonArray jsonArray) {
101         try {
102             if (jsonArray.size() > 0) {
103                 return Mono.just(jsonArray.get(0).getAsJsonObject());
104             } else {
105                 throw new IllegalStateException("JSON Array was empty");
106             }
107         } catch (IllegalStateException e) {
108             LOGGER.warn("Failed to retrieve JSON Object from array", e);
109             return Mono.error(e);
110         }
111     }
112
113     private String getUri(String host, Integer port, String... paths) {
114         return new URI.URIBuilder()
115             .scheme("http")
116             .host(host)
117             .port(port)
118             .path(String.join("/", paths))
119             .build().toString();
120     }
121
122 }