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
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
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
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
\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