Add some logs to AAI queries
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / config / MicroServiceConfig.java
1 /**\r
2  * Copyright  2017 ZTE Corporation.\r
3  * <p>\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  * <p>\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  * <p>\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 \r
21 import javax.ws.rs.client.Client;\r
22 import javax.ws.rs.client.ClientBuilder;\r
23 import javax.ws.rs.core.Response;\r
24 \r
25 import lombok.extern.slf4j.Slf4j;\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     public 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 "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
48     }\r
49 \r
50     public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {\r
51         String ret = null;\r
52         String queryString = getConsulAddrInfo() + hostname;\r
53         log.info("Query the " + hostname + " address using the URL: " + queryString);\r
54         try {\r
55             JSONObject addrJson = (JSONObject) JSON.parseArray(execQuery(queryString)).get(0);\r
56             if (addrJson != null && addrJson.get("ServiceAddress") != null\r
57                     && addrJson.get("ServicePort") != null) {\r
58                 ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson\r
59                         .getString("ServicePort");\r
60             }\r
61         } catch (Exception e) {\r
62             log.warn(e.getMessage(), e);\r
63         }\r
64         log.info("The " + hostname + " address is " + ret);\r
65         return ret;\r
66     }\r
67 \r
68     private static String execQuery(String queryString) {\r
69         Client client = ClientBuilder.newBuilder().build();\r
70         Response response = client.target(queryString).request().get();\r
71         return response.readEntity(String.class);\r
72     }\r
73 \r
74     public static String getServiceConfigInfoFromCBS(String hostname) {\r
75         String ret = null;\r
76         String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;\r
77         try {\r
78             ret = execQuery(url);\r
79         } catch (Exception e) {\r
80             log.warn(e.getMessage(), e);\r
81         }\r
82         log.info("The query url is: " + url + ". The corresponding configurations are " + ret);\r
83         return ret;\r
84     }\r
85 \r
86     public static String getMsbServerAddrWithHttpPrefix() {\r
87         String[] addrInfo = getMsbIpAndPort();\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[] getMsbIpAndPort() {\r
96         return split(getEnv(MSB_ADDR));\r
97     }\r
98 \r
99     public static String[] getMicroServiceIpAndPort() {\r
100         String[] serviceAddrInfo = null;\r
101         String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));\r
102         log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");\r
103         if (info != null && !info.isEmpty()) {\r
104             serviceAddrInfo = split(info);\r
105         } else {\r
106             serviceAddrInfo = split(getEnv(HOSTNAME));\r
107         }\r
108         return serviceAddrInfo;\r
109     }\r
110 \r
111     private static String[] split(String addr) {\r
112         String ip;\r
113         String port = "80";\r
114         if (addr.lastIndexOf(":") == -1) {\r
115             ip = addr;\r
116         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
117             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
118         } else {\r
119             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
120             port = addr.substring(addr.lastIndexOf(":") + 1);\r
121         }\r
122         return new String[]{ip, port};\r
123     }\r
124 \r
125 }\r