-/**\r
- * Copyright 2017 ZTE Corporation.\r
- * <p>\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * <p>\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- * <p>\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package org.onap.holmes.common.config;\r
-\r
-import com.alibaba.fastjson.JSON;\r
-import com.alibaba.fastjson.JSONObject;\r
-\r
-import javax.ws.rs.client.Client;\r
-import javax.ws.rs.client.ClientBuilder;\r
-import javax.ws.rs.core.Response;\r
-\r
-import lombok.extern.slf4j.Slf4j;\r
-import org.onap.holmes.common.constant.AlarmConst;\r
-\r
-import java.util.regex.Pattern;\r
-\r
-@Slf4j\r
-public class MicroServiceConfig {\r
-\r
- final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";\r
- final static public String CONSUL_HOST = "CONSUL_HOST";\r
- final static public String HOSTNAME = "HOSTNAME";\r
- final static public String POD_IP = "POD_IP";\r
- final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";\r
- final static public String DOCKER_HOST = "DOCKER_HOST";\r
- final static public String MSB_ADDR = "MSB_ADDR";\r
- final static public Pattern IP_REG = Pattern.compile("(http(s)?://)?(\\d+\\.\\d+\\.\\d+\\.\\d+)(:(\\d+))?");\r
-\r
- public static String getEnv(String name) {\r
- String value = System.getenv(name);\r
- if (value == null) {\r
- value = System.getProperty(name);\r
- }\r
- return value;\r
- }\r
-\r
- public static String getConsulAddrInfo() {\r
- return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;\r
- }\r
-\r
- public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {\r
- String ret = null;\r
- String queryString = getConsulAddrInfo() + hostname;\r
- log.info("Query the " + hostname + " address using the URL: " + queryString);\r
- try {\r
- JSONObject addrJson = (JSONObject) JSON.parseArray(execQuery(queryString)).get(0);\r
- if (addrJson != null && addrJson.get("ServiceAddress") != null\r
- && addrJson.get("ServicePort") != null) {\r
- ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson\r
- .getString("ServicePort");\r
- }\r
- } catch (Exception e) {\r
- log.warn(e.getMessage(), e);\r
- }\r
- log.info("The " + hostname + " address is " + ret);\r
- return ret;\r
- }\r
-\r
- private static String execQuery(String queryString) {\r
- Client client = ClientBuilder.newBuilder().build();\r
- Response response = client.target(queryString).request().get();\r
- return response.readEntity(String.class);\r
- }\r
-\r
- public static String getServiceConfigInfoFromCBS(String hostname) {\r
- String ret = null;\r
- String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;\r
- try {\r
- ret = execQuery(url);\r
- } catch (Exception e) {\r
- log.warn(e.getMessage(), e);\r
- }\r
- log.info("The query url is: " + url + ". The corresponding configurations are " + ret);\r
- return ret;\r
- }\r
-\r
- public static String getMsbServerAddrWithHttpPrefix() {\r
- String[] addrInfo = getMsbIpAndPort();\r
- String ret = addrInfo[0] + ":" + addrInfo[1];\r
- if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {\r
- ret = AlarmConst.HTTP + ret;\r
- }\r
- return ret;\r
- }\r
-\r
- public static String[] getMsbIpAndPort() {\r
- return split(getEnv(MSB_ADDR));\r
- }\r
-\r
- public static String[] getMicroServiceIpAndPort() {\r
- String[] serviceAddrInfo = null;\r
- String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME));\r
- log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + ".");\r
-\r
- if (info != null && !info.isEmpty()) {\r
- if (!isIpAddress(info)) {\r
- info = getEnv(POD_IP);\r
- }\r
- serviceAddrInfo = split(info);\r
- } else {\r
- serviceAddrInfo = split(getEnv(HOSTNAME));\r
- }\r
- return serviceAddrInfo;\r
- }\r
-\r
- private static boolean isIpAddress(String info) {\r
- return IP_REG.matcher(info).matches();\r
- }\r
-\r
- private static String[] split(String addr) {\r
- String ip;\r
- String port = "80";\r
- if (addr.lastIndexOf(":") == -1) {\r
- ip = addr;\r
- } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
- ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix\r
- } else {\r
- ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
- port = addr.substring(addr.lastIndexOf(":") + 1);\r
- }\r
- return new String[]{ip, port};\r
- }\r
-\r
-}\r
+/**
+ * Copyright 2017-2021 ZTE Corporation.
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.holmes.common.config;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.holmes.common.constant.AlarmConst;
+import org.onap.holmes.common.utils.JerseyClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.onap.holmes.common.utils.CommonUtils.getEnv;
+
+public class MicroServiceConfig {
+
+ final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/";
+ final static public String CONSUL_HOST = "CONSUL_HOST";
+ final static public String HOSTNAME = "HOSTNAME";
+ final static public String POD_IP = "POD_IP";
+ final static public String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
+ final static public String MSB_IAG_SERVICE_HOST = "MSB_IAG_SERVICE_HOST";
+ final static public String MSB_IAG_SERVICE_PORT = "MSB_IAG_SERVICE_PORT";
+ final static public String AAI_ADDR = "AAI_ADDR";
+ final static public String NAMESPACE = "NAMESPACE";
+ final static public String PROTOCOL_HTTP = "http";
+ final static public String PROTOCOL_HTTPS = "https";
+ final static public int PLAIN_PORT = 80;
+ final static public int TLS_PORT = 443;
+
+ final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class);
+
+ public static String getConsulAddrInfo() {
+ return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF;
+ }
+
+ public static String getServiceAddrInfoFromDcaeConsulByHostName(String hostname) {
+ String ret = null;
+ String queryString = getConsulAddrInfo() + hostname;
+ log.info("Query the " + hostname + " address using the URL: " + queryString);
+ try {
+ JsonArray addrArray = JsonParser.parseString(execQuery(queryString)).getAsJsonArray();
+ if (addrArray.size() > 0) {
+ JsonObject addrJson = addrArray.get(0).getAsJsonObject();
+ if (addrJson != null && addrJson.get("ServiceAddress") != null
+ && addrJson.get("ServicePort") != null) {
+ ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson
+ .get("ServicePort").getAsString();
+ }
+ } else {
+ log.info("No service info is returned from DCAE Consul. Hostname: {}", hostname);
+ }
+ } catch (Exception e) {
+ log.warn(e.getMessage(), e);
+ }
+ log.info("The " + hostname + " address is " + ret);
+ return ret;
+ }
+
+ private static String execQuery(String queryString) {
+ return JerseyClient.newInstance().get(queryString);
+ }
+
+ public static String getServiceConfigInfoFromCBS(String hostname) {
+ String url = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(CONFIG_BINDING_SERVICE)) + "/service_component/" + hostname;
+ String ret = execQuery(url);
+ log.info("The query url is: " + url + ". The corresponding configurations are " + ret);
+ return ret;
+ }
+
+ public static String getMsbServerAddrWithHttpPrefix() {
+ String[] addrInfo = getMsbIpAndPort();
+ String ret = addrInfo[0] + ":" + addrInfo[1];
+ if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {
+ ret = AlarmConst.HTTP + ret;
+ }
+ return ret;
+ }
+
+ public static String getAaiAddr() {
+ boolean tlsEnabled = Boolean.valueOf(getEnv("ENABLE_ENCRYPT"));
+
+ return String.format("%s://%s.%s:%d",
+ tlsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP,
+ nullToEmptyString(getEnv(AAI_ADDR)),
+ nullToEmptyString(getEnv(NAMESPACE)),
+ tlsEnabled ? TLS_PORT : PLAIN_PORT);
+ }
+
+ public static String[] getMsbIpAndPort() {
+ return new String[]{getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)};
+ }
+
+ public static String[] getMicroServiceIpAndPort() {
+ String info = getEnv(POD_IP);
+ if (info != null) {
+ return split(info);
+ } else {
+ return split(getEnv(HOSTNAME));
+ }
+ }
+
+ private static String[] split(String addr) {
+ String ip;
+ String port = "80";
+ if (addr.lastIndexOf(":") == -1) {
+ ip = addr;
+ } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {
+ ip = addr.substring(addr.indexOf("//") + 2); //remove the http(s):// prefix
+ } else {
+ ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));
+ port = addr.substring(addr.lastIndexOf(":") + 1);
+ }
+ return new String[]{ip, port};
+ }
+
+ private static String nullToEmptyString(String input) {
+ return input == null ? StringUtils.EMPTY : input;
+ }
+
+}