Geted Holmes IP from Env Var 12/70712/1 1.2.2
authorGuangrongFu <fu.guangrong@zte.com.cn>
Thu, 18 Oct 2018 06:44:52 +0000 (14:44 +0800)
committerGuangrongFu <fu.guangrong@zte.com.cn>
Thu, 18 Oct 2018 06:44:52 +0000 (14:44 +0800)
Change-Id: Idf803dd37a33cf325af71dbf43fca3fe2dacaefe
Issue-ID: HOLMES-175
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
holmes-actions/pom.xml
holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java
holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java
pom.xml
version.properties

index 5090b74..2b43532 100644 (file)
@@ -12,7 +12,7 @@
     <parent>\r
         <groupId>org.onap.holmes.common</groupId>\r
         <artifactId>holmes-common-parent</artifactId>\r
-        <version>1.2.1</version>\r
+        <version>1.2.2</version>\r
     </parent>\r
 \r
     <name>holmes-common-service</name>\r
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
index 59af9d3..a5d6597 100644 (file)
@@ -31,6 +31,7 @@ import org.powermock.api.easymock.PowerMock;
 import org.powermock.core.classloader.annotations.PowerMockIgnore;\r
 import org.powermock.core.classloader.annotations.PrepareForTest;\r
 import org.powermock.modules.junit4.rule.PowerMockRule;\r
+import org.powermock.reflect.internal.WhiteboxImpl;\r
 \r
 @PrepareForTest(MicroServiceConfig.class)\r
 @PowerMockIgnore({"javax.ws.*"})\r
@@ -292,4 +293,52 @@ public class MicroServiceConfigTest {
 \r
         System.clearProperty(MSB_ADDR);\r
     }\r
+\r
+    @Test\r
+    public void isValidIpAddress_with_port() throws Exception {\r
+        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21:90");\r
+        assertThat(res, is(true));\r
+    }\r
+\r
+    @Test\r
+    public void isValidIpAddress_without_port() throws Exception {\r
+        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "10.75.13.21");\r
+        assertThat(res, is(true));\r
+    }\r
+\r
+    @Test\r
+    public void isValidIpAddress_with_port_with_http_prefix() throws Exception {\r
+        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://10.75.13.21:90");\r
+        assertThat(res, is(true));\r
+    }\r
+\r
+    @Test\r
+    public void isValidIpAddress_without_port_with_https_prefix() throws Exception {\r
+        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://10.75.13.21");\r
+        assertThat(res, is(true));\r
+    }\r
+\r
+    @Test\r
+    public void isValidIpAddress_invalid_ip_without_port() throws Exception {\r
+        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt");\r
+        assertThat(res, is(false));\r
+    }\r
+\r
+    @Test\r
+    public void isValidIpAddress_invalid_ip_with_port() throws Exception {\r
+        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "holmes-rule-mgmt:80");\r
+        assertThat(res, is(false));\r
+    }\r
+\r
+    @Test\r
+    public void isValidIpAddress_invalid_ip_without_port_with_http_prefix() throws Exception {\r
+        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "http://holmes-rule-mgmt");\r
+        assertThat(res, is(false));\r
+    }\r
+\r
+    @Test\r
+    public void isValidIpAddress_invalid_ip_with_port_with_https_prefix() throws Exception {\r
+        boolean res = WhiteboxImpl.invokeMethod(MicroServiceConfig.class, "isIpAddress", "https://holmes-rule-mgmt:80");\r
+        assertThat(res, is(false));\r
+    }\r
 }
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 5e450b8..7b5932c 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -20,7 +20,7 @@
     <artifactId>holmes-common-parent</artifactId>\r
     <packaging>pom</packaging>\r
 \r
-    <version>1.2.1</version>\r
+    <version>1.2.2</version>\r
     <name>holmes-common</name>\r
     <modules>\r
         <module>holmes-actions</module>\r
index 146a672..cfb32e0 100644 (file)
@@ -4,7 +4,7 @@
 \r
 major=1\r
 minor=2\r
-patch=1\r
+patch=2\r
 \r
 base_version=${major}.${minor}.${patch}\r
 \r