Fix the DCAE integration Bugs
[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     public 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 "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
47     }\r
48 \r
49     public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {\r
50         String ret = null;\r
51         String queryString = getConsulAddrInfo() + hostname;\r
52         log.info("Query the " + hostname + " 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 = "http://" + 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 " + hostname + " 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 getServiceConfigInfoFromCBS(String hostname) {\r
72         String ret = null;\r
73         String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;\r
74         try {\r
75             ret = execQuery(url);\r
76         } catch (Exception e) {\r
77             log.warn(e.getMessage(), e);\r
78         }\r
79         log.info("The query url is: " + url + ". The corresponding configurations are " + ret);\r
80         return ret;\r
81     }\r
82 \r
83     public static String getMsbServerAddrWithHttpPrefix() {\r
84         String[] addrInfo = getMsbIpAndPort();\r
85         String ret = addrInfo[0] + ":" + addrInfo[1];\r
86         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){\r
87             ret = AlarmConst.HTTP + ret;\r
88         }\r
89         return ret;\r
90     }\r
91 \r
92     public static String[] getMsbIpAndPort() {\r
93         return split(getEnv(MSB_ADDR));\r
94     }\r
95 \r
96     public static String[] getMicroServiceIpAndPort() {\r
97         String[] serviceAddrInfo = null;\r
98         String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));\r
99         log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");\r
100         if (info != null && !info.isEmpty()){\r
101             serviceAddrInfo = split(info);\r
102         } else {\r
103             serviceAddrInfo = split(getEnv(HOSTNAME));\r
104         }\r
105         return serviceAddrInfo;\r
106     }\r
107 \r
108     private static String[] split(String addr) {\r
109         String ip;\r
110         String port = "80";\r
111         if (addr.lastIndexOf(":") == -1){\r
112             ip = addr;\r
113         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
114             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
115         } else {\r
116             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
117             port = addr.substring(addr.lastIndexOf(":") + 1);\r
118         }\r
119         return new String[] {ip, port};\r
120     }\r
121 \r
122 }\r