Fix startup
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / configuration / EnvironmentProcessor.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.configuration;
22 import java.util.Optional;
23 import java.util.Properties;
24 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
25 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties;
26 import org.oransc.policyagent.exceptions.EnvironmentLoaderException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import reactor.core.publisher.Mono;
30
31 class EnvironmentProcessor {
32
33     private static final int DEFAULT_CONSUL_PORT = 8500;
34     private static final Logger logger = LoggerFactory.getLogger(EnvironmentProcessor.class);
35
36     private EnvironmentProcessor() {
37     }
38
39     static Mono<EnvProperties> readEnvironmentVariables(Properties systemEnvironment) {
40
41         EnvProperties envProperties;
42         try {
43             envProperties = ImmutableEnvProperties.builder() //
44                     .consulHost(getConsulHost(systemEnvironment)) //
45                     .consulPort(getConsultPort(systemEnvironment)) //
46                     .cbsName(getConfigBindingService(systemEnvironment)) //
47                     .appName(getService(systemEnvironment)) //
48                     .build();
49         } catch (EnvironmentLoaderException e) {
50             return Mono.error(e);
51         }
52         logger.trace("Evaluated environment system variables {}", envProperties);
53         return Mono.just(envProperties);
54     }
55
56     private static String getConsulHost(Properties systemEnvironments) throws EnvironmentLoaderException {
57         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_HOST"))
58                 .orElseThrow(() -> new EnvironmentLoaderException("$CONSUL_HOST environment has not been defined"));
59     }
60
61     private static Integer getConsultPort(Properties systemEnvironments) {
62         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_PORT")) //
63                 .map(Integer::valueOf) //
64                 .orElseGet(EnvironmentProcessor::getDefaultPortOfConsul);
65     }
66
67     private static String getConfigBindingService(Properties systemEnvironments) throws EnvironmentLoaderException {
68         return Optional.ofNullable(systemEnvironments.getProperty("CONFIG_BINDING_SERVICE")) //
69                 .orElseThrow(
70                         () -> new EnvironmentLoaderException("$CONFIG_BINDING_SERVICE environment has not been defined"));
71     }
72
73     private static String getService(Properties systemEnvironments) throws EnvironmentLoaderException {
74         return Optional
75                 .ofNullable(Optional.ofNullable(systemEnvironments.getProperty("HOSTNAME"))
76                         .orElse(systemEnvironments.getProperty("SERVICE_NAME")))
77                 .orElseThrow(() -> new EnvironmentLoaderException(
78                         "Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment"));
79     }
80
81     private static Integer getDefaultPortOfConsul() {
82         logger.warn("$CONSUL_PORT variable will be set to default port {}", DEFAULT_CONSUL_PORT);
83         return DEFAULT_CONSUL_PORT;
84     }
85 }