Geted Holmes IP from Env Var
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / config / MicroServiceConfig.java
1 /**\r
2  * Copyright  2017 ZTE Corporation.\r
3  * <p>\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  * <p>\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  * <p>\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.onap.holmes.common.config;\r
17 \r
18 import com.alibaba.fastjson.JSON;\r
19 import com.alibaba.fastjson.JSONObject;\r
20 \r
21 import javax.ws.rs.client.Client;\r
22 import javax.ws.rs.client.ClientBuilder;\r
23 import javax.ws.rs.core.Response;\r
24 \r
25 import lombok.extern.slf4j.Slf4j;\r
26 import org.onap.holmes.common.constant.AlarmConst;\r
27 \r
28 import java.util.regex.Pattern;\r
29 \r
30 @Slf4j\r
31 public class MicroServiceConfig {\r
32 \r
33     final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";\r
34     final static public String CONSUL_HOST = "CONSUL_HOST";\r
35     final static public String HOSTNAME = "HOSTNAME";\r
36     final static public String POD_IP = "POD_IP";\r
37     final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";\r
38     final static public String DOCKER_HOST = "DOCKER_HOST";\r
39     final static public String MSB_ADDR = "MSB_ADDR";\r
40     final static public Pattern IP_REG = Pattern.compile("(http(s)?://)?(\\d+\\.\\d+\\.\\d+\\.\\d+)(:(\\d+))?");\r
41 \r
42     public static String getEnv(String name) {\r
43         String value = System.getenv(name);\r
44         if (value == null) {\r
45             value = System.getProperty(name);\r
46         }\r
47         return value;\r
48     }\r
49 \r
50     public static String getConsulAddrInfo() {\r
51         return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
52     }\r
53 \r
54     public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {\r
55         String ret = null;\r
56         String queryString = getConsulAddrInfo() + hostname;\r
57         log.info("Query the " + hostname + " address using the URL: " + queryString);\r
58         try {\r
59             JSONObject addrJson = (JSONObject) JSON.parseArray(execQuery(queryString)).get(0);\r
60             if (addrJson != null && addrJson.get("ServiceAddress") != null\r
61                     && addrJson.get("ServicePort") != null) {\r
62                 ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson\r
63                         .getString("ServicePort");\r
64             }\r
65         } catch (Exception e) {\r
66             log.warn(e.getMessage(), e);\r
67         }\r
68         log.info("The " + hostname + " address is " + ret);\r
69         return ret;\r
70     }\r
71 \r
72     private static String execQuery(String queryString) {\r
73         Client client = ClientBuilder.newBuilder().build();\r
74         Response response = client.target(queryString).request().get();\r
75         return response.readEntity(String.class);\r
76     }\r
77 \r
78     public static String getServiceConfigInfoFromCBS(String hostname) {\r
79         String ret = null;\r
80         String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;\r
81         try {\r
82             ret = execQuery(url);\r
83         } catch (Exception e) {\r
84             log.warn(e.getMessage(), e);\r
85         }\r
86         log.info("The query url is: " + url + ". The corresponding configurations are " + ret);\r
87         return ret;\r
88     }\r
89 \r
90     public static String getMsbServerAddrWithHttpPrefix() {\r
91         String[] addrInfo = getMsbIpAndPort();\r
92         String ret = addrInfo[0] + ":" + addrInfo[1];\r
93         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {\r
94             ret = AlarmConst.HTTP + ret;\r
95         }\r
96         return ret;\r
97     }\r
98 \r
99     public static String[] getMsbIpAndPort() {\r
100         return split(getEnv(MSB_ADDR));\r
101     }\r
102 \r
103     public static String[] getMicroServiceIpAndPort() {\r
104         String[] serviceAddrInfo = null;\r
105         String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));\r
106         log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");\r
107 \r
108         if (info != null && !info.isEmpty()) {\r
109             if (!isIpAddress(info)) {\r
110                 info = getEnv(POD_IP);\r
111             }\r
112             serviceAddrInfo = split(info);\r
113         } else {\r
114             serviceAddrInfo = split(getEnv(HOSTNAME));\r
115         }\r
116         return serviceAddrInfo;\r
117     }\r
118 \r
119     private static boolean isIpAddress(String info) {\r
120         return IP_REG.matcher(info).matches();\r
121     }\r
122 \r
123     private static String[] split(String addr) {\r
124         String ip;\r
125         String port = "80";\r
126         if (addr.lastIndexOf(":") == -1) {\r
127             ip = addr;\r
128         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
129             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
130         } else {\r
131             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
132             port = addr.substring(addr.lastIndexOf(":") + 1);\r
133         }\r
134         return new String[]{ip, port};\r
135     }\r
136 \r
137 }\r