From: Guangrong Fu Date: Sat, 2 Sep 2017 05:36:19 +0000 (+0800) Subject: Introduce MSB Java SDK X-Git-Tag: v1.0.0~48 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=holmes%2Fcommon.git;a=commitdiff_plain;h=892ee11e90a7ad564a8dae5d744287db3bf3ba04 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 --- diff --git a/holmes-actions/pom.xml b/holmes-actions/pom.xml index 3f79bcf..e8a1605 100644 --- a/holmes-actions/pom.xml +++ b/holmes-actions/pom.xml @@ -9,7 +9,6 @@ 4.0.0 - org.onap.holmes.common holmes-common-parent @@ -20,6 +19,10 @@ jar holmes-actions + + org.onap.msb.java-sdk + msb-java-sdk + org.apache.activemq activemq-core 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; @@ -30,6 +31,32 @@ public class MicroServiceConfigTest { System.clearProperty("MSB_ADDR"); } + @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");