Fixed the CLM Issues
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / config / MicroServiceConfig.java
1 /**\r
2  * Copyright  2017-2020 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.google.gson.JsonObject;\r
19 import com.google.gson.JsonParser;\r
20 import org.onap.holmes.common.constant.AlarmConst;\r
21 import org.slf4j.Logger;\r
22 import org.slf4j.LoggerFactory;\r
23 \r
24 import javax.ws.rs.client.Client;\r
25 import javax.ws.rs.client.ClientBuilder;\r
26 import javax.ws.rs.core.Response;\r
27 import java.util.regex.Pattern;\r
28 \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 POD_IP = "POD_IP";\r
35     final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";\r
36     final static public String DOCKER_HOST = "DOCKER_HOST";\r
37     final static public String MSB_ADDR = "MSB_ADDR";\r
38     final static public Pattern IP_REG = Pattern.compile("(http(s)?://)?(\\d+\\.\\d+\\.\\d+\\.\\d+)(:(\\d+))?");\r
39     final static public String AAI_HOSTNAME = "aai.onap";\r
40 \r
41     final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class);\r
42 \r
43     public static String getEnv(String name) {\r
44         String value = System.getenv(name);\r
45         if (value == null) {\r
46             value = System.getProperty(name);\r
47         }\r
48         return value;\r
49     }\r
50 \r
51     public static String getConsulAddrInfo() {\r
52         return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
53     }\r
54 \r
55     public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {\r
56         String ret = null;\r
57         String queryString = getConsulAddrInfo() + hostname;\r
58         log.info("Query the " + hostname + " address using the URL: " + queryString);\r
59         try {\r
60             JsonObject addrJson = JsonParser.parseString(execQuery(queryString))\r
61                     .getAsJsonArray()\r
62                     .get(0)\r
63                     .getAsJsonObject();\r
64             if (addrJson != null && addrJson.get("ServiceAddress") != null\r
65                     && addrJson.get("ServicePort") != null) {\r
66                 ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson\r
67                         .get("ServicePort").getAsString();\r
68             }\r
69         } catch (Exception e) {\r
70             log.warn(e.getMessage(), e);\r
71         }\r
72         log.info("The " + hostname + " address is " + ret);\r
73         return ret;\r
74     }\r
75 \r
76     private static String execQuery(String queryString) {\r
77         Client client = ClientBuilder.newBuilder().build();\r
78         Response response = client.target(queryString).request().get();\r
79         return response.readEntity(String.class);\r
80     }\r
81 \r
82     public static String getServiceConfigInfoFromCBS(String hostname) {\r
83         String ret = null;\r
84         String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;\r
85         try {\r
86             ret = execQuery(url);\r
87         } catch (Exception e) {\r
88             log.warn(e.getMessage(), e);\r
89         }\r
90         log.info("The query url is: " + url + ". The corresponding configurations are " + ret);\r
91         return ret;\r
92     }\r
93 \r
94     public static String getMsbServerAddrWithHttpPrefix() {\r
95         String[] addrInfo = getMsbIpAndPort();\r
96         String ret = addrInfo[0] + ":" + addrInfo[1];\r
97         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {\r
98             ret = AlarmConst.HTTP + ret;\r
99         }\r
100         return ret;\r
101     }\r
102 \r
103     public static String getAaiAddr() {\r
104         return AlarmConst.HTTPS + AAI_HOSTNAME + ":8443";\r
105     }\r
106 \r
107     public static String[] getMsbIpAndPort() {\r
108         return split(getEnv(MSB_ADDR));\r
109     }\r
110 \r
111     public static String[] getMicroServiceIpAndPort() {\r
112         String[] serviceAddrInfo = null;\r
113         String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));\r
114         log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");\r
115 \r
116         if (info != null && !info.isEmpty()) {\r
117             if (!isIpAddress(info)) {\r
118                 info = getEnv(POD_IP);\r
119             }\r
120             serviceAddrInfo = split(info);\r
121         } else {\r
122             serviceAddrInfo = split(getEnv(HOSTNAME));\r
123         }\r
124         return serviceAddrInfo;\r
125     }\r
126 \r
127     private static boolean isIpAddress(String info) {\r
128         return IP_REG.matcher(info).matches();\r
129     }\r
130 \r
131     private static String[] split(String addr) {\r
132         String ip;\r
133         String port = "80";\r
134         if (addr.lastIndexOf(":") == -1) {\r
135             ip = addr;\r
136         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
137             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
138         } else {\r
139             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
140             port = addr.substring(addr.lastIndexOf(":") + 1);\r
141         }\r
142         return new String[]{ip, port};\r
143     }\r
144 \r
145 }\r