Test Replace Jackson with GSON
[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.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) JSONArray.fromObject(execQuery(queryString)).get(0);\r
54             if (addrJson.has("ServiceAddress") && addrJson.has("ServicePort")) {\r
55                 ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson.getString("ServicePort");\r
56             }\r
57         } catch (Exception e) {\r
58             log.warn(e.getMessage(), e);\r
59         }\r
60         log.info("The " + hostname + " address is " + ret);\r
61         return ret;\r
62     }\r
63 \r
64     private static String execQuery(String queryString) {\r
65         Client client = ClientBuilder.newBuilder().build();\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 getServiceConfigInfoFromCBS(String hostname) {\r
71         String ret = null;\r
72         String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;\r
73         try {\r
74             ret = execQuery(url);\r
75         } catch (Exception e) {\r
76             log.warn(e.getMessage(), e);\r
77         }\r
78         log.info("The query url is: " + url + ". The corresponding configurations are " + ret);\r
79         return ret;\r
80     }\r
81 \r
82     public static String getMsbServerAddrWithHttpPrefix() {\r
83         String[] addrInfo = getMsbIpAndPort();\r
84         String ret = addrInfo[0] + ":" + addrInfo[1];\r
85         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){\r
86             ret = AlarmConst.HTTP + ret;\r
87         }\r
88         return ret;\r
89     }\r
90 \r
91     public static String[] getMsbIpAndPort() {\r
92         return split(getEnv(MSB_ADDR));\r
93     }\r
94 \r
95     public static String[] getMicroServiceIpAndPort() {\r
96         String[] serviceAddrInfo = null;\r
97         String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));\r
98         log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");\r
99         if (info != null && !info.isEmpty()){\r
100             serviceAddrInfo = split(info);\r
101         } else {\r
102             serviceAddrInfo = split(getEnv(HOSTNAME));\r
103         }\r
104         return serviceAddrInfo;\r
105     }\r
106 \r
107     private static String[] split(String addr) {\r
108         String ip;\r
109         String port = "80";\r
110         if (addr.lastIndexOf(":") == -1){\r
111             ip = addr;\r
112         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
113             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
114         } else {\r
115             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
116             port = addr.substring(addr.lastIndexOf(":") + 1);\r
117         }\r
118         return new String[] {ip, port};\r
119     }\r
120 \r
121 }\r