Update the logic for MSB addr retrieving
[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.constant.AlarmConst;\r
26 \r
27 @Slf4j\r
28 public class MicroServiceConfig {\r
29 \r
30     final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";\r
31     final static public String CONSUL_HOST = "CONSUL_HOST";\r
32     final static public String HOSTNAME = "HOSTNAME";\r
33     final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";\r
34     final static public String DOCKER_HOST = "DOCKER_HOST";\r
35     final static public String MSB_ADDR = "MSB_ADDR";\r
36 \r
37     private static String getEnv(String name) {\r
38         String value = System.getenv(name);\r
39         if (value == null) {\r
40             value = System.getProperty(name);\r
41         }\r
42         return value;\r
43     }\r
44 \r
45     public static String getConsulAddrInfo() {\r
46         return getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
47     }\r
48 \r
49     public static String getConfigBindingServiceAddrInfo() {\r
50         String ret = null;\r
51         String queryString = getConsulAddrInfo() + CONFIG_BINDING_SERVICE;\r
52         log.info("Query the CBS address using the URL: " + queryString);\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         log.info("The CBS address is " + ret);\r
62         return ret;\r
63     }\r
64 \r
65     private static String execQuery(String queryString) {\r
66         Client client = ClientBuilder.newClient(new ClientConfig());\r
67         Response response = client.target(queryString).request().get();\r
68         return response.readEntity(String.class);\r
69     }\r
70 \r
71     public static String getServiceAddrInfoFromCBS(String serviceName) {\r
72         String ret = null;\r
73         String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName;\r
74         try {\r
75             JSONObject jsonObject = JSONObject.fromObject(execQuery(url));\r
76             log.info("The origin configurations (" + url + ") returned by DCAE is: " + jsonObject.toString());\r
77             if (jsonObject.has(serviceName)) {\r
78                 ret = (String) jsonObject.getJSONArray(serviceName).get(0);\r
79             }\r
80         } catch (Exception e) {\r
81             log.warn(e.getMessage(), e);\r
82         }\r
83         return ret;\r
84     }\r
85 \r
86     public static String getMsbServerAddr() {\r
87         String[] addrInfo = getMsbAddrInfo();\r
88         String ret = addrInfo[0] + ":" + addrInfo[1];\r
89         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){\r
90             ret = AlarmConst.HTTP + ret;\r
91         }\r
92         return ret;\r
93     }\r
94 \r
95     public static String[] getMsbAddrInfo() {\r
96         String[] msbServerInfo = null;\r
97 \r
98         //String info = getServiceAddrInfoFromCBS(MSB_ADDR);\r
99         String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME));\r
100         log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from CBS. The response is " + info + ".");\r
101         JSONObject infoObj = JSONObject.fromObject(info);\r
102         info = infoObj.has("msb.hostname") ? infoObj.getString("msb.hostname") : null;\r
103         if (info != null){\r
104             msbServerInfo = split(info);\r
105         } else {\r
106             msbServerInfo = split(getEnv(MSB_ADDR));\r
107         }\r
108 \r
109         return msbServerInfo;\r
110     }\r
111 \r
112     public static String[] getServiceAddrInfo() {\r
113         String[] serviceAddrInfo = null;\r
114         String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME));\r
115         log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from CBS. The response is " + info + ".");\r
116         if (info != null){\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 String[] split(String addr) {\r
125         String ip;\r
126         String port = "80";\r
127         if (addr.lastIndexOf(":") == -1){\r
128             ip = addr;\r
129         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
130             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
131         } else {\r
132             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
133             port = addr.substring(addr.lastIndexOf(":") + 1);\r
134         }\r
135         return new String[] {ip, port};\r
136     }\r
137 \r
138 }\r