f0cad0ee8fc66762f04639d1e2705950f1b9aeb7
[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     final static public String AAI_HOSTNAME = "aai.onap";\r
42 \r
43     public static String getEnv(String name) {\r
44         String value = System.getenv(name);\r
45         if (value == null) {\r
46             value = System.getProperty(name);\r
47         }\r
48         return value;\r
49     }\r
50 \r
51     public static String getConsulAddrInfo() {\r
52         return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
53     }\r
54 \r
55     public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {\r
56         String ret = null;\r
57         String queryString = getConsulAddrInfo() + hostname;\r
58         log.info("Query the " + hostname + " address using the URL: " + queryString);\r
59         try {\r
60             JSONObject addrJson = (JSONObject) JSON.parseArray(execQuery(queryString)).get(0);\r
61             if (addrJson != null && addrJson.get("ServiceAddress") != null\r
62                     && addrJson.get("ServicePort") != null) {\r
63                 ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson\r
64                         .getString("ServicePort");\r
65             }\r
66         } catch (Exception e) {\r
67             log.warn(e.getMessage(), e);\r
68         }\r
69         log.info("The " + hostname + " address is " + ret);\r
70         return ret;\r
71     }\r
72 \r
73     private static String execQuery(String queryString) {\r
74         Client client = ClientBuilder.newBuilder().build();\r
75         Response response = client.target(queryString).request().get();\r
76         return response.readEntity(String.class);\r
77     }\r
78 \r
79     public static String getServiceConfigInfoFromCBS(String hostname) {\r
80         String ret = null;\r
81         String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;\r
82         try {\r
83             ret = execQuery(url);\r
84         } catch (Exception e) {\r
85             log.warn(e.getMessage(), e);\r
86         }\r
87         log.info("The query url is: " + url + ". The corresponding configurations are " + ret);\r
88         return ret;\r
89     }\r
90 \r
91     public static String getMsbServerAddrWithHttpPrefix() {\r
92         String[] addrInfo = getMsbIpAndPort();\r
93         String ret = addrInfo[0] + ":" + addrInfo[1];\r
94         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {\r
95             ret = AlarmConst.HTTP + ret;\r
96         }\r
97         return ret;\r
98     }\r
99 \r
100     public static String getAaiAddr() {\r
101         return AlarmConst.HTTPS + AAI_HOSTNAME;\r
102     }\r
103 \r
104     public static String[] getMsbIpAndPort() {\r
105         return split(getEnv(MSB_ADDR));\r
106     }\r
107 \r
108     public static String[] getMicroServiceIpAndPort() {\r
109         String[] serviceAddrInfo = null;\r
110         String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));\r
111         log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");\r
112 \r
113         if (info != null && !info.isEmpty()) {\r
114             if (!isIpAddress(info)) {\r
115                 info = getEnv(POD_IP);\r
116             }\r
117             serviceAddrInfo = split(info);\r
118         } else {\r
119             serviceAddrInfo = split(getEnv(HOSTNAME));\r
120         }\r
121         return serviceAddrInfo;\r
122     }\r
123 \r
124     private static boolean isIpAddress(String info) {\r
125         return IP_REG.matcher(info).matches();\r
126     }\r
127 \r
128     private static String[] split(String addr) {\r
129         String ip;\r
130         String port = "80";\r
131         if (addr.lastIndexOf(":") == -1) {\r
132             ip = addr;\r
133         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
134             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
135         } else {\r
136             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
137             port = addr.substring(addr.lastIndexOf(":") + 1);\r
138         }\r
139         return new String[]{ip, port};\r
140     }\r
141 \r
142 }\r