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=33bd1d2e7c763a5cdddc5e162345397e0eb7a7d6;hb=490fc3c1fafe50d5fb0e23db5cfd10730be96866;hp=c179280a00f83840d7904e41e1a1402e07c69676;hpb=c7fd788a1cfc030e54047701b1c1f27e42a91d1c;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 c179280..33bd1d2 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-2020 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,25 +15,30 @@ */ package org.onap.holmes.common.config; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; +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 java.util.regex.Pattern; -import lombok.extern.slf4j.Slf4j; -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 POD_IP = "POD_IP"; 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 AAI_HOSTNAME = "aai.onap"; + + final static public Logger log = LoggerFactory.getLogger(MicroServiceConfig.class); public static String getEnv(String name) { String value = System.getenv(name); @@ -52,11 +57,14 @@ 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); + JsonObject addrJson = JsonParser.parseString(execQuery(queryString)) + .getAsJsonArray() + .get(0) + .getAsJsonObject(); if (addrJson != null && addrJson.get("ServiceAddress") != null && addrJson.get("ServicePort") != null) { - ret = "http://" + addrJson.getString("ServiceAddress") + ":" + addrJson - .getString("ServicePort"); + ret = "http://" + addrJson.get("ServiceAddress").getAsString() + ":" + addrJson + .get("ServicePort").getAsString(); } } catch (Exception e) { log.warn(e.getMessage(), e); @@ -92,6 +100,10 @@ public class MicroServiceConfig { return ret; } + public static String getAaiAddr() { + return AlarmConst.HTTPS + AAI_HOSTNAME + ":8443"; + } + public static String[] getMsbIpAndPort() { return split(getEnv(MSB_ADDR)); } @@ -100,7 +112,11 @@ public class MicroServiceConfig { String[] serviceAddrInfo = null; String info = getServiceAddrInfoFromDcaeConsulByHostName(getEnv(HOSTNAME)); log.info("Got the service information of \"" + getEnv(HOSTNAME) + "\" from Consul. The response is " + info + "."); + if (info != null && !info.isEmpty()) { + if (!isIpAddress(info)) { + info = getEnv(POD_IP); + } serviceAddrInfo = split(info); } else { serviceAddrInfo = split(getEnv(HOSTNAME)); @@ -108,6 +124,10 @@ 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; String port = "80";