modify not publish messages to DMaaP 05/18705/5
authorShiwei Tian <tian.shiwei@zte.com.cn>
Fri, 13 Oct 2017 06:11:57 +0000 (14:11 +0800)
committerShiwei Tian <tian.shiwei@zte.com.cn>
Fri, 13 Oct 2017 12:54:25 +0000 (20:54 +0800)
Issue-ID: HOLMES-71

Change-Id: I96097090b5d2ba2ab611ccb326e4670eba39cea7
Signed-off-by: Shiwei Tian <tian.shiwei@zte.com.cn>
holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java
holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationQuery.java [new file with mode: 0644]
holmes-actions/src/main/java/org/onap/holmes/common/dcae/DcaeConfigurationsCache.java [new file with mode: 0644]
holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java
holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java
holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java
holmes-actions/src/test/java/org/onap/holmes/common/dcae/DcaeConfigurationsCacheTest.java [new file with mode: 0644]
holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java

index 074c509..fb8a8ca 100644 (file)
@@ -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 (file)
index 0000000..1576215
--- /dev/null
@@ -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 (file)
index 0000000..2564e23
--- /dev/null
@@ -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;
+    }
+}
index b08261f..f1307c4 100644 (file)
@@ -32,7 +32,6 @@ public class DcaeConfigurations extends HashMap<String, Object>{
         if (null == rule) {\r
             return;\r
         }\r
-\r
         this.rules.add(rule);\r
     }\r
 \r
index 05b3583..64b7dbc 100644 (file)
@@ -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<String, String> 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) {
index 035ecaa..285c3d6 100644 (file)
@@ -25,6 +25,7 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;\r
 import lombok.Getter;\r
 import lombok.Setter;\r
+import org.apache.http.HttpStatus;\r
 import org.glassfish.jersey.client.ClientConfig;\r
 import org.jvnet.hk2.annotations.Service;\r
 import org.onap.holmes.common.dmaap.entity.PolicyMsg;\r
@@ -47,17 +48,16 @@ public class Publisher {
         try {\r
             content = mapper.writeValueAsString(msg);\r
         } catch (JsonProcessingException e) {\r
-            throw new CorrelationException("Failed to convert the message object to a json string.", e);\r
+            throw new CorrelationException("Failed to convert the message object to a json string.",\r
+                    e);\r
         }\r
-\r
         WebTarget webTarget = client.target(url);\r
         Response response = webTarget.request(MediaType.APPLICATION_JSON)\r
                 .post(Entity.entity(content, MediaType.APPLICATION_JSON));\r
-\r
         return checkStatus(response);\r
     }\r
 \r
     private boolean checkStatus(Response response) {\r
-        return false;\r
+        return (response.getStatus() == HttpStatus.SC_OK) ? true : false;\r
     }\r
 }\r
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 (file)
index 0000000..cb071d3
--- /dev/null
@@ -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
index 72a0bc8..15b6b80 100644 (file)
@@ -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();