Released Version 1.4.7
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / config / MicroServiceConfig.java
1 /**
2  * Copyright  2017-2021 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.holmes.common.config;
17
18 import com.google.gson.JsonArray;
19 import com.google.gson.JsonObject;
20 import com.google.gson.JsonParser;
21 import org.apache.commons.lang3.StringUtils;
22 import org.onap.holmes.common.constant.AlarmConst;
23 import org.onap.holmes.common.utils.JerseyClient;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
28
29 public class MicroServiceConfig {
30
31     final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";
32     final static public String CONSUL_HOST = "CONSUL_HOST";
33     final static public String HOSTNAME = "HOSTNAME";
34     final static public String POD_IP = "POD_IP";
35     final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
36     final static public String MSB_IAG_SERVICE_HOST = "MSB_IAG_SERVICE_HOST";
37     final static public String MSB_IAG_SERVICE_PORT = "MSB_IAG_SERVICE_PORT";
38     final static public String AAI_ADDR = "AAI_ADDR";
39     final static public String NAMESPACE = "NAMESPACE";
40     final static public String PROTOCOL_HTTP = "http";
41     final static public String PROTOCOL_HTTPS = "https";
42     final static public int PLAIN_PORT = 80;
43     final static public int TLS_PORT = 443;
44
45     final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class);
46
47     public static String getConsulAddrInfo() {
48         return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
49     }
50
51     public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {
52         String ret = null;
53         String queryString = getConsulAddrInfo() + hostname;
54         log.info("Query the " + hostname + " address using the URL: " + queryString);
55         try {
56             JsonArray addrArray = JsonParser.parseString(execQuery(queryString)).getAsJsonArray();
57             if (addrArray.size() > 0) {
58                 JsonObject addrJson = addrArray.get(0).getAsJsonObject();
59                 if (addrJson != null && addrJson.get("ServiceAddress") != null
60                         && addrJson.get("ServicePort") != null) {
61                     ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson
62                             .get("ServicePort").getAsString();
63                 }
64             } else {
65                 log.info("No service info is returned from DCAE Consul. Hostname: {}", hostname);
66             }
67         } catch (Exception e) {
68             log.warn(e.getMessage(), e);
69         }
70         log.info("The " + hostname + " address is " + ret);
71         return ret;
72     }
73
74     private static String execQuery(String queryString) {
75         return JerseyClient.newInstance().get(queryString);
76     }
77
78     public static String getServiceConfigInfoFromCBS(String hostname) {
79         String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;
80         String ret = execQuery(url);
81         log.info("The query url is: " + url + ". The corresponding configurations are " + ret);
82         return ret;
83     }
84
85     public static String getMsbServerAddrWithHttpPrefix() {
86         String[] addrInfo = getMsbIpAndPort();
87         String ret = addrInfo[0] + ":" + addrInfo[1];
88         if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {
89             ret = AlarmConst.HTTP + ret;
90         }
91         return ret;
92     }
93
94     public static String getAaiAddr() {
95         boolean tlsEnabled = Boolean.valueOf(getEnv("ENABLE_ENCRYPT"));
96
97         return String.format("%s://%s.%s:%d",
98                 tlsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP,
99                 nullToEmptyString(getEnv(AAI_ADDR)),
100                 nullToEmptyString(getEnv(NAMESPACE)),
101                 tlsEnabled ? TLS_PORT : PLAIN_PORT);
102     }
103
104     public static String[] getMsbIpAndPort() {
105         return new String[]{getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)};
106     }
107
108     public static String[] getMicroServiceIpAndPort() {
109         String info = getEnv(POD_IP);
110         if (info != null) {
111             return split(info);
112         } else {
113             return split(getEnv(HOSTNAME));
114         }
115     }
116
117     private static String[] split(String addr) {
118         String ip;
119         String port = "80";
120         if (addr.lastIndexOf(":") == -1) {
121             ip = addr;
122         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {
123             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix
124         } else {
125             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
126             port = addr.substring(addr.lastIndexOf(":") + 1);
127         }
128         return new String[]{ip, port};
129     }
130
131     private static String nullToEmptyString(String input) {
132         return input == null ? StringUtils.EMPTY : input;
133     }
134
135 }