16a1dc3ac95823b5b596ff3a6426d1e688255663
[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 org.springframework.stereotype.Service;
31 import org.springframework.web.util.DefaultUriBuilderFactory;
32 import reactor.core.publisher.Mono;
33
34 /**
35  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 11/15/18
36  */
37 @Service
38 public final class ReactiveCloudConfigurationProvider implements CloudConfigurationProvider {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveCloudConfigurationProvider.class);
41     private static final String EXCEPTION_MESSAGE = "Unsupported method call: ";
42
43     private final CloudHttpClient cloudHttpClient;
44
45     public ReactiveCloudConfigurationProvider() {
46         this(new CloudHttpClient());
47     }
48
49     ReactiveCloudConfigurationProvider(
50         CloudHttpClient cloudHttpClient) {
51         this.cloudHttpClient = cloudHttpClient;
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         return cloudHttpClient.callHttpGet(getConsulUrl(envProperties), JsonArray.class)
79             .flatMap(jsonArray -> this.createConfigBindingServiceUrl(jsonArray, envProperties.appName()));
80     }
81
82     private String getConsulUrl(EnvProperties envProperties) {
83         return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service",
84             envProperties.cbsName());
85     }
86
87     private Mono<JsonObject> callConfigBindingServiceForConfiguration(String configBindingServiceUri) {
88         LOGGER.info("Retrieving configuration");
89         return cloudHttpClient.callHttpGet(configBindingServiceUri, JsonObject.class);
90     }
91
92
93     private Mono<String> createConfigBindingServiceUrl(JsonArray jsonArray, String appName) {
94         return getConfigBindingObject(jsonArray)
95             .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName));
96     }
97
98     private Mono<String> buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) {
99         return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(),
100             jsonObject.get("ServicePort").getAsInt(), "/service_component", appName));
101     }
102
103     private Mono<JsonObject> getConfigBindingObject(JsonArray jsonArray) {
104         try {
105             if (jsonArray.size() > 0) {
106                 return Mono.just(jsonArray.get(0).getAsJsonObject());
107             } else {
108                 throw new IllegalStateException("JSON Array was empty");
109             }
110         } catch (IllegalStateException e) {
111             LOGGER.warn("Failed to retrieve JSON Object from array", e);
112             return Mono.error(e);
113         }
114     }
115
116     private String getUri(String host, Integer port, String... paths) {
117         return new DefaultUriBuilderFactory().builder()
118             .scheme("http")
119             .host(host)
120             .port(port)
121             .path(String.join("/", paths))
122             .build().toString();
123     }
124
125 }