Introduce MSB Java SDK 65/10065/1
authorGuangrong Fu <fu.guangrong@zte.com.cn>
Sat, 2 Sep 2017 05:36:19 +0000 (13:36 +0800)
committerGuangrong Fu <fu.guangrong@zte.com.cn>
Sat, 2 Sep 2017 05:36:19 +0000 (13:36 +0800)
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>
holmes-actions/pom.xml
holmes-actions/src/main/java/org/onap/holmes/common/config/MicroServiceConfig.java
holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java
holmes-actions/src/test/java/org/onap/holmes/common/config/MicroServiceConfigTest.java

index 3f79bcf..e8a1605 100644 (file)
@@ -9,7 +9,6 @@
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">\r
     <modelVersion>4.0.0</modelVersion>\r
-\r
     <parent>\r
         <groupId>org.onap.holmes.common</groupId>\r
         <artifactId>holmes-common-parent</artifactId>\r
     <packaging>jar</packaging>\r
     <artifactId>holmes-actions</artifactId>\r
     <dependencies>\r
+        <dependency>\r
+            <groupId>org.onap.msb.java-sdk</groupId>\r
+            <artifactId>msb-java-sdk</artifactId>\r
+        </dependency>\r
         <dependency>\r
             <groupId>org.apache.activemq</groupId>\r
             <artifactId>activemq-core</artifactId>\r
index 6f5a67c..8f9c9e8 100644 (file)
@@ -31,6 +31,18 @@ public class MicroServiceConfig {
         return AlarmConst.HTTP + getProperty("MSB_ADDR");\r
     }\r
 \r
+    public static String getMsbServerIp() {\r
+        return getProperty("MSB_ADDR");\r
+    }\r
+\r
+    public static int getMsbServerPort() {\r
+        try {\r
+            return Integer.valueOf(getProperty("MSB_PORT"));\r
+        } catch (NumberFormatException e) {\r
+            return 80;\r
+        }\r
+    }\r
+\r
     public static String getServiceIp() {\r
         return getProperty("SERVICE_IP");\r
     }\r
index 1bb3fe3..8052f78 100644 (file)
 \r
 package org.onap.holmes.common.utils;\r
 \r
+import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;\r
+\r
 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
 import java.io.IOException;\r
 import lombok.extern.slf4j.Slf4j;\r
 import org.jvnet.hk2.annotations.Service;\r
-import org.onap.holmes.common.msb.MicroserviceBusRest;\r
 import org.onap.holmes.common.api.entity.ServiceRegisterEntity;\r
 import org.onap.holmes.common.config.MicroServiceConfig;\r
+import org.onap.holmes.common.exception.CorrelationException;\r
+import org.onap.holmes.common.msb.MicroserviceBusRest;\r
+import org.onap.msb.sdk.discovery.common.RouteException;\r
+import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;\r
+import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;\r
+import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;\r
 \r
 @Slf4j\r
 @Service\r
 public class MSBRegisterUtil {\r
 \r
     public void register(ServiceRegisterEntity entity) throws IOException {\r
-        log.info("start holmes micro service register");\r
+        log.info("Start register Holmes Service to MSB...");\r
         boolean flag = false;\r
         int retry = 0;\r
         while (!flag && retry < 20) {\r
-            log.info("Holmes microservice register. retry:" + retry);\r
+            log.info("Holmes Service Registration. Retry: " + retry);\r
             retry++;\r
             flag = innerRegister(entity);\r
             if (!flag) {\r
-                log.warn("micro service register failed, sleep 30S and try again.");\r
+                log.warn("Failed to register the service to MSB. Sleep 30s and try again.");\r
                 threadSleep(30000);\r
             } else {\r
-                log.info("micro service register success!");\r
+                log.info("Registration succeeded!");\r
                 break;\r
             }\r
         }\r
-        log.info("holmes micro service register end.");\r
+        log.info("Service registration completed.");\r
     }\r
 \r
     private boolean innerRegister(ServiceRegisterEntity entity) {\r
@@ -55,20 +62,52 @@ public class MSBRegisterUtil {
                     MicroServiceConfig.getMsbServerAddr(), MicroserviceBusRest.class);\r
             resourceserviceproxy.registerServce("false", entity);\r
         } catch (Exception error) {\r
-            log.error("microservice register failed!" + error.getMessage(), error);\r
+            log.error("Micro-service registration failed!" + error.getMessage(), error);\r
             return false;\r
         }\r
         return true;\r
     }\r
 \r
+    public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {\r
+        MSBServiceClient msbClient = new MSBServiceClient(MicroServiceConfig.getMsbServerIp(),\r
+                MicroServiceConfig.getMsbServerPort());\r
+\r
+        log.info("Start register Holmes Service to MSB...");\r
+        MicroServiceFullInfo microServiceFullInfo = null;\r
+        int retry = 0;\r
+        while (null == microServiceFullInfo && retry < 20) {\r
+            log.info("Holmes Service Registration. Retry: " + retry);\r
+            retry++;\r
+            try {\r
+                microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo, false);\r
+            } catch (RouteException e) {\r
+\r
+            }\r
+\r
+            if (null == microServiceFullInfo) {\r
+                log.warn("Failed to register the service to MSB. Sleep 30s and try again.");\r
+                threadSleep(30000);\r
+            } else {\r
+                log.info("Registration succeeded!");\r
+                break;\r
+            }\r
+        }\r
+\r
+        if (null == microServiceFullInfo) {\r
+            throw new CorrelationException("Failed to register the service to MSB!");\r
+        }\r
+\r
+        log.info("Service registration completed.");\r
+    }\r
+\r
     private void threadSleep(int second) {\r
-        log.info("start sleep ....");\r
+        log.info("Start sleeping...");\r
         try {\r
             Thread.sleep(second);\r
         } catch (InterruptedException error) {\r
-            log.error("thread sleep error.errorMsg:" + error.getMessage(), error);\r
+            log.error("thread sleep error message:" + error.getMessage(), error);\r
             Thread.currentThread().interrupt();\r
         }\r
-        log.info("sleep end .");\r
+        log.info("Wake up.");\r
     }\r
 }
\ No newline at end of file
index 33431ff..b29e490 100644 (file)
@@ -18,6 +18,7 @@ package org.onap.holmes.common.config;
 \r
 import static org.hamcrest.core.IsEqual.equalTo;\r
 import static org.junit.Assert.assertThat;\r
+import static org.junit.Assert.assertTrue;\r
 \r
 import org.junit.Test;\r
 \r
@@ -30,6 +31,32 @@ public class MicroServiceConfigTest {
         System.clearProperty("MSB_ADDR");\r
     }\r
 \r
+    @Test\r
+    public void getMsbServerIpTest() {\r
+        System.setProperty("MSB_ADDR", "10.54.23.79");\r
+        assertThat("10.54.23.79", equalTo(MicroServiceConfig.getMsbServerIp()));\r
+        System.clearProperty("MSB_ADDR");\r
+    }\r
+\r
+    @Test\r
+    public void getMsbPortTest() {\r
+        System.setProperty("MSB_PORT", "110");\r
+        assertTrue(110 == MicroServiceConfig.getMsbServerPort());\r
+        System.clearProperty("MSB_PORT");\r
+    }\r
+\r
+    @Test\r
+    public void getMsbPortTestNonnumeric() {\r
+        System.setProperty("MSB_PORT", "test");\r
+        assertTrue(80 == MicroServiceConfig.getMsbServerPort());\r
+        System.clearProperty("MSB_PORT");\r
+    }\r
+\r
+    @Test\r
+    public void getMsbPortTestNullValue() {\r
+        assertTrue(80 == MicroServiceConfig.getMsbServerPort());\r
+    }\r
+\r
     @Test\r
     public void getServiceIpTest() {\r
         System.setProperty("SERVICE_IP", "test");\r