diff options
author | Jakub Zieba <jakub.zieba@nokia.com> | 2018-08-28 14:07:56 +0200 |
---|---|---|
committer | Jakub Zieba <jakub.zieba@nokia.com> | 2018-08-29 13:09:46 +0200 |
commit | 4fbb9e8ad097c9ec3c3434ba257e887fb3a64699 (patch) | |
tree | b244acb1fd41b987afd514205636f16fe6d5e76e /prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java | |
parent | 5b4e3b195960af161604eccb1c1c805bb91c6648 (diff) |
Revert web server to prh, fix configuration
Revert web server to prh, becaue it is used by health check
Fix cloud configuration fetching
Additional code refactoring
Change-Id: Ic3d6f4c266064436e2701ec3ef3a7534b2d5b8b4
Issue-ID: DCAEGEN2-696
Signed-off-by: Jakub Zieba <jakub.zieba@nokia.com>
Diffstat (limited to 'prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java')
-rw-r--r-- | prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java new file mode 100644 index 00000000..7af4a7c8 --- /dev/null +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/service/PrhConfigurationProvider.java @@ -0,0 +1,115 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.services.prh.service; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import org.apache.http.client.utils.URIBuilder; +import org.onap.dcaegen2.services.prh.model.EnvProperties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +import java.net.URISyntaxException; + +/** + * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 8/10/18 + */ + +@Service +public class PrhConfigurationProvider { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + private final HttpGetClient httpGetClient; + + PrhConfigurationProvider() { + this(new HttpGetClient()); + } + + PrhConfigurationProvider(HttpGetClient httpGetClient) { + this.httpGetClient = httpGetClient; + } + + public Mono<JsonObject> callForPrhConfiguration(EnvProperties envProperties) { + return callConsulForConfigBindingServiceEndpoint(envProperties) + .flatMap(this::callConfigBindingServiceForPrhConfiguration); + } + + private Mono<String> callConsulForConfigBindingServiceEndpoint(EnvProperties envProperties) { + logger.info("Retrieving Config Binding Service endpoint from Consul"); + try { + return httpGetClient.callHttpGet(getConsulUrl(envProperties), JsonArray.class) + .flatMap(jsonArray -> this.createConfigBindingServiceURL(jsonArray, envProperties.appName())); + } catch (URISyntaxException e) { + logger.warn("Malformed Consul uri", e); + return Mono.error(e); + } + } + + private String getConsulUrl(EnvProperties envProperties) throws URISyntaxException { + return getUri(envProperties.consulHost(), envProperties.consulPort(), "/v1/catalog/service", + envProperties.cbsName()); + } + + private Mono<JsonObject> callConfigBindingServiceForPrhConfiguration(String configBindingServiceUri) { + logger.info("Retrieving PRH configuration"); + return httpGetClient.callHttpGet(configBindingServiceUri, JsonObject.class); + } + + + private Mono<String> createConfigBindingServiceURL(JsonArray jsonArray, String appName) { + return getConfigBindingObject(jsonArray).flatMap(jsonObject -> buildConfigBindingServiceURL(jsonObject, appName)); + } + + private Mono<String> buildConfigBindingServiceURL(JsonObject jsonObject, String appName) { + try { + return Mono.just(getUri(jsonObject.get("ServiceAddress").getAsString(), + jsonObject.get("ServicePort").getAsInt(), "/service_component", appName)); + } catch (URISyntaxException e) { + logger.warn("Malformed Config Binding Service uri", e); + return Mono.error(e); + } + } + + private Mono<JsonObject> getConfigBindingObject(JsonArray jsonArray) { + try { + if (jsonArray.size() > 0) { + return Mono.just(jsonArray.get(0).getAsJsonObject()); + } else { + throw new IllegalStateException("JSON Array was empty"); + } + } catch (IllegalStateException e) { + logger.warn("Failed to retrieve JSON Object from array", e); + return Mono.error(e); + } + } + + private String getUri(String host, Integer port, String... paths) throws URISyntaxException { + return new URIBuilder() + .setScheme("http") + .setHost(host) + .setPort(port) + .setPath(String.join("/", paths)) + .build().toString(); + } +} |