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