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=43fef116acfd41d7b8b9b681677398b79ab4995c;hb=6cc976de165087178e784c40423242bc7f1b07ee;hp=3d94325c7e15c1173f7537ec4192384d25b705c7;hpb=76e5d677724b13160bb3360b734239192b7be20e;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 3d94325..43fef11 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,5 +1,5 @@ /** - * Copyright 2017 ZTE Corporation. + * 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. @@ -15,19 +15,21 @@ */ package org.onap.holmes.common.config; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.onap.holmes.common.constant.AlarmConst; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.Response; - -import lombok.extern.slf4j.Slf4j; -import org.onap.holmes.common.constant.AlarmConst; - import java.util.regex.Pattern; -@Slf4j +import static org.onap.holmes.common.utils.CommonUtils.getEnv; +import static org.onap.holmes.common.utils.CommonUtils.isIpAddress; + public class MicroServiceConfig { final static public String CONSUL_ADDR_SUF = ":8500/v1/catalog/service/"; @@ -37,16 +39,12 @@ public class MicroServiceConfig { 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"; - final static public Pattern IP_REG = Pattern.compile("(http(s)?://)?(\\d+\\.\\d+\\.\\d+\\.\\d+)(:(\\d+))?"); + 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_HOSTNAME = "aai.onap"; - public static String getEnv(String name) { - String value = System.getenv(name); - if (value == null) { - value = System.getProperty(name); - } - return value; - } + final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class); public static String getConsulAddrInfo() { return "http://" + getEnv(CONSUL_HOST) + CONSUL_ADDR_SUF; @@ -57,11 +55,16 @@ public class MicroServiceConfig { String queryString = getConsulAddrInfo() + hostname; log.info("Query the " + hostname + " address using the URL: " + queryString); try { - JSONObject addrJson = (JSONObject) JSON.parseArray(execQuery(queryString)).get(0); - if (addrJson != null && addrJson.get("ServiceAddress") != null - && addrJson.get("ServicePort") != null) { - ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson - .getString("ServicePort"); + 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); @@ -102,7 +105,7 @@ public class MicroServiceConfig { } public static String[] getMsbIpAndPort() { - return split(getEnv(MSB_ADDR)); + return new String[]{getEnv(MSB_IAG_SERVICE_HOST), getEnv(MSB_IAG_SERVICE_PORT)}; } public static String[] getMicroServiceIpAndPort() { @@ -121,9 +124,7 @@ public class MicroServiceConfig { return serviceAddrInfo; } - private static boolean isIpAddress(String info) { - return IP_REG.matcher(info).matches(); - } + private static String[] split(String addr) { String ip;