Geted Holmes IP from Env Var
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / config / MicroServiceConfig.java
index c179280..66daebf 100644 (file)
@@ -25,15 +25,19 @@ import javax.ws.rs.core.Response;
 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
@@ -100,7 +104,11 @@ public class MicroServiceConfig {
         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
@@ -108,6 +116,10 @@ public class MicroServiceConfig {
         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