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=621e23bce0bed476243037f919130ff8379b244e;hb=839fbb5f6d868df469743473b1e52a241124c369;hp=d86f56ce55c32acd344710f8a73fa3240bbc1bb2;hpb=d30677e80b8756048f9633ecba4c26360e3f644c;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 d86f56c..621e23b 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 @@ -1,143 +1,140 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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 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.constant.AlarmConst; - -@Slf4j -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 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); - } - return value; - } - - public static String getConsulAddrInfo() { - return getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF; - } - - public static String getConfigBindingServiceAddrInfo() { - String ret = null; - String queryString = getConsulAddrInfo() + CONFIG_BINDING_SERVICE; - log.info("Query the CBS address using the URL: " + queryString); - 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); - } - log.info("The CBS address is " + ret); - return ret; - } - - 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 String getServiceAddrInfoFromCBS(String serviceName) { - String ret = null; - String url = getConfigBindingServiceAddrInfo() + "/service_component/" +serviceName; - try { - JSONObject jsonObject = JSONObject.fromObject(execQuery(url)); - log.info("The origin configurations (" + url + ") returned by DCAE is: " + jsonObject.toString()); - 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[] getMsbAddrInfo() { - String[] msbServerInfo = null; - - //String info = getServiceAddrInfoFromCBS(MSB_ADDR); - String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME)); - log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from CBS. The response is " + info + "."); - - if (info != null){ - JSONObject infoObj = JSONObject.fromObject(info); - String msbInfoTmp = infoObj.has("msb.hostname") ? infoObj.getString("msb.hostname") : null; - if (msbInfoTmp != null) { - msbServerInfo = split(msbInfoTmp); - } else { - msbServerInfo = split(getEnv(MSB_ADDR)); - } - } else { - msbServerInfo = split(getEnv(MSB_ADDR)); - } - - return msbServerInfo; - } - - public static String[] getServiceAddrInfo() { - String[] serviceAddrInfo = null; - String info = getServiceAddrInfoFromCBS(getEnv(HOSTNAME)); - log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from CBS. The response is " + info + "."); - 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}; - } - -} +/** + * Copyright 2017-2021 ZTE Corporation. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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_ADDR = "MSB_ADDR"; + 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 BASE_URL = "BASE_URL"; + final static public String PRE_ADDR = "PRE_ADDR"; + final static public String POST_ADDR = "POST_ADDR"; + final static public String AAI_ADDR = "AAI_ADDR"; + 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%s.%s:%d", + tlsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP, + nullToEmptyString(getEnv(PRE_ADDR)), + nullToEmptyString(getEnv(AAI_ADDR)), + nullToEmptyString(getEnv(POST_ADDR)), + nullToEmptyString(getEnv(BASE_URL)), + 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; + } + +}