Add dmaap connectivity check in pap 24/123524/4
authorRam Krishna Verma <ram_krishna.verma@bell.ca>
Tue, 24 Aug 2021 21:33:07 +0000 (17:33 -0400)
committerRam Krishna Verma <ram_krishna.verma@bell.ca>
Wed, 25 Aug 2021 13:00:00 +0000 (09:00 -0400)
Add dmaap connectivity check in pap consolidated health check.
Make a rest call to fetch all the topics.
Verify POLICY-PDP-PAP topic is present.

Issue-ID: POLICY-2896
Change-Id: Ide39c70999eaca61205225c95b904e3009d41408
Signed-off-by: Ram Krishna Verma <ram_krishna.verma@bell.ca>
main/src/main/java/org/onap/policy/pap/main/rest/DmaapGetTopicResponse.java [new file with mode: 0644]
main/src/main/java/org/onap/policy/pap/main/rest/PolicyComponentsHealthCheckProvider.java
main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyComponentsHealthCheckProvider.java
main/src/test/resources/parameters/PapConfigParameters.json
main/src/test/resources/parameters/PapConfigParametersStd.json
packages/policy-pap-tarball/src/main/resources/etc/defaultConfig.json

diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/DmaapGetTopicResponse.java b/main/src/main/java/org/onap/policy/pap/main/rest/DmaapGetTopicResponse.java
new file mode 100644 (file)
index 0000000..8288650
--- /dev/null
@@ -0,0 +1,37 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2021 Bell Canada. All rights reserved.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pap.main.rest;
+
+import java.util.List;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+/**
+ * Class to capture get topic response from dmaap.
+ */
+@Getter
+@Setter
+@ToString
+public class DmaapGetTopicResponse {
+
+    private List<String> topics;
+}
index b1e1f5c..2eceb4b 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
- *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
+ *  Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -195,26 +195,32 @@ public class PolicyComponentsHealthCheckProvider {
         HealthCheckReport clientReport;
         try {
             Response resp = httpClient.get();
-            clientReport = replaceIpWithHostname(resp.readEntity(HealthCheckReport.class), httpClient.getBaseUrl());
+            if (httpClient.getName().equalsIgnoreCase("dmaap")) {
+                clientReport = verifyDmaapClient(httpClient, resp);
+            } else {
+                clientReport = replaceIpWithHostname(resp.readEntity(HealthCheckReport.class), httpClient.getBaseUrl());
+            }
 
-            // A health report is read successfully when HTTP status is not OK, it is also not healthy
+            // A health report is read successfully when HTTP status is not OK, it is also
+            // not healthy
             // even in the report it says healthy.
             if (resp.getStatus() != HttpURLConnection.HTTP_OK) {
                 clientReport.setHealthy(false);
             }
         } catch (RuntimeException e) {
             LOGGER.warn("{} connection error", httpClient.getName());
-            clientReport = createUnHealthCheckReport(httpClient.getName(), httpClient.getBaseUrl(),
-                HttpURLConnection.HTTP_INTERNAL_ERROR, e.getMessage());
+            clientReport = createHealthCheckReport(httpClient.getName(), httpClient.getBaseUrl(),
+                            HttpURLConnection.HTTP_INTERNAL_ERROR, false, e.getMessage());
         }
         return clientReport;
     }
 
