01a5f2e6818fc67b02f262627e25b2997407d8d2
[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         try {\r
53             JSONObject addrJson = (JSONObject) JSONArray.fromObject(execQuery(queryString)).get(0);\r
54             if (addrJson.has("ServiceAddress") && addrJson.has("ServicePort")) {\r
55                 ret = addrJson.getString("ServiceAddress") + ":" + addrJson.getString("ServicePort");\r
56             }\r
57         } catch (Exception e) {\r
58             log.warn(e.getMessage(), e);\r
59         }\r
60         return ret;\r
61     }\r
62 \r
63     private static String execQuery(String queryString) {\r
64         Client client = ClientBuilder.newClient(new ClientConfig());\r
65         Response response = client.target(queryString).request().get();\r
66         return response.readEntity(String.class);\r
67     }\r
68 \r
69     public static String getServiceAddrInfoFromCBS(String serviceName) {\r
70         String ret = null;\r
71         String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName;\r
72         try {\r
73             JSONObject jsonObject = JSONObject.fromObject(execQuery(url));\r
74             if (jsonObject.has(serviceName)) {\r
75                 ret = (String) jsonObject.getJSONArray(serviceName).get(0);\r
76             }\r
77         } catch (Exception e) {\r
78             log.warn(e.getMessage(), e);\r
79         }\r
80         return ret;\r
81     }\r
82 \r
83     public static String getMsbServerAddr() {\r
84         String[] addrInfo = getMsbAddrInfo();\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[] getMsbAddrInfo() {\r
93         String[] msbServerInfo = null;\r
94 \r
95         String info = getServiceAddrInfoFromCBS(MSB_ADDR);\r
96         if (info != null){\r
97             msbServerInfo = split(info);\r
98         } else {\r
99             msbServerInfo = split(getEnv(MSB_ADDR));\r
100         }\r
101 \r
102         return msbServerInfo;\r
103     }\r
104 \r
105     public static String[] getServiceAddrInfo() {\r
106         String[] serviceAddrInfo = null;\r
107         String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME));\r
108         if (info != null){\r
109             serviceAddrInfo = split(info);\r
110         } else {\r
111             serviceAddrInfo = split(getEnv(HOSTNAME));\r
112         }\r
113         return serviceAddrInfo;\r
114     }\r
115 \r
116     private static String[] split(String addr) {\r
117         String ip;\r
118         String port = "80";\r
119         if (addr.lastIndexOf(":") == -1){\r
120             ip = addr;\r
121         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
122             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
123         } else {\r
124             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
125             port = addr.substring(addr.lastIndexOf(":") + 1);\r
126         }\r
127         return new String[] {ip, port};\r
128     }\r
129 \r
130 }\r