X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=holmes-actions%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fholmes%2Fcommon%2Fconfig%2FMicroServiceConfig.java;h=bb78288be6d208a365fff8a39174406d1d35b8b9;hb=9d2cccba20f32fe54e4ccf1f433f68b5cee95bd5;hp=8f9c9e8951b1200640555a66588aa830fa1da079;hpb=8eedabbb6faf3bc2ba10df850e68cf719cd6c8ad;p=holmes%2Fcommon.git diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java index 8f9c9e8..bb78288 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java @@ -15,11 +15,27 @@ */ package org.onap.holmes.common.config; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.Response; +import lombok.extern.slf4j.Slf4j; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; +import org.glassfish.jersey.client.ClientConfig; +import org.onap.holmes.common.api.stat.Alarm; import org.onap.holmes.common.constant.AlarmConst; +@Slf4j public class MicroServiceConfig { - private static String getProperty(String name) { + 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 CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE"; + final static public String DOCKER_HOST = "DOCKER_HOST"; + final static public String MSB_ADDR = "MSB_ADDR"; + + private static String getEnv(String name) { String value = System.getenv(name); if (value == null) { value = System.getProperty(name); @@ -27,24 +43,89 @@ public class MicroServiceConfig { return value; } - public static String getMsbServerAddr() { - return AlarmConst.HTTP + getProperty("MSB_ADDR"); + public static String getConsulAddrInfo() { + return getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF; + } + + public static String getConfigBindingServiceAddrInfo() { + String ret = null; + String queryString = getConsulAddrInfo() + CONFIG_BINDING_SERVICE; + try { + JSONObject addrJson = (JSONObject) JSONArray.fromObject(execQuery(queryString)).get(0); + if (addrJson.has("ServiceAddress") && addrJson.has("ServicePort")) { + ret = addrJson.getString("ServiceAddress") + ":" + addrJson.getString("ServicePort"); + } + } catch (Exception e) { + log.warn(e.getMessage(), e); + } + return ret; } - public static String getMsbServerIp() { - return getProperty("MSB_ADDR"); + private static String execQuery(String queryString) { + Client client = ClientBuilder.newClient(new ClientConfig()); + Response response = client.target(queryString).request().get(); + return response.readEntity(String.class); } - public static int getMsbServerPort() { + public static String getServiceAddrInfoFromCBS(String serviceName) { + String ret = null; + String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName; try { - return Integer.valueOf(getProperty("MSB_PORT")); - } catch (NumberFormatException e) { - return 80; + JSONObject jsonObject = JSONObject.fromObject(execQuery(url)); + if (jsonObject.has(serviceName)) { + ret = (String) jsonObject.getJSONArray(serviceName).get(0); + } + } catch (Exception e) { + log.warn(e.getMessage(), e); + } + return ret; + } + + public static String getMsbServerAddr() { + String[] addrInfo = getMsbAddrInfo(); + String ret = addrInfo[0] + ":" + addrInfo[1]; + if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){ + ret = AlarmConst.HTTP + ret; } + return ret; } - public static String getServiceIp() { - return getProperty("SERVICE_IP"); + public static String[] getMsbAddrInfo() { + String[] msbServerInfo = null; + + String info = getServiceAddrInfoFromCBS(MSB_ADDR); + if (info != null){ + msbServerInfo = split(info); + } else { + msbServerInfo = split(getEnv(MSB_ADDR)); + } + + return msbServerInfo; + } + + public static String[] getServiceAddrInfo() { + String[] serviceAddrInfo = null; + String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME)); + if (info != null){ + serviceAddrInfo = split(info); + } else { + serviceAddrInfo = split(getEnv(HOSTNAME)); + } + return serviceAddrInfo; + } + + 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}; } }