Get the MSB info from DCAE Consul
[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 javax.ws.rs.client.Client;\r
19 import javax.ws.rs.client.ClientBuilder;\r
20 import javax.ws.rs.core.Response;\r
21 import lombok.extern.slf4j.Slf4j;\r
22 import net.sf.json.JSONArray;\r
23 import net.sf.json.JSONObject;\r
24 import org.glassfish.jersey.client.ClientConfig;\r
25 import org.onap.holmes.common.api.stat.Alarm;\r
26 import org.onap.holmes.common.constant.AlarmConst;\r
27 \r
28 @Slf4j\r
29 public class MicroServiceConfig {\r
30 \r
31     final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";\r
32     final static public String CONSUL_HOST = "CONSUL_HOST";\r
33     final static public String HOSTNAME = "HOSTNAME";\r
34     final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";\r
35     final static public String DOCKER_HOST = "DOCKER_HOST";\r
36     final static public String MSB_ADDR = "MSB_ADDR";\r
37 \r
38     private static String getEnv(String name) {\r
39         String value = System.getenv(name);\r
40         if (value == null) {\r
41             value = System.getProperty(name);\r
42         }\r
43         return value;\r
44     }\r
45 \r
46     public static String getConsulAddrInfo() {\r
47         return getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
48     }\r
49 \r
50     public static String getConfigBindingServiceAddrInfo() {\r
51         String ret = null;\r
52         String queryString = getConsulAddrInfo() + CONFIG_BINDING_SERVICE;\r
53         try {\r
54             JSONObject addrJson = (JSONObject) JSONArray.fromObject(execQuery(queryString)).get(0);\r
55             if (addrJson.has("ServiceAddress") && addrJson.has("ServicePort")) {\r
56                 ret = addrJson.getString("ServiceAddress") + ":" + addrJson.getString("ServicePort");\r
57             }\r
58         } catch (Exception e) {\r
59             log.warn(e.getMessage(), e);\r
60         }\r
61         return ret;\r
62     }\r
63 \r
64     private static String execQuery(String queryString) {\r
65         Client client = ClientBuilder.newClient(new ClientConfig());\r
66         Response response = client.target(queryString).request().get();\r
67         return response.readEntity(String.class);\r
68     }\r
69 \r
70     public static String getServiceAddrInfoFromCBS(String serviceName) {\r
71         String ret = null;\r
72         String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName;\r
73         try {\r
74             JSONObject jsonObject = JSONObject.fromObject(execQuery(url));\r
75             if (jsonObject.has(serviceName)) {\r
76                 ret = (String) jsonObject.getJSONArray(serviceName).get(0);\r
77             }\r
78         } catch (Exception e) {\r
79             log.warn(e.getMessage(), e);\r
80         }\r
81         return ret;\r
82     }\r
83 \r
84     public static String getMsbServerAddr() {\r
85         String[] addrInfo = getMsbAddrInfo();\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[] getMsbAddrInfo() {\r
94         String[] msbServerInfo = null;\r
95 \r
96         String info = getServiceAddrInfoFromCBS(MSB_ADDR);\r
97         if (info != null){\r
98             msbServerInfo = split(info);\r
99         } else {\r
100             msbServerInfo = split(getEnv(MSB_ADDR));\r
101         }\r
102 \r
103         return msbServerInfo;\r
104     }\r
105 \r
106     public static String[] getServiceAddrInfo() {\r
107         String[] serviceAddrInfo = null;\r
108         String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME));\r
109         if (info != null){\r
110             serviceAddrInfo = split(info);\r
111         } else {\r
112             serviceAddrInfo = split(getEnv(HOSTNAME));\r
113         }\r
114         return serviceAddrInfo;\r
115     }\r
116 \r
117     private static String[] split(String addr) {\r
118         String ip;\r
119         String port = "80";\r
120         if (addr.lastIndexOf(":") == -1){\r
121             ip = addr;\r
122         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
123             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
124         } else {\r
125             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
126             port = addr.substring(addr.lastIndexOf(":") + 1);\r
127         }\r
128         return new String[] {ip, port};\r
129     }\r
130 \r
131 }\r