Geted Holmes IP from Env Var
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / config / MicroServiceConfig.java
index f74d1b2..66daebf 100644 (file)
@@ -1,12 +1,12 @@
 /**\r
  * Copyright  2017 ZTE Corporation.\r
- *\r
+ * <p>\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
- *\r
- *     http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
+ * <p>\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * <p>\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
@@ -17,21 +17,27 @@ package org.onap.holmes.common.config;
 \r
 import com.alibaba.fastjson.JSON;\r
 import com.alibaba.fastjson.JSONObject;\r
+\r
 import javax.ws.rs.client.Client;\r
 import javax.ws.rs.client.ClientBuilder;\r
 import javax.ws.rs.core.Response;\r
+\r
 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
@@ -84,7 +90,7 @@ public class MicroServiceConfig {
     public static String getMsbServerAddrWithHttpPrefix() {\r
         String[] addrInfo = getMsbIpAndPort();\r
         String ret = addrInfo[0] + ":" + addrInfo[1];\r
-        if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)){\r
+        if (!ret.startsWith(AlarmConst.HTTP) || !ret.startsWith(AlarmConst.HTTPS)) {\r
             ret = AlarmConst.HTTP + ret;\r
         }\r
         return ret;\r
@@ -98,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
-        if (info != null && !info.isEmpty()){\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
@@ -106,10 +116,14 @@ 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
-        if (addr.lastIndexOf(":") == -1){\r
+        if (addr.lastIndexOf(":") == -1) {\r
             ip = addr;\r
         } else if (addr.lastIndexOf(":") < 5 && addr.indexOf("://") != -1) {\r
             ip = addr.substring(addr.indexOf("//") + 2);    //remove the http(s):// prefix\r
@@ -117,7 +131,7 @@ public class MicroServiceConfig {
             ip = addr.substring(addr.indexOf("://") != -1 ? addr.indexOf("//") + 2 : 0, addr.lastIndexOf(":"));\r
             port = addr.substring(addr.lastIndexOf(":") + 1);\r
         }\r
-        return new String[] {ip, port};\r
+        return new String[]{ip, port};\r
     }\r
 \r
 }\r