From 105437a89bd5bcfcaf40dac25e2c087aafb0996b Mon Sep 17 00:00:00 2001 From: Shiwei Tian Date: Fri, 13 Oct 2017 14:11:57 +0800 Subject: [PATCH] modify not publish messages to DMaaP Issue-ID: HOLMES-71 Change-Id: I96097090b5d2ba2ab611ccb326e4670eba39cea7 Signed-off-by: Shiwei Tian --- .../java/org/onap/holmes/common/aai/AaiQuery.java | 1 - .../holmes/common/dcae/DcaeConfigurationQuery.java | 45 ++++++++++++++++++++++ .../common/dcae/DcaeConfigurationsCache.java | 35 +++++++++++++++++ .../common/dcae/entity/DcaeConfigurations.java | 1 - .../org/onap/holmes/common/dmaap/DmaapService.java | 8 +++- .../org/onap/holmes/common/dmaap/Publisher.java | 8 ++-- .../common/dcae/DcaeConfigurationsCacheTest.java | 42 ++++++++++++++++++++ .../onap/holmes/common/dmaap/DmaapServiceTest.java | 28 +------------- 8 files changed, 134 insertions(+), 34 deletions(-) create mode 100644 holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationQuery.java create mode 100644 holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationsCache.java create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/dcae/DcaeConfigurationsCacheTest.java diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java index 074c509..fb8a8ca 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java @@ -15,7 +15,6 @@ package org.onap.holmes.common.aai; import java.util.HashMap; import java.util.Map; -import java.util.stream.Stream; import javax.inject.Inject; import lombok.extern.slf4j.Slf4j; import org.jvnet.hk2.annotations.Service; diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationQuery.java new file mode 100644 index 0000000..1576215 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationQuery.java @@ -0,0 +1,45 @@ +/* + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.common.dcae; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.WebTarget; +import org.glassfish.jersey.client.ClientConfig; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.config.MicroServiceConfig; +import org.onap.holmes.common.dcae.entity.DcaeConfigurations; +import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser; +import org.onap.holmes.common.exception.CorrelationException; + +public class DcaeConfigurationQuery { + + public static DcaeConfigurations getDcaeConfigurations(String hostname) + throws CorrelationException { + String serviceAddrInfo = MicroServiceConfig.getServiceAddrInfoFromCBS(hostname); + String response = getDcaeResponse(serviceAddrInfo); + DcaeConfigurations dcaeConfigurations = null; + dcaeConfigurations = DcaeConfigurationParser.parse(response); + return dcaeConfigurations; + } + + private static String getDcaeResponse(String serviceAddrInfo) { + Client client = ClientBuilder.newClient(new ClientConfig()); + WebTarget webTarget = client.target(serviceAddrInfo); + return webTarget.request("application/json").get() + .readEntity(String.class); + } +} diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationsCache.java b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationsCache.java new file mode 100644 index 0000000..2564e23 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationsCache.java @@ -0,0 +1,35 @@ +/* + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.common.dcae; + +import org.onap.holmes.common.dcae.entity.DcaeConfigurations; +import org.onap.holmes.common.dcae.entity.SecurityInfo; + +public class DcaeConfigurationsCache { + + private static DcaeConfigurations dcaeConfigurations; + + public synchronized static SecurityInfo getPubSecInfo(String key) { + if (dcaeConfigurations != null) { + return dcaeConfigurations.getPubSecInfo(key); + } + return null; + } + + public synchronized static void setDcaeConfigurations(DcaeConfigurations configurations) { + dcaeConfigurations = configurations; + } +} diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java index b08261f..f1307c4 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java @@ -32,7 +32,6 @@ public class DcaeConfigurations extends HashMap{ if (null == rule) { return; } - this.rules.add(rule); } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java index 05b3583..64b7dbc 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java @@ -27,6 +27,7 @@ import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData; import org.onap.holmes.common.aai.entity.VmEntity; import org.onap.holmes.common.aai.entity.VnfEntity; import org.onap.holmes.common.api.stat.VesAlarm; +import org.onap.holmes.common.dcae.DcaeConfigurationsCache; import org.onap.holmes.common.dmaap.entity.PolicyMsg; import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS; import org.onap.holmes.common.exception.CorrelationException; @@ -37,15 +38,18 @@ import org.onap.holmes.common.utils.JacksonUtil; public class DmaapService { public static final int POLICY_MESSAGE_ABATED = 1; + + public static final String PUBLISHER_KEY = "unauthenticated.DCAE_CL_OUTPUT"; + @Inject private AaiQuery aaiQuery; - @Inject - private Publisher publisher; public static ConcurrentHashMap loopControlNames = new ConcurrentHashMap<>(); public void publishPolicyMsg(PolicyMsg policyMsg) { try { + Publisher publisher = new Publisher(); + publisher.setUrl(DcaeConfigurationsCache.getPubSecInfo(PUBLISHER_KEY).getDmaapInfo().getTopicUrl()); publisher.publish(policyMsg); log.info("send policyMsg: " + JacksonUtil.beanToJson(policyMsg)); } catch (CorrelationException e) { diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java index 035ecaa..285c3d6 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java @@ -25,6 +25,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import lombok.Getter; import lombok.Setter; +import org.apache.http.HttpStatus; import org.glassfish.jersey.client.ClientConfig; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.dmaap.entity.PolicyMsg; @@ -47,17 +48,16 @@ public class Publisher { try { content = mapper.writeValueAsString(msg); } catch (JsonProcessingException e) { - throw new CorrelationException("Failed to convert the message object to a json string.", e); + throw new CorrelationException("Failed to convert the message object to a json string.", + e); } - WebTarget webTarget = client.target(url); Response response = webTarget.request(MediaType.APPLICATION_JSON) .post(Entity.entity(content, MediaType.APPLICATION_JSON)); - return checkStatus(response); } private boolean checkStatus(Response response) { - return false; + return (response.getStatus() == HttpStatus.SC_OK) ? true : false; } } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dcae/DcaeConfigurationsCacheTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dcae/DcaeConfigurationsCacheTest.java new file mode 100644 index 0000000..cb071d3 --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/dcae/DcaeConfigurationsCacheTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.holmes.common.dcae; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.holmes.common.dcae.entity.DcaeConfigurations; +import org.onap.holmes.common.dcae.entity.SecurityInfo; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@PrepareForTest(DcaeConfigurationsCache.class) +@RunWith(PowerMockRunner.class) +public class DcaeConfigurationsCacheTest { + + @Test + public void testDcaeConfigurationsCache() { + DcaeConfigurations dcaeConfigurations = new DcaeConfigurations(); + SecurityInfo securityInfo = new SecurityInfo(); + securityInfo.setAafUsername("tset11"); + dcaeConfigurations.addPubSecInfo("test", securityInfo); + DcaeConfigurationsCache.setDcaeConfigurations(dcaeConfigurations); + assertThat(DcaeConfigurationsCache.getPubSecInfo("test").getAafUsername(), + equalTo(securityInfo.getAafUsername())); + } +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java index 72a0bc8..15b6b80 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertThat; import java.util.ArrayList; import java.util.List; import org.junit.runner.RunWith; +import org.omg.CORBA.Any; import org.onap.holmes.common.aai.AaiQuery; import org.onap.holmes.common.aai.entity.RelationshipList.Relationship; import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData; @@ -29,6 +30,7 @@ import org.onap.holmes.common.aai.entity.VmEntity; import org.onap.holmes.common.aai.entity.VnfEntity; import org.onap.holmes.common.api.stat.VesAlarm; import org.onap.holmes.common.exception.CorrelationException; +import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.junit.Before; @@ -47,8 +49,6 @@ public class DmaapServiceTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private Publisher publisher; - private AaiQuery aaiQuery; private DmaapService dmaapService; @@ -56,34 +56,10 @@ public class DmaapServiceTest { @Before public void setUp() { dmaapService = new DmaapService(); - publisher = PowerMock.createMock(Publisher.class); - Whitebox.setInternalState(dmaapService, "publisher", publisher); aaiQuery = PowerMock.createMock(AaiQuery.class); Whitebox.setInternalState(dmaapService, "aaiQuery", aaiQuery); } - @Test - public void testDmaapService_publish_ok() throws Exception { - PowerMock.resetAll(); - PolicyMsg policyMsg = new PolicyMsg(); - PowerMock.expectPrivate(publisher, "publish", anyObject(PolicyMsg.class)).andReturn(true) - .anyTimes(); - PowerMock.replayAll(); - Whitebox.invokeMethod(dmaapService, "publishPolicyMsg", policyMsg); - PowerMock.verifyAll(); - } - - @Test - public void testDmaapService_publish_exception() throws Exception { - PowerMock.resetAll(); - final PolicyMsg policyMsg = new PolicyMsg(); - PowerMock.expectPrivate(publisher, "publish", policyMsg) - .andThrow(new CorrelationException("")).anyTimes(); - PowerMock.replayAll(); - Whitebox.invokeMethod(dmaapService, "publishPolicyMsg", policyMsg); - PowerMock.verifyAll(); - } - @Test public void testDmaapService_getDefaultPolicyMsg_ok() throws Exception { PowerMock.resetAll(); -- 2.16.6