f74d1b2978198faba803b91510b1a30f9104b3b2
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / config / MicroServiceConfig.java
1 /**\r
2  * Copyright  2017 ZTE Corporation.\r
3  *\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  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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 import javax.ws.rs.client.Client;\r
21 import javax.ws.rs.client.ClientBuilder;\r
22 import javax.ws.rs.core.Response;\r
23 import lombok.extern.slf4j.Slf4j;\r
24 import org.onap.holmes.common.constant.AlarmConst;\r
25 \r
26 @Slf4j\r
27 public class MicroServiceConfig {\r
28 \r
29     final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";\r
30     final static public String CONSUL_HOST = "CONSUL_HOST";\r
31     final static public String HOSTNAME = "HOSTNAME";\r
32     final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";\r
33     final static public String DOCKER_HOST = "DOCKER_HOST";\r
34     final static public String MSB_ADDR = "MSB_ADDR";\r
35 \r
36     public static String getEnv(String name) {\r
37         String value = System.getenv(name);\r
38         if (value == null) {\r
39             value = System.getProperty(name);\r
40         }\r
41         return value;\r
42     }\r
43 \r
44     public static String getConsulAddrInfo() {\r
45         return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
46     }\r
47 \r
48     public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {\r
49         String ret = null;\r
50         String queryString = getConsulAddrInfo() + hostname;\r
51         log.info("Query the " + hostname + " address using the URL: " + queryString);\r
52         try {\r
53             JSONObject addrJson = (JSONObject) JSON.parseArray(execQuery(queryString)).get(0);\r
54             if (addrJson != null && addrJson.get("ServiceAddress") != null\r
55                     && addrJson.get("ServicePort") != null) {\r
56                 ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson\r
57                         .getString("ServicePort");\r
58             }\r
59         } catch (Exception e) {\r
60             log.warn(e.getMessage(), e);\r
61         }\r
62         log.info("The " + hostname + " address is " + ret);\r
63         return ret;\r
64     }\r
65 \r
66     private static String execQuery(String queryString) {\r
67         Client client = ClientBuilder.newBuilder().build();\r
68         Response response = client.target(queryString).request().get();\r
69         return response.readEntity(String.class);\r
70     }\r
71 \r
72     public static String getServiceConfigInfoFromCBS(String hostname) {\r
73         String ret = null;\r
74         String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;\r
75         try {\r
76             ret = execQuery(url);\r
77         } catch (Exception e) {\r
78             log.warn(e.getMessage(), e);\r
79         }\r
80         log.info("The query url is: " + url + ". The corresponding configurations are " + ret);\r
81         return ret;\r
82     }\r
83 \r
84     public static String getMsbServerAddrWithHttpPrefix() {\r
85         String[] addrInfo = getMsbIpAndPort();\r
86         String ret = addrInfo[0] + ":" + addrInfo[1];\r
87         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){\r
88             ret = AlarmConst.HTTP + ret;\r
89         }\r
90         return ret;\r
91     }\r
92 \r
93     public static String[] getMsbIpAndPort() {\r
94         return split(getEnv(MSB_ADDR));\r
95     }\r
96 \r
97     public static String[] getMicroServiceIpAndPort() {\r
98         String[] serviceAddrInfo = null;\r
99         String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));\r
100         log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");\r
101         if (info != null && !info.isEmpty()){\r
102             serviceAddrInfo = split(info);\r
103         } else {\r
104             serviceAddrInfo = split(getEnv(HOSTNAME));\r
105         }\r
106         return serviceAddrInfo;\r
107     }\r
108 \r
109     private static String[] split(String addr) {\r
110         String ip;\r
111         String port = "80";\r
112         if (addr.lastIndexOf(":") == -1){\r
113             ip = addr;\r
114         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
115             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
116         } else {\r
117             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
118             port = addr.substring(addr.lastIndexOf(":") + 1);\r
119         }\r
120         return new String[] {ip, port};\r
121     }\r
122 \r
123 }\r