Fail component health check if PDP is down 12/111012/2
authorRam Krishna Verma <ram_krishna.verma@bell.ca>
Thu, 6 Aug 2020 22:06:17 +0000 (18:06 -0400)
committerRam Krishna Verma <ram_krishna.verma@bell.ca>
Fri, 7 Aug 2020 13:15:53 +0000 (09:15 -0400)
Changing the policy component health check to fail if the required
PDP instances are down or not registered with PAP.
The check is performed by comparing the "currentInstanceCount"
& "desiredInstanceCount" fields of PdpSubGroup.

Issue-ID: POLICY-2718
Change-Id: Iad1075d0812f4ef166dfed4f110eff4d9e5f995e
Signed-off-by: Ram Krishna Verma <ram_krishna.verma@bell.ca>
main/src/main/java/org/onap/policy/pap/main/rest/PolicyComponentsHealthCheckProvider.java
main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupHealthCheckProvider.java
main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyComponentsHealthCheckProvider.java
main/src/test/resources/rest/pdpGroup.json

index 36f25a0..3ce8aa6 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 2020 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.
@@ -141,9 +142,10 @@ public class PolicyComponentsHealthCheckProvider {
 
         // Check PDPs, read status from DB
         try {
-            Map<String, List<Pdp>> pdpListWithType = fetchPdpsHealthStatus();
-            if (isHealthy && (pdpListWithType.isEmpty() || pdpListWithType.values().stream().flatMap(List::stream)
-                .anyMatch(pdp -> !PdpHealthStatus.HEALTHY.equals(pdp.getHealthy())))) {
+            List<PdpGroup> groups = fetchPdpGroups();
+            Map<String, List<Pdp>> pdpListWithType = fetchPdpsHealthStatus(groups);
+            if (isHealthy && (!verifyNumberOfPdps(groups) || pdpListWithType.values().stream().flatMap(List::stream)
+                            .anyMatch(pdp -> !PdpHealthStatus.HEALTHY.equals(pdp.getHealthy())))) {
                 isHealthy = false;
             }
             result.put(PapConstants.POLICY_PDPS, pdpListWithType);
@@ -157,22 +159,40 @@ public class PolicyComponentsHealthCheckProvider {
         return Pair.of(Status.OK, result);
     }
 
-    private Map<String, List<Pdp>> fetchPdpsHealthStatus() throws PfModelException {
+    private Map<String, List<Pdp>> fetchPdpsHealthStatus(List<PdpGroup> groups) {
         Map<String, List<Pdp>> pdpListWithType = new HashMap<>();
-        final PolicyModelsProviderFactoryWrapper modelProviderWrapper = Registry
-            .get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
-        try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
-            final List<PdpGroup> groups = databaseProvider.getPdpGroups(null);
-            for (final PdpGroup group : groups) {
-                for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
-                    List<Pdp> pdpList = new ArrayList<>(subGroup.getPdpInstances());
-                    pdpListWithType.computeIfAbsent(subGroup.getPdpType(), k -> new ArrayList<>()).addAll(pdpList);
-                }
+        for (final PdpGroup group : groups) {
+            for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
+                List<Pdp> pdpList = new ArrayList<>(subGroup.getPdpInstances());
+                pdpListWithType.computeIfAbsent(subGroup.getPdpType(), k -> new ArrayList<>()).addAll(pdpList);
             }
         }
         return pdpListWithType;
     }
 
+    private boolean verifyNumberOfPdps(List<PdpGroup> groups) {
+        boolean flag = true;
+        for (final PdpGroup group : groups) {
+            for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
+                if (subGroup.getCurrentInstanceCount() < subGroup.getDesiredInstanceCount()) {
+                    flag = false;
+                    break;
+                }
+            }
+        }
+        return flag;
+    }
+
+    private List<PdpGroup> fetchPdpGroups() throws PfModelException {
+        List<PdpGroup> groups = new ArrayList<>();
+        final PolicyModelsProviderFactoryWrapper modelProviderWrapper =
+                        Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
+        try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
+            groups = databaseProvider.getPdpGroups(null);
+        }
+        return groups;
+    }
+
     private HealthCheckReport fetchPolicyComponentHealthStatus(HttpClient httpClient) {
         HealthCheckReport clientReport;
         try {
index c910a98..0babb71 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2020 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.
@@ -88,7 +89,7 @@ public class TestPdpGroupHealthCheckProvider {
     }
 
     private void verifyPdps(final List<Pdp> pdpList, final List<PdpGroup> groups) {
-        assertEquals(5, pdpList.size());
+        assertEquals(6, pdpList.size());
         for (final PdpGroup group : groups) {
             for (final PdpSubGroup subGroup : group.getPdpSubgroups()) {
                 pdpList.containsAll(subGroup.getPdpInstances());
index bdf4700..ff2159d 100644 (file)
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2020 Nordix Foundation.
  *  Modifications Copyright (C) 2020 AT&T Corp.
+ *  Modifications Copyright (C) 2020 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.
@@ -192,6 +193,14 @@ public class TestPolicyComponentsHealthCheckProvider {
         assertFalse((Boolean) result.get(HEALTHY));
     }
 
+    @Test
+    public void testFetchPolicyComponentsHealthStatus_PdpDown() {
+        // Set currentInstanceCount as 0 to simulate PDP down
+        groups.get(0).getPdpSubgroups().get(0).setCurrentInstanceCount(0);
+        Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
+        assertFalse((Boolean) result.get(HEALTHY));
+    }
+
     @Test
     public void testFetchPolicyComponentsHealthStatus_unhealthyPap() {
         when(papActivator.isAlive()).thenReturn(false);
index 2a7b4ed..279d807 100644 (file)
                             "instanceId": "pdpAA_2",
                             "pdpState": "PASSIVE",
                             "healthy": "HEALTHY"
+                        },
+                        {
+                            "instanceId": "pdpAA_3",
+                            "pdpState": "PASSIVE",
+                            "healthy": "HEALTHY"
                         }
                     ],
                     "supportedPolicyTypes": [
@@ -42,6 +47,7 @@
                 },
                 {
                     "pdpType": "pdpTypeB",
+                    "currentInstanceCount": 1,
                     "desiredInstanceCount": 1,
                     "pdpInstances": [
                         {
@@ -66,6 +72,7 @@
             "pdpSubgroups": [
                 {
                     "pdpType": "pdpTypeA",
+                    "currentInstanceCount": 2,
                     "desiredInstanceCount": 2,
                     "pdpInstances": [
                         {