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