2  * ================================================================================
 
   3  * Copyright (c) 2018 AT&T 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
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  20 package org.onap.dcae.analytics.model.util.supplier;
 
  23 import java.util.List;
 
  24 import java.util.Optional;
 
  25 import java.util.function.Function;
 
  26 import java.util.function.Supplier;
 
  28 import org.onap.dcae.analytics.model.configbindingservice.ConfigBindingServiceConstants;
 
  29 import org.onap.dcae.analytics.model.configbindingservice.ConsulConfigBindingServiceQueryResponse;
 
  30 import org.onap.dcae.analytics.model.util.function.StringToURLFunction;
 
  31 import org.onap.dcae.analytics.model.util.function.URLToHttpGetFunction;
 
  32 import org.onap.dcae.analytics.model.util.json.AnalyticsModelJsonConversion;
 
  33 import org.slf4j.Logger;
 
  34 import org.slf4j.LoggerFactory;
 
  37  * A Function which fetches ApplicationProperties configuration from
 
  38  * Config Binding Service when deployed via docker, typically during application startup time.
 
  40  * @author Rajiv Singla
 
  42 public class ConfigBindingServiceJsonSupplier implements Supplier<Optional<String>> {
 
  44     private static final Logger logger = LoggerFactory.getLogger(ConfigBindingServiceJsonSupplier.class);
 
  46     private final Function<String, Optional<String>> fetchUrlContentFunction;
 
  48     public ConfigBindingServiceJsonSupplier(final Function<String, Optional<String>> fetchUrlContentFunction) {
 
  49         this.fetchUrlContentFunction = fetchUrlContentFunction;
 
  52     public ConfigBindingServiceJsonSupplier() {
 
  53         fetchUrlContentFunction = (String s) -> new StringToURLFunction().apply(s).flatMap(new URLToHttpGetFunction());
 
  57     public Optional<String> get() {
 
  59         logger.info("Consul Host Environment Variable: {}",
 
  60                 ConfigBindingServiceConstants.CONSUL_HOST_ENV_VARIABLE_VALUE);
 
  61         logger.info("Config Binding Service Environment Variable: {}",
 
  62                 ConfigBindingServiceConstants.CONFIG_BINDING_SERVICE_ENV_VARIABLE_VALUE);
 
  63         logger.info("Service Name Environment Variable: {}",
 
  64                 ConfigBindingServiceConstants.SERVICE_NAME_ENV_VARIABLE_VALUE);
 
  66         if (ConfigBindingServiceConstants.CONSUL_HOST_ENV_VARIABLE_VALUE == null ||
 
  67                 ConfigBindingServiceConstants.CONFIG_BINDING_SERVICE_ENV_VARIABLE_VALUE == null ||
 
  68                 ConfigBindingServiceConstants.SERVICE_NAME_ENV_VARIABLE_VALUE == null) {
 
  69             logger.error("Environment variables required to query Config Binding Service are not present");
 
  70             return Optional.empty();
 
  73         return Optional.of(ConfigBindingServiceConstants.CONSUL_QUERY_URL_STRING)
 
  74                 // Step 1: Query CONSUL to get the IP/PORT of CONFIG BINDING SERVICE
 
  75                 .flatMap(fetchUrlContentFunction)
 
  76                 // Step 2: Fetch the generated configurations from CONFIG BINDING SERVICE
 
  77                 .flatMap(ConfigBindingServiceJsonSupplier::parseConsulConfigBindingServiceQueryResponseJson)
 
  78                 // Step 3: create url from service address and service port
 
  79                 .flatMap(ConfigBindingServiceJsonSupplier::createConfigServiceURL)
 
  80                 // Step 4: Fetch final config binding service generated application configuration json string
 
  81                 .flatMap(fetchUrlContentFunction);
 
  85      * Creates URL using config binding service ip address and port
 
  87      * @param consulConfigBindingServiceQueryResponse consul config binding service query response containing config
 
  88      * binding service address and service port
 
  90      * @return config service url to fetch service configuration
 
  92     private static Optional<String> createConfigServiceURL(final ConsulConfigBindingServiceQueryResponse
 
  93                                                                    consulConfigBindingServiceQueryResponse) {
 
  94         final String configBindingServiceAddress = consulConfigBindingServiceQueryResponse.getServiceAddress();
 
  95         final Integer configServicePort = consulConfigBindingServiceQueryResponse.getServicePort();
 
  97         if (configBindingServiceAddress == null && configServicePort == null) {
 
  98             logger.error("Config Binding Service Address & Port are not present.");
 
  99             return Optional.empty();
 
 102         return Optional.of(String.format(ConfigBindingServiceConstants.CONFIG_SERVICE_QUERY_URL_STRING,
 
 103                 configBindingServiceAddress, configServicePort,
 
 104                 ConfigBindingServiceConstants.SERVICE_NAME_ENV_VARIABLE_VALUE));
 
 108     private static Optional<ConsulConfigBindingServiceQueryResponse>
 
 109     parseConsulConfigBindingServiceQueryResponseJson(final String configBindingServiceQueryResponseJson) {
 
 111         final Optional<List<ConsulConfigBindingServiceQueryResponse>> configBindingServiceQueryResponseOptional =
 
 112                 AnalyticsModelJsonConversion.CONFIG_BINDING_SERVICE_LIST_JSON_FUNCTION
 
 113                         .apply(configBindingServiceQueryResponseJson);
 
 115         // check parsing is successful and at least 1 config binding query response is present
 
 116         if (!configBindingServiceQueryResponseOptional.isPresent() ||
 
 117                 configBindingServiceQueryResponseOptional.get().isEmpty()) {
 
 118             logger.error("No Consul config binding service information found in JSON: {} ",
 
 119                     configBindingServiceQueryResponseJson);
 
 120             return Optional.empty();
 
 123         // return first consul query response
 
 124         return Optional.of(configBindingServiceQueryResponseOptional.get().get(0));