migrate discovery source codes from OPEN-O
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / util / ConfigUtil.java
1 /**
2  * Copyright 2016 ZTE, Inc. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.msb.sdclient.wrapper.util;
17
18 import org.apache.commons.lang3.StringUtils;
19 import org.onap.msb.sdclient.DiscoverAppConfig;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * @ClassName: ConfigUtil
25  * @Description: TODO(服务发现配置项工具类)
26  * @author tanghua10186366
27  * @date 2017年1月23日
28  * 
29  */
30 public class ConfigUtil {
31
32   private static ConfigUtil instance = new ConfigUtil();
33
34
35   private ConfigUtil() {}
36
37   public static ConfigUtil getInstance() {
38     return instance;
39   }
40
41
42   private String tcpudpPortRangeStart= DiscoverUtil.TCP_UDP_PORT_RANGE_START;
43
44   private String tcpudpPortRangeEnd=DiscoverUtil.TCP_UDP_PORT_RANGE_END;
45
46   private String consulAddress = DiscoverUtil.CONSUL_ADDRESSS;
47
48   private String consulRegisterMode=DiscoverUtil.CONSUL_REGISTER_MODE;
49
50   private static final Logger LOGGER = LoggerFactory.getLogger(ConfigUtil.class);
51
52   public void initConsulClientInfo(DiscoverAppConfig config) {
53
54
55     String env_CONSUL_IP = System.getenv("CONSUL_IP");
56
57
58     if (StringUtils.isNotBlank(env_CONSUL_IP)) {
59       String consul_port = DiscoverUtil.CONSUL_DEFAULT_PORT;
60       try {
61         consul_port = config.getConsulAdderss().split(":")[1];
62       } catch (Exception e) {
63         LOGGER.error("initConsulClientInfo throw err:" + e.getMessage());
64       }
65
66       consulAddress = env_CONSUL_IP + ":" + consul_port;
67     } else if (StringUtils.isNotBlank(config.getConsulAdderss())) {
68       {
69         consulAddress = config.getConsulAdderss();
70       }
71
72
73       LOGGER.info("init Discover CONSUL ADDRESSS:" + consulAddress);
74
75
76     }
77   }
78   
79   public void initTCP_UDP_portRange(){
80     
81     String env_TCP_UDP_PORT_RANGE_START=System.getenv("TCP_UDP_PORT_RANGE_START");
82     String env_TCP_UDP_PORT_RANGE_END=System.getenv("TCP_UDP_PORT_RANGE_END");
83     
84     
85     if(StringUtils.isNotBlank(env_TCP_UDP_PORT_RANGE_START))
86     {
87       tcpudpPortRangeStart=env_TCP_UDP_PORT_RANGE_START;
88     }
89    
90     
91     if(StringUtils.isNotBlank(env_TCP_UDP_PORT_RANGE_END))
92     {
93       tcpudpPortRangeEnd=env_TCP_UDP_PORT_RANGE_END;
94     }
95    
96     LOGGER.info("init TCP_UDP portRange:"+ tcpudpPortRangeStart+"-"+tcpudpPortRangeEnd);
97     
98   }
99   
100   public void initConsulRegisterMode(DiscoverAppConfig config){
101     
102     String env_CONSUL_REGISTER_MODE=System.getenv("CONSUL_REGISTER_MODE");
103     
104     if(StringUtils.isNotBlank(env_CONSUL_REGISTER_MODE))
105     {
106       consulRegisterMode=env_CONSUL_REGISTER_MODE;
107     }
108     else{
109       if(StringUtils.isNotBlank(config.getConsulRegisterMode())){
110         consulRegisterMode=config.getConsulRegisterMode();
111       }
112     }
113     
114     LOGGER.info("init Consul Register Mode:"+consulRegisterMode);
115     
116   }
117   
118
119
120   public String getConsulAddress() {
121     return consulAddress;
122   }
123   
124   public String getTcpudpPortRangeStart() {
125     return tcpudpPortRangeStart;
126   }
127
128
129   public String getTcpudpPortRangeEnd() {
130     return tcpudpPortRangeEnd;
131   }
132
133
134   public String getConsulRegisterMode() {
135     return consulRegisterMode;
136   }
137
138   
139   
140 }