aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/controller/EnvPropertiesReader.java
blob: 319caa651c506e4b27d65aee8a57a7639fd3ac2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dcaegen2.collectors.ves
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright (C) 2018 Nokia. 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.dcae.controller;

import io.vavr.collection.Map;
import io.vavr.control.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.vavr.API.List;
import static io.vavr.API.Try;
import static org.onap.dcae.common.publishing.VavrUtils.f;

final class EnvPropertiesReader {

    private final static Logger log = LoggerFactory.getLogger(EnvPropertiesReader.class);

    static Option<EnvProps> readEnvProps(Map<String, String> environmentVariables) {
        log.info("Loading necessary environment variables for dynamic configuration update");
        int consulPort = getConsulPort(environmentVariables);
        String consulProtocol = getConsulProtocol(environmentVariables);
        String cbsProtocol = getCbsProtocol(environmentVariables);
        Option<String> consulHost = getConsulHost(environmentVariables);
        Option<String> cbsServiceName = getCBSName(environmentVariables);
        Option<String> vesCollectorAppName = getAppName(environmentVariables);
        return Option.sequence(List(consulHost, cbsServiceName, vesCollectorAppName))
            .map(e -> new EnvProps(consulProtocol, e.get(0), consulPort, cbsProtocol, e.get(1), e.get(2)))
            .onEmpty(() -> log.warn("Some required environment variables are missing"))
            .peek(props -> log.info(f("Discovered following environment variables: '%s'", props)));
    }

    private static Option<String> getAppName(Map<String, String> environmentVariables) {
        return environmentVariables.get("HOSTNAME")
            .orElse(environmentVariables.get("SERVICE_NAME"))
            .onEmpty(() -> log.warn("App service name (as registered in CBS) (env var: 'HOSTNAME' / 'SERVICE_NAME') "
                + "is missing error environment variables."));
    }

    private static Option<String> getCBSName(Map<String, String> environmentVariables) {
        return environmentVariables.get("CONFIG_BINDING_SERVICE")
            .onEmpty(() -> log.warn("Name of CBS Service (as registered in Consul) (env var: 'CONFIG_BINDING_SERVICE') "
                + "is missing from environment variables."));
    }

    private static Integer getConsulPort(Map<String, String> environmentVariables) {
        return environmentVariables.get("CONSUL_PORT")
            .flatMap(str -> Try(() -> Integer.valueOf(str))
                .onFailure(exc -> log.warn("Consul port is not an integer value", exc))
                .toOption())
            .onEmpty(() -> log.warn("Consul port (env var: 'CONSUL_PORT') is missing from environment variables. "
                + "Using default value of 8500"))
            .getOrElse(8500);
    }

    private static Option<String> getConsulHost(Map<String, String> environmentVariables) {
        return environmentVariables.get("CONSUL_HOST")
            .onEmpty(() -> log.warn("Consul host (env var: 'CONSUL_HOST') (without port) "
                + "is missing from environment variables."));
    }

    private static String getConsulProtocol(Map<String, String> environmentVariables) {
        return getProtocolFrom("CONSUL_PROTOCOL", environmentVariables);
    }

    private static String getCbsProtocol(Map<String, String> environmentVariables) {
        return getProtocolFrom("CBS_PROTOCOL", environmentVariables);
    }

    private static String getProtocolFrom(String variableName, Map<String, String> environmentVariables) {
        return environmentVariables.get(variableName)
            .onEmpty(() -> log.warn("Consul protocol (env var: '" + variableName + "') is missing "
                + "from environment variables."))
            .getOrElse("http");
    }

}