a6a328ec3227346920ef9105fff8b915e7a8bc0e
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / configuration / Environment.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
23 import java.util.Optional;
24 import java.util.Properties;
25
26 import org.oransc.policyagent.exceptions.ServiceException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import reactor.core.publisher.Mono;
30
31 class Environment {
32
33     public static class Variables {
34
35         public final String consulHost;
36         public final Integer consulPort;
37         public final String cbsName;
38         public final String appName;
39
40         public Variables(String consulHost, Integer consulPort, String cbsName, String appName) {
41             this.consulHost = consulHost;
42             this.consulPort = consulPort;
43             this.cbsName = cbsName;
44             this.appName = appName;
45         }
46     }
47
48     private static final int DEFAULT_CONSUL_PORT = 8500;
49     private static final Logger logger = LoggerFactory.getLogger(Environment.class);
50
51     private Environment() {
52     }
53
54     static Mono<Variables> readEnvironmentVariables(Properties systemEnvironment) {
55         logger.trace("Loading system environment variables");
56
57         try {
58             Variables envProperties = new Variables(getConsulHost(systemEnvironment) //
59                 , getConsultPort(systemEnvironment) //
60                 , getConfigBindingService(systemEnvironment) //
61                 , getService(systemEnvironment)); //
62
63             logger.trace("Evaluated environment system variables {}", envProperties);
64             return Mono.just(envProperties);
65         } catch (ServiceException e) {
66             return Mono.error(e);
67         }
68     }
69
70     private static String getConsulHost(Properties systemEnvironments) throws ServiceException {
71         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_HOST"))
72             .orElseThrow(() -> new ServiceException("$CONSUL_HOST environment has not been defined"));
73     }
74
75     private static Integer getConsultPort(Properties systemEnvironments) {
76         return Optional.ofNullable(systemEnvironments.getProperty("CONSUL_PORT")) //
77             .map(Integer::valueOf) //
78             .orElseGet(Environment::getDefaultPortOfConsul);
79     }
80
81     private static String getConfigBindingService(Properties systemEnvironments) throws ServiceException {
82         return Optional.ofNullable(systemEnvironments.getProperty("CONFIG_BINDING_SERVICE")) //
83             .orElseThrow(() -> new ServiceException("$CONFIG_BINDING_SERVICE environment has not been defined"));
84     }
85
86     private static String getService(Properties systemEnvironments) throws ServiceException {
87         return Optional
88             .ofNullable(Optional.ofNullable(systemEnvironments.getProperty("HOSTNAME"))
89                 .orElse(systemEnvironments.getProperty("SERVICE_NAME")))
90             .orElseThrow(() -> new ServiceException(
91                 "Neither $HOSTNAME/$SERVICE_NAME have not been defined as system environment"));
92     }
93
94     private static Integer getDefaultPortOfConsul() {
95         logger.warn("$CONSUL_PORT variable will be set to default port {}", DEFAULT_CONSUL_PORT);
96         return DEFAULT_CONSUL_PORT;
97     }
98 }