aca87e5b5a3725915ac99265705c06bf63db80cb
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.dcaegen2.collectors.datafile.service;
20
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonObject;
23 import org.onap.dcaegen2.collectors.datafile.model.EnvProperties;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.stereotype.Service;
27 import org.springframework.web.util.DefaultUriBuilderFactory;
28 import reactor.core.publisher.Mono;
29
30 /**
31  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 9/19/18
32  */
33 @Service
34 public class DatafileConfigurationProvider {
35
36     private static final Logger logger = LoggerFactory.getLogger(DatafileConfigurationProvider.class);
37
38     private final HttpGetClient httpGetClient;
39
40     DatafileConfigurationProvider() {
41         this(new HttpGetClient());
42     }
43
44     DatafileConfigurationProvider(HttpGetClient httpGetClient) {
45         this.httpGetClient = httpGetClient;
46     }
47
48     public Mono<JsonObject> callForDataFileCollectorConfiguration(EnvProperties envProperties) {
49         return callConsulForConfigBindingServiceEndpoint(envProperties)
50             .flatMap(this::callConfigBindingServiceForDatafileConfiguration);
51     }
52
53     private Mono<String> callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) {
54         logger.info("Retrieving Config Binding Service endpoint from Consul");
55         return httpGetClient.callHttpGet(getConsulUrl(envProperties), JsonArray.class)
56             .flatMap(jsonArray -> this.createConfigBindingServiceUrl(jsonArray, envProperties.appName()));
57
58     }
59
60     private String getConsulUrl(EnvProperties envProperties) {
61         return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service",
62             envProperties.cbsName());
63     }
64
65     private Mono<JsonObject> callConfigBindingServiceForDatafileConfiguration(String configBindingServiceUri) {
66         logger.info("Retrieving Datafile configuration");
67         return httpGetClient.callHttpGet(configBindingServiceUri, JsonObject.class);
68     }
69
70
71     private Mono<String> createConfigBindingServiceUrl(JsonArray jsonArray, String appName) {
72         return getConfigBindingObject(jsonArray)
73             .flatMap(jsonObject -> buildConfigBindingServiceUrl(jsonObject, appName));
74     }
75
76     private Mono<String> buildConfigBindingServiceUrl(JsonObject jsonObject, String appName) {
77         return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(),
78             jsonObject.get("ServicePort").getAsInt(), "/service_component", appName));
79     }
80
81     private Mono<JsonObject> getConfigBindingObject(JsonArray jsonArray) {
82         try {
83             if (jsonArray.size() > 0) {
84                 return Mono.just(jsonArray.get(0).getAsJsonObject());
85             } else {
86                 throw new IllegalStateException("JSON Array was empty");
87             }
88         } catch (IllegalStateException e) {
89             logger.warn("Failed to retrieve JSON Object from array", e);
90             return Mono.error(e);
91         }
92     }
93
94     private String getUri(String host, Integer port, String... paths) {
95         return new DefaultUriBuilderFactory().builder()
96             .scheme("http")
97             .host(host)
98             .port(port)
99             .path(String.join("/", paths))
100             .build().toString();
101     }
102 }