27d365836b282c155bfb0f3b4480e398e558f733
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2018-2019 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.adapters.http.HttpMethod;
27 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpRequest;
28 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest;
29 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient;
30 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
31 import org.onap.dcaegen2.services.sdk.rest.services.uri.URI;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import reactor.core.publisher.Mono;
35
36 /**
37  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
38  */
39 public final class ReactiveCloudConfigurationProvider implements CloudConfigurationProvider {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveCloudConfigurationProvider.class);
42     private static final String EXCEPTION_MESSAGE = "Unsupported method call: ";
43
44     private final RxHttpClient rxHttpClient;
45
46     public ReactiveCloudConfigurationProvider() {
47         this(RxHttpClient.create());
48     }
49
50     ReactiveCloudConfigurationProvider(RxHttpClient rxHttpClient) {
51         this.rxHttpClient = rxHttpClient;
52     }
53
54     @Override
55     public Mono<JsonObject> callForServiceConfigurationReactive(EnvProperties envProperties) {
56         return callConsulForConfigBindingServiceEndpoint(envProperties)
57             .flatMap(this::callConfigBindingServiceForConfiguration);
58     }
59
60     @Override
61     public Mono<JsonObject> callForServiceConfigurationReactive(String consulHost, int consulPort, String cbsName,
62         String appName) {
63         throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this);
64     }
65
66     @Override
67     public JsonObject callForServiceConfiguration(String consulHost, int consulPort, String cbsName, String appName) {
68         throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this);
69     }
70
71     @Override
72     public JsonObject callForServiceConfiguration(EnvProperties envProperties) {
73         throw new UnsupportedOperationException(EXCEPTION_MESSAGE + this);
74     }
75
76     private Mono<String> callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) {
77         LOGGER.info("Retrieving Config Binding Service endpoint from Consul");
78
79         HttpRequest httpRequest = ImmutableHttpRequest.builder()
80                 .url(getConsulUrl(envProperties)).method(HttpMethod.GET).build();
81
82         return rxHttpClient.call(httpRequest)
83                 .map(resp -> resp.bodyAsJson(JsonArray.class))
84                 .flatMap(jsonArray ->
85                         this.createConfigBindingServiceUrl(
86                                 jsonArray,
87                                 envProperties.appName())
88                 );
89     }
90
91     private String getConsulUrl(EnvProperties envProperties) {
92         return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service",
93             envProperties.cbsName());
94     }
95
96     private Mono<JsonObject> callConfigBindingServiceForConfiguration(String configBindingServiceUri) {
97         LOGGER.info("Retrieving configuration");
98         HttpRequest httpRequest = ImmutableHttpRequest.builder()
99                 .url(configBindingServiceUri).method(HttpMethod.GET).build();
100
101         return rxHttpClient.call(httpRequest)
102                 .map(httpResponse -> httpResponse.bodyAsJson(JsonObject.class));
103     }
104
105
106     private Mono<String> createConfigBindingServiceUrl(JsonArray jsonArray, String appName) {
107         return getConfigBindingObject(jsonArray)
108             .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName));
109     }
110
111     private Mono<String> buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) {
112         return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(),
113             jsonObject.get("ServicePort").getAsInt(), "/service_component", appName));
114     }
115
116     private Mono<JsonObject> getConfigBindingObject(JsonArray jsonArray) {
117         try {
118             if (jsonArray.size() > 0) {
119                 return Mono.just(jsonArray.get(0).getAsJsonObject());
120             } else {
121                 throw new IllegalStateException("JSON Array was empty");
122             }
123         } catch (IllegalStateException e) {
124             LOGGER.warn("Failed to retrieve JSON Object from array", e);
125             return Mono.error(e);
126         }
127     }
128
129     private String getUri(String host, Integer port, String... paths) {
130         return new URI.URIBuilder()
131             .scheme("http")
132             .host(host)
133             .port(port)
134             .path(String.join("/", paths))
135             .build().toString();
136     }
137
138 }