-    private HealthCheckReport createUnHealthCheckReport(String name, String url, int code, String message) {
+    private HealthCheckReport createHealthCheckReport(String name, String url, int code, boolean status,
+                    String message) {
         var report = new HealthCheckReport();
         report.setName(name);
         report.setUrl(url);
-        report.setHealthy(false);
+        report.setHealthy(status);
         report.setCode(code);
         report.setMessage(message);
         return report;
@@ -229,6 +235,17 @@ public class PolicyComponentsHealthCheckProvider {
         return report;
     }
 
+    private HealthCheckReport verifyDmaapClient(HttpClient httpClient, Response resp) {
+        DmaapGetTopicResponse dmaapResponse = resp.readEntity(DmaapGetTopicResponse.class);
+        var topicVerificationStatus = (dmaapResponse.getTopics() != null
+                        && dmaapResponse.getTopics().contains(PapConstants.TOPIC_POLICY_PDP_PAP));
+        String message = (topicVerificationStatus ? "PAP to DMaaP connection check is successfull"
+                        : "PAP to DMaaP connection check failed");
+        int code = (topicVerificationStatus ? resp.getStatus() : 503);
+        return createHealthCheckReport(httpClient.getName(), httpClient.getBaseUrl(), code,
+                        topicVerificationStatus, message);
+    }
+
     /**
      * This method clears clients {@link List} and clientHealthCheckExecutorService {@link ExecutorService}.
      */
index 3f4ecd3..f27a8d3 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Nordix Foundation.
  *  Modifications Copyright (C) 2020-2021 AT&T Corp.
- *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
+ *  Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.when;
 
 import java.io.File;
 import java.net.HttpURLConnection;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import javax.ws.rs.core.Response;
@@ -88,12 +89,18 @@ public class TestPolicyComponentsHealthCheckProvider {
     @Mock
     private HttpClient client2;
 
+    @Mock
+    private HttpClient client3;
+
     @Mock
     private Response response1;
 
     @Mock
     private Response response2;
 
+    @Mock
+    private Response response3;
+
     private List<PdpGroup> groups;
 
     private PapParameterGroup savedPapParameterGroup;
@@ -133,10 +140,17 @@ public class TestPolicyComponentsHealthCheckProvider {
         when(response2.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
         when(client2.get()).thenReturn(response2);
 
+        when(client3.getName()).thenReturn("dmaap");
+        when(client3.getBaseUrl()).thenReturn("message-router");
+        when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
+        when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(createDmaapResponse());
+        when(client3.get()).thenReturn(response3);
+
         PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
         List<RestClientParameters> params = papParameterGroup.getHealthCheckRestClientParameters();
         when(clientFactory.build(params.get(0))).thenReturn(client1);
         when(clientFactory.build(params.get(1))).thenReturn(client2);
+        when(clientFactory.build(params.get(2))).thenReturn(client3);
 
         PolicyComponentsHealthCheckProvider.initializeClientHealthCheckExecutorService(papParameterGroup,
             clientFactory);
@@ -180,6 +194,20 @@ public class TestPolicyComponentsHealthCheckProvider {
         assertFalse((Boolean) result2.get(HEALTHY));
         HealthCheckReport report2 = (HealthCheckReport) result.get(CLIENT_1);
         assertFalse(report2.isHealthy());
+
+        when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
+        when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(null);
+        Map<String, Object> result3 = callFetchPolicyComponentsHealthStatus();
+        assertFalse((Boolean) result3.get(HEALTHY));
+        HealthCheckReport report3 = (HealthCheckReport) result3.get("dmaap");
+        assertFalse(report3.isHealthy());
+
+        when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
+        when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(new DmaapGetTopicResponse());
+        Map<String, Object> result4 = callFetchPolicyComponentsHealthStatus();
+        assertFalse((Boolean) result4.get(HEALTHY));
+        HealthCheckReport report4 = (HealthCheckReport) result4.get("dmaap");
+        assertFalse(report4.isHealthy());
     }
 
     @SuppressWarnings("unchecked")
@@ -234,4 +262,10 @@ public class TestPolicyComponentsHealthCheckProvider {
             throw new RuntimeException(e);
         }
     }
+
+    private DmaapGetTopicResponse createDmaapResponse() {
+        DmaapGetTopicResponse response = new DmaapGetTopicResponse();
+        response.setTopics(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP));
+        return response;
+    }
 }
index 09adfce..4f59d69 100644 (file)
         "password": "zb!XztG34",
         "useHttps": true,
         "basePath": "healthcheck"
+    },
+    {
+        "clientName": "dmaap",
+        "hostname": "message-router",
+        "port": 3905,
+        "useHttps": true,
+        "basePath": "topics"
     }]
 }
index 0f88143..56e891a 100644 (file)
         "password": "zb!XztG34",
         "useHttps": true,
         "basePath": "healthcheck"
+    },
+    {
+        "clientName": "dmaap",
+        "hostname": "message-router",
+        "port": 3905,
+        "useHttps": true,
+        "basePath": "topics"
     }]
 }
index 575880c..573ecc9 100644 (file)
         "password": "zb!XztG34",
         "useHttps": true,
         "basePath": "healthcheck"
+    },
+    {
+        "clientName": "dmaap",
+        "hostname": "message-router",
+        "port": 3905,
+        "useHttps": true,
+        "basePath": "topics"
     }]
 }