diff options
author | 2017-09-02 13:36:19 +0800 | |
---|---|---|
committer | 2017-09-02 13:36:19 +0800 | |
commit | 892ee11e90a7ad564a8dae5d744287db3bf3ba04 (patch) | |
tree | 27c7e6061b8c34d223755a86248bd4dd56a4ecb4 /holmes-actions/src | |
parent | 23d05bd2fc5e6b97ed54e50a0bafb0e9dc4ab406 (diff) |
Introduce MSB Java SDK
Add the registration logic using MSB Java SDK
Add corresponding unit test codes
Add dependencies into pom.xml
Change-Id: I4c003c3585702d4956855f62c7ce341bc66b07f4
Issue-ID: HOLMES-48
Signed-off-by: Guangrong Fu <fu.guangrong@zte.com.cn>
Diffstat (limited to 'holmes-actions/src')
3 files changed, 88 insertions, 10 deletions
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 6f5a67c..8f9c9e8 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 @@ -31,6 +31,18 @@ public class MicroServiceConfig { return AlarmConst.HTTP + getProperty("MSB_ADDR");
}
+ public static String getMsbServerIp() {
+ return getProperty("MSB_ADDR");
+ }
+
+ public static int getMsbServerPort() {
+ try {
+ return Integer.valueOf(getProperty("MSB_PORT"));
+ } catch (NumberFormatException e) {
+ return 80;
+ }
+ }
+
public static String getServiceIp() {
return getProperty("SERVICE_IP");
}
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java index 1bb3fe3..8052f78 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java @@ -16,35 +16,42 @@ package org.onap.holmes.common.utils;
+import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
+
import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.jvnet.hk2.annotations.Service;
-import org.onap.holmes.common.msb.MicroserviceBusRest;
import org.onap.holmes.common.api.entity.ServiceRegisterEntity;
import org.onap.holmes.common.config.MicroServiceConfig;
+import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.msb.MicroserviceBusRest;
+import org.onap.msb.sdk.discovery.common.RouteException;
+import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
+import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
+import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
@Slf4j
@Service
public class MSBRegisterUtil {
public void register(ServiceRegisterEntity entity) throws IOException {
- log.info("start holmes micro service register");
+ log.info("Start register Holmes Service to MSB...");
boolean flag = false;
int retry = 0;
while (!flag && retry < 20) {
- log.info("Holmes microservice register. retry:" + retry);
+ log.info("Holmes Service Registration. Retry: " + retry);
retry++;
flag = innerRegister(entity);
if (!flag) {
- log.warn("micro service register failed, sleep 30S and try again.");
+ log.warn("Failed to register the service to MSB. Sleep 30s and try again.");
threadSleep(30000);
} else {
- log.info("micro service register success!");
+ log.info("Registration succeeded!");
break;
}
}
- log.info("holmes micro service register end.");
+ log.info("Service registration completed.");
}
private boolean innerRegister(ServiceRegisterEntity entity) {
@@ -55,20 +62,52 @@ public class MSBRegisterUtil { MicroServiceConfig.getMsbServerAddr(), MicroserviceBusRest.class);
resourceserviceproxy.registerServce("false", entity);
} catch (Exception error) {
- log.error("microservice register failed!" + error.getMessage(), error);
+ log.error("Micro-service registration failed!" + error.getMessage(), error);
return false;
}
return true;
}
+ public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {
+ MSBServiceClient msbClient = new MSBServiceClient(MicroServiceConfig.getMsbServerIp(),
+ MicroServiceConfig.getMsbServerPort());
+
+ log.info("Start register Holmes Service to MSB...");
+ MicroServiceFullInfo microServiceFullInfo = null;
+ int retry = 0;
+ while (null == microServiceFullInfo && retry < 20) {
+ log.info("Holmes Service Registration. Retry: " + retry);
+ retry++;
+ try {
+ microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo, false);
+ } catch (RouteException e) {
+
+ }
+
+ if (null == microServiceFullInfo) {
+ log.warn("Failed to register the service to MSB. Sleep 30s and try again.");
+ threadSleep(30000);
+ } else {
+ log.info("Registration succeeded!");
+ break;
+ }
+ }
+
+ if (null == microServiceFullInfo) {
+ throw new CorrelationException("Failed to register the service to MSB!");
+ }
+
+ log.info("Service registration completed.");
+ }
+
private void threadSleep(int second) {
- log.info("start sleep ....");
+ log.info("Start sleeping...");
try {
Thread.sleep(second);
} catch (InterruptedException error) {
- log.error("thread sleep error.errorMsg:" + error.getMessage(), error);
+ log.error("thread sleep error message:" + error.getMessage(), error);
Thread.currentThread().interrupt();
}
- log.info("sleep end .");
+ log.info("Wake up.");
}
}
\ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java index 33431ff..b29e490 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java @@ -18,6 +18,7 @@ package org.onap.holmes.common.config; import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
import org.junit.Test;
@@ -31,6 +32,32 @@ public class MicroServiceConfigTest { }
@Test
+ public void getMsbServerIpTest() {
+ System.setProperty("MSB_ADDR", "10.54.23.79");
+ assertThat("10.54.23.79", equalTo(MicroServiceConfig.getMsbServerIp()));
+ System.clearProperty("MSB_ADDR");
+ }
+
+ @Test
+ public void getMsbPortTest() {
+ System.setProperty("MSB_PORT", "110");
+ assertTrue(110 == MicroServiceConfig.getMsbServerPort());
+ System.clearProperty("MSB_PORT");
+ }
+
+ @Test
+ public void getMsbPortTestNonnumeric() {
+ System.setProperty("MSB_PORT", "test");
+ assertTrue(80 == MicroServiceConfig.getMsbServerPort());
+ System.clearProperty("MSB_PORT");
+ }
+
+ @Test
+ public void getMsbPortTestNullValue() {
+ assertTrue(80 == MicroServiceConfig.getMsbServerPort());
+ }
+
+ @Test
public void getServiceIpTest() {
System.setProperty("SERVICE_IP", "test");
assertThat("test", equalTo(MicroServiceConfig.getServiceIp()));
|