3a4abcf29d7d8542ead701ad110874e91404ef0e
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.tasks;
22
23 import java.util.Optional;
24 import java.util.Properties;
25
26 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
28 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import reactor.core.publisher.Mono;
32
33 /**
34  * This class reads a few environment variables used for locating the Consul
35  * (Config Binding Service).
36  */
37 class EnvironmentProcessor {
38
39     private static final int DEFAULT_CONSUL_PORT = 8500;
40     private static final Logger logger = LoggerFactory.getLogger(EnvironmentProcessor.class);
41
42     private EnvironmentProcessor() {}
43
44     static Mono<EnvProperties> readEnvironmentVariables(Properties systemEnvironment) {
45
46         EnvProperties envProperties;
47         try {
48             envProperties = ImmutableEnvProperties.builder() //
49                     .consulHost(getConsulHost(systemEnvironment)) //
50                     .consulPort(getConsultPort(systemEnvironment)) //
51                     .cbsName(getConfigBindingService(systemEnvironment)) //
52                     .appName(getService(systemEnvironment)) //
53                     .build();
54         } catch (ServiceException e) {
55             return Mono.error(e);
56         }
57         logger.trace("Evaluated environment system variables {}", envProperties);
58         return Mono.just(envProperties);
59     }
60
61     private static String getConsulHost(Properties systemEnvironments) throws ServiceException {
62         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_HOST"))
63                 .orElseThrow(() -> new ServiceException("$CONSUL_HOST environment has not been defined"));
64     }
65
66     private static Integer getConsultPort(Properties systemEnvironments) {
67         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_PORT")) //
68                 .map(Integer::valueOf) //
69                 .orElseGet(EnvironmentProcessor::getDefaultPortOfConsul);
70     }
71
72     private static String getConfigBindingService(Properties systemEnvironments) throws ServiceException {
73         return Optional.ofNullable(systemEnvironments.getProperty("CONFIG_BINDING_SERVICE")) //
74                 .orElseThrow(() -> new ServiceException("$CONFIG_BINDING_SERVICE environment has not been defined"));
75     }
76
77     private static String getService(Properties systemEnvironments) throws ServiceException {
78         return Optional
79                 .ofNullable(Optional.ofNullable(systemEnvironments.getProperty("HOSTNAME"))
80                         .orElse(systemEnvironments.getProperty("SERVICE_NAME")))
81                 .orElseThrow(() -> new ServiceException(
82                         "Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment"));
83     }
84
85     private static Integer getDefaultPortOfConsul() {
86         logger.warn("$CONSUL_PORT variable will be set to default port {}", DEFAULT_CONSUL_PORT);
87         return DEFAULT_CONSUL_PORT;
88     }
89 }