191d8978a570a2e013837dc9d42e20f15804b2a5
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / config / MicroServiceConfig.java
1 /**\r
2  * Copyright  2017-2021 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.JsonArray;\r
19 import com.google.gson.JsonObject;\r
20 import com.google.gson.JsonParser;\r
21 import org.onap.holmes.common.constant.AlarmConst;\r
22 import org.onap.holmes.common.utils.JerseyClient;\r
23 import org.slf4j.Logger;\r
24 import org.slf4j.LoggerFactory;\r
25 \r
26 import static org.onap.holmes.common.utils.CommonUtils.getEnv;\r
27 \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 POD_IP = "POD_IP";\r
34     final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";\r
35     final static public String MSB_ADDR = "MSB_ADDR";\r
36     final static public String MSB_IAG_SERVICE_HOST = "MSB_IAG_SERVICE_HOST";\r
37     final static public String MSB_IAG_SERVICE_PORT = "MSB_IAG_SERVICE_PORT";\r
38 \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 getConsulAddrInfo() {\r
44         return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
45     }\r
46 \r
47     public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {\r
48         String ret = null;\r
49         String queryString = getConsulAddrInfo() + hostname;\r
50         log.info("Query the " + hostname + " address using the URL: " + queryString);\r
51         try {\r
52             JsonArray addrArray = JsonParser.parseString(execQuery(queryString)).getAsJsonArray();\r
53             if (addrArray.size() > 0) {\r
54                 JsonObject addrJson = addrArray.get(0).getAsJsonObject();\r
55                 if (addrJson != null && addrJson.get("ServiceAddress") != null\r
56                         && addrJson.get("ServicePort") != null) {\r
57                     ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson\r
58                             .get("ServicePort").getAsString();\r
59                 }\r
60             } else {\r
61                 log.info("No service info is returned from DCAE Consul. Hostname: {}", hostname);\r
62             }\r
63         } catch (Exception e) {\r
64             log.warn(e.getMessage(), e);\r
65         }\r
66         log.info("The " + hostname + " address is " + ret);\r
67         return ret;\r
68     }\r
69 \r
70     private static String execQuery(String queryString) {\r
71         return JerseyClient.newInstance().get(queryString);\r
72     }\r
73 \r
74     public static String getServiceConfigInfoFromCBS(String hostname) {\r
75         String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;\r
76         String ret = execQuery(url);\r
77         log.info("The query url is: " + url + ". The corresponding configurations are " + ret);\r
78         return ret;\r
79     }\r
80 \r
81     public static String getMsbServerAddrWithHttpPrefix() {\r
82         String[] addrInfo = getMsbIpAndPort();\r
83         String ret = addrInfo[0] + ":" + addrInfo[1];\r
84         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {\r
85             ret = AlarmConst.HTTP + ret;\r
86         }\r
87         return ret;\r
88     }\r
89 \r
90     public static String getAaiAddr() {\r
91         return AlarmConst.HTTPS + AAI_HOSTNAME + ":8443";\r
92     }\r
93 \r
94     public static String[] getMsbIpAndPort() {\r
95         return new String[]{getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)};\r
96     }\r
97 \r
98     public static String[] getMicroServiceIpAndPort() {\r
99         String info = getEnv(POD_IP);\r
100         if (info != null) {\r
101             return split(info);\r
102         } else {\r
103             return split(getEnv(HOSTNAME));\r
104         }\r
105     }\r
106 \r
107     private static String[] split(String addr) {\r
108         String ip;\r
109         String port = "80";\r
110         if (addr.lastIndexOf(":") == -1) {\r
111             ip = addr;\r
112         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
113             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
114         } else {\r
115             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
116             port = addr.substring(addr.lastIndexOf(":") + 1);\r
117         }\r
118         return new String[]{ip, port};\r
119     }\r
120 \r
121 }\r