From 51e4c57db505195b9f4f1b4c45f6f68977986331 Mon Sep 17 00:00:00 2001 From: 6092002067 Date: Wed, 8 Mar 2017 17:44:56 +0800 Subject: Add msb register code Issue-ID:HOLMES-50 Change-Id: I854c07b552dddba14b317b41b16d6e198b23e32d Signed-off-by: youbowu --- .../org/openo/holmes/rulemgt/RuleAppConfig.java | 12 ---------- .../rulemgt/bolt/enginebolt/EngineService.java | 12 ++++------ .../holmes/rulemgt/resources/RuleMgtResources.java | 26 ++++++++++++++++++++++ .../openo/holmes/rulemgt/RuleAppConfigTest.java | 7 ------ .../rulemgt/bolt/enginebolt/EngineServiceTest.java | 14 ++++++++---- .../rulemgt/bolt/enginebolt/EngineWrapperTest.java | 4 ++-- 6 files changed, 42 insertions(+), 33 deletions(-) diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java index 57ae0c5..4fcb486 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/RuleAppConfig.java @@ -32,9 +32,6 @@ public class RuleAppConfig extends Configuration { @NotEmpty private String apidescription = "Holmes rule management rest API"; - @NotEmpty - private String msbServerAddr; - @Valid @NotNull private DataSourceFactory database = new DataSourceFactory(); @@ -58,13 +55,4 @@ public class RuleAppConfig extends Configuration { this.apidescription = apidescription; } - @JsonProperty - public String getMsbServerAddr() { - return msbServerAddr; - } - - @JsonProperty - public void setMsbServerAddr(String msbServerAddr) { - this.msbServerAddr = msbServerAddr; - } } diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java index b96303a..1a1aa83 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java @@ -19,7 +19,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; -import javax.inject.Inject; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; @@ -32,9 +31,9 @@ import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.jvnet.hk2.annotations.Service; +import org.openo.holmes.common.config.MicroServiceConfig; import org.openo.holmes.common.exception.CorrelationException; import org.openo.holmes.common.utils.I18nProxy; -import org.openo.holmes.rulemgt.RuleAppConfig; import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; import org.openo.holmes.rulemgt.constant.RuleMgtConstant; @@ -42,24 +41,21 @@ import org.openo.holmes.rulemgt.constant.RuleMgtConstant; @Service public class EngineService { - @Inject - private RuleAppConfig ruleAppConfig; - protected HttpResponse delete(String packageName) throws IOException { - return deleteRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName); + return deleteRequest(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName); } protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine) throws IOException { ObjectMapper mapper = new ObjectMapper(); String content = mapper.writeValueAsString(correlationCheckRule4Engine); - return postRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content); + return postRequest(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content); } protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException { ObjectMapper mapper = new ObjectMapper(); String content = mapper.writeValueAsString(correlationDeployRule4Engine); - return putRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content); + return putRequest(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content); } private HttpResponse postRequest(String url, String content) throws IOException { diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java index 682faf0..08d66f7 100644 --- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java +++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/resources/RuleMgtResources.java @@ -22,6 +22,7 @@ import io.swagger.annotations.ApiParam; import io.swagger.annotations.SwaggerDefinition; import java.io.IOException; import java.util.Locale; +import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.DELETE; @@ -36,11 +37,14 @@ import javax.ws.rs.core.MediaType; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONObject; import org.jvnet.hk2.annotations.Service; +import org.openo.holmes.common.api.entity.ServiceRegisterEntity; +import org.openo.holmes.common.config.MicroServiceConfig; import org.openo.holmes.common.exception.CorrelationException; import org.openo.holmes.common.utils.ExceptionUtil; import org.openo.holmes.common.utils.I18nProxy; import org.openo.holmes.common.utils.JacksonUtil; import org.openo.holmes.common.utils.LanguageUtil; +import org.openo.holmes.common.utils.MSBRegisterUtil; import org.openo.holmes.common.utils.UserUtil; import org.openo.holmes.rulemgt.bean.request.RuleCreateRequest; import org.openo.holmes.rulemgt.bean.request.RuleDeleteRequest; @@ -59,9 +63,20 @@ import org.openo.holmes.rulemgt.wrapper.RuleMgtWrapper; @Slf4j public class RuleMgtResources { + @Inject + private MSBRegisterUtil msbRegisterUtil; @Inject private RuleMgtWrapper ruleMgtWrapper; + @PostConstruct + public void init() { + try { + msbRegisterUtil.register(initServiceEntity()); + } catch (IOException e) { + log.warn("Micro service registry httpclient close failure",e); + } + } + @PUT @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "Save the alarm+ rule to the database, and deployed to the engine when the enable to open.", response = RuleAddAndUpdateResponse.class) @@ -161,4 +176,15 @@ public class RuleMgtResources { I18nProxy.RULE_MANAGEMENT_DATA_FORMAT_ERROR)); } } + + private ServiceRegisterEntity initServiceEntity() { + ServiceRegisterEntity serviceRegisterEntity = new ServiceRegisterEntity(); + serviceRegisterEntity.setServiceName("holmes"); + serviceRegisterEntity.setProtocol("REST"); + serviceRegisterEntity.setVersion("v1"); + serviceRegisterEntity.setUrl("/api/holmes/v1"); + serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9101", 0); + serviceRegisterEntity.setVisualRange("1"); + return serviceRegisterEntity; + } } diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java index e451410..d14e55e 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleAppConfigTest.java @@ -56,11 +56,4 @@ public class RuleAppConfigTest { ruleAppConfig.setApidescription(value); assertThat(ruleAppConfig.getApidescription(), equalTo(value)); } - - @Test - public void getterAndSetter4MsbServerAddr() throws Exception { - final String value = "msbServerAddr"; - ruleAppConfig.setMsbServerAddr(value); - assertThat(ruleAppConfig.getMsbServerAddr(), equalTo(value)); - } } \ No newline at end of file diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java index 596a408..3b1c9bd 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java @@ -19,18 +19,22 @@ package org.openo.holmes.rulemgt.bolt.enginebolt; import org.apache.http.HttpResponse; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; import org.easymock.EasyMock; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.openo.holmes.rulemgt.RuleAppConfig; import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine; import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine; import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.rule.PowerMockRule; -import org.powermock.reflect.Whitebox; +@PrepareForTest({HttpClients.class, CloseableHttpClient.class}) public class EngineServiceTest { @Rule @@ -39,14 +43,16 @@ public class EngineServiceTest { public PowerMockRule powerMockRule = new PowerMockRule(); private EngineService engineService; private HttpResponse httpResponseMock; - private RuleAppConfig ruleAppConfig = new RuleAppConfig(); + private CloseableHttpClient closeableHttpClient; private CorrelationDeployRule4Engine correlationDeployRule4Engine; + private CloseableHttpResponse closeableHttpResponseMock; @Before public void setUp() { engineService = new EngineService(); + closeableHttpClient = PowerMock.createMock(CloseableHttpClient.class); httpResponseMock = PowerMock.createMock(HttpResponse.class); - Whitebox.setInternalState(engineService, "ruleAppConfig", ruleAppConfig); + closeableHttpResponseMock = PowerMock.createMock(CloseableHttpResponse.class); correlationDeployRule4Engine = new CorrelationDeployRule4Engine(); correlationDeployRule4Engine.setContent("{\"package\":\"test\"}"); correlationDeployRule4Engine.setEngineId("engine_id"); diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java index f672415..8d25d6a 100644 --- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java +++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java @@ -165,7 +165,7 @@ public class EngineWrapperTest { } @Test - public void checkRuleFromEngine_invoke_rule_delete_exception() throws Exception { + public void checkRuleFromEngine_rule_delete_exception() throws Exception { thrown.expect(CorrelationException.class); thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CALL_CHECK_RULE_REST_FAILED); @@ -179,7 +179,7 @@ public class EngineWrapperTest { } @Test - public void checkRuleFromEngine_http_status_not_ok() throws Exception { + public void checkRuleFromEngine_http_status_not_200() throws Exception { thrown.expect(CorrelationException.class); thrown.expectMessage(I18nProxy.RULE_MANAGEMENT_CHECK_NO_PASS); -- cgit 1.2.3-korg