Update DCAE Committer in INFO.yaml
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-model / src / main / java / org / onap / dcae / analytics / model / util / supplier / ConfigBindingServiceJsonSupplier.java
1 /*
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
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
20 package org.onap.dcae.analytics.model.util.supplier;
21
22
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.function.Function;
26 import java.util.function.Supplier;
27
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;
35
36 /**
37  * A Function which fetches ApplicationProperties configuration from
38  * Config Binding Service when deployed via docker, typically during application startup time.
39  *
40  * @author Rajiv Singla
41  */
42 public class ConfigBindingServiceJsonSupplier implements Supplier<Optional<String>> {
43
44     private static final Logger logger = LoggerFactory.getLogger(ConfigBindingServiceJsonSupplier.class);
45
46     private final Function<String, Optional<String>> fetchUrlContentFunction;
47
48     public ConfigBindingServiceJsonSupplier(final Function<String, Optional<String>> fetchUrlContentFunction) {
49         this.fetchUrlContentFunction = fetchUrlContentFunction;
50     }
51
52     public ConfigBindingServiceJsonSupplier() {
53         fetchUrlContentFunction = (String s) -> new StringToURLFunction().apply(s).flatMap(new URLToHttpGetFunction());
54     }
55
56     @Override
57     public Optional<String> get() {
58
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);
65
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();
71         }
72
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);
82     }
83
84     /**
85      * Creates URL using config binding service ip address and port
86      *
87      * @param consulConfigBindingServiceQueryResponse consul config binding service query response containing config
88      * binding service address and service port
89      *
90      * @return config service url to fetch service configuration
91      */
92     private static Optional<String> createConfigServiceURL(final ConsulConfigBindingServiceQueryResponse
93                                                                    consulConfigBindingServiceQueryResponse) {
94         final String configBindingServiceAddress = consulConfigBindingServiceQueryResponse.getServiceAddress();
95         final Integer configServicePort = consulConfigBindingServiceQueryResponse.getServicePort();
96
97         if (configBindingServiceAddress == null && configServicePort == null) {
98             logger.error("Config Binding Service Address & Port are not present.");
99             return Optional.empty();
100         }
101
102         return Optional.of(String.format(ConfigBindingServiceConstants.CONFIG_SERVICE_QUERY_URL_STRING,
103                 configBindingServiceAddress, configServicePort,
104                 ConfigBindingServiceConstants.SERVICE_NAME_ENV_VARIABLE_VALUE));
105
106     }
107
108     private static Optional<ConsulConfigBindingServiceQueryResponse>
109     parseConsulConfigBindingServiceQueryResponseJson(final String configBindingServiceQueryResponseJson) {
110         // parse json
111         final Optional<List<ConsulConfigBindingServiceQueryResponse>> configBindingServiceQueryResponseOptional =
112                 AnalyticsModelJsonConversion.CONFIG_BINDING_SERVICE_LIST_JSON_FUNCTION
113                         .apply(configBindingServiceQueryResponseJson);
114
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();
121         }
122
123         // return first consul query response
124         return Optional.of(configBindingServiceQueryResponseOptional.get().get(0));
125     }
126
127
128 }