Add ability to turn on/off pdp statistics

Adding an ability in pap to turn on/off processing of pdp statistics.
By default it will be turned on and can be changed via configuration.

Issue-ID: POLICY-3450
Change-Id: I093fe3f1390769f9acf2cfe85dd0d4edf0539de8
Signed-off-by: Ram Krishna Verma <ram_krishna.verma@bell.ca>
(cherry picked from commit 57e39b87ac53c128502110b64b06947e9e380d4e)
Signed-off-by: Ram Krishna Verma <ram_krishna.verma@bell.ca>
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/PdpHeartbeatListener.java b/main/src/main/java/org/onap/policy/pap/main/comm/PdpHeartbeatListener.java
index 512609a..aaca9dc 100644
--- a/main/src/main/java/org/onap/policy/pap/main/comm/PdpHeartbeatListener.java
+++ b/main/src/main/java/org/onap/policy/pap/main/comm/PdpHeartbeatListener.java
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
  *  Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ *  Modifications 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.
@@ -35,19 +36,22 @@
 
     private final PdpParameters params;
 
+    private final boolean savePdpStatistics;
+
     /**
      * Constructs the object.
      *
      * @param params PDP parameters
      */
-    public PdpHeartbeatListener(PdpParameters params) {
+    public PdpHeartbeatListener(PdpParameters params, boolean savePdpStatistics) {
         this.params = params;
+        this.savePdpStatistics = savePdpStatistics;
     }
 
     @Override
     public void onTopicEvent(final CommInfrastructure infra, final String topic, final PdpStatus message) {
 
-        final PdpStatusMessageHandler handler = new PdpStatusMessageHandler(params);
+        final PdpStatusMessageHandler handler = new PdpStatusMessageHandler(params, savePdpStatistics);
         handler.handlePdpStatus(message);
     }
 }
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java b/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
index 5ec9aba..bf5fc12 100644
--- a/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
+++ b/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
@@ -62,6 +62,8 @@
 
     private final PdpParameters params;
 
+    private final boolean savePdpStatistics;
+
     /**
      * List to store policies present in db.
      */
@@ -82,9 +84,10 @@
      *
      * @param params PDP parameters
      */
-    public PdpStatusMessageHandler(PdpParameters params) {
+    public PdpStatusMessageHandler(PdpParameters params, boolean savePdpStatistics) {
         super(true);
         this.params = params;
+        this.savePdpStatistics = savePdpStatistics;
     }
 
     /**
@@ -249,12 +252,10 @@
             LOGGER.debug("PdpInstance details are correct. Saving current state in DB - {}", pdpInstance);
             updatePdpHealthStatus(message, pdpSubGroup, pdpInstance, pdpGroup, databaseProvider);
 
-            if (validatePdpStatisticsDetails(message, pdpInstance, pdpGroup, pdpSubGroup)) {
-                LOGGER.debug("PdpStatistics details are correct. Saving current statistics in DB - {}",
-                        message.getStatistics());
-                createPdpStatistics(message.getStatistics(), databaseProvider);
+            if (savePdpStatistics) {
+                processPdpStatistics(message, pdpSubGroup, pdpInstance, pdpGroup, databaseProvider);
             } else {
-                LOGGER.debug("PdpStatistics details are not correct - {}", message.getStatistics());
+                LOGGER.debug("Not processing PdpStatistics - {}", message.getStatistics());
             }
         } else {
             LOGGER.debug("PdpInstance details are not correct. Sending PdpUpdate message - {}", pdpInstance);
@@ -265,6 +266,17 @@
         }
     }
 
+    private void processPdpStatistics(final PdpStatus message, final PdpSubGroup pdpSubGroup, final Pdp pdpInstance,
+                    final PdpGroup pdpGroup, final PolicyModelsProvider databaseProvider) throws PfModelException {
+        if (validatePdpStatisticsDetails(message, pdpInstance, pdpGroup, pdpSubGroup)) {
+            LOGGER.debug("PdpStatistics details are correct. Saving current statistics in DB - {}",
+                    message.getStatistics());
+            createPdpStatistics(message.getStatistics(), databaseProvider);
+        } else {
+            LOGGER.debug("PdpStatistics details are not correct - {}", message.getStatistics());
+        }
+    }
+
     private void processPdpTermination(final PdpSubGroup pdpSubGroup, final Pdp pdpInstance, final PdpGroup pdpGroup,
             final PolicyModelsProvider databaseProvider) throws PfModelException {
         pdpSubGroup.getPdpInstances().remove(pdpInstance);
diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java
index 9a50cf0..2b5e986 100644
--- a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java
+++ b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
+ *  Modifications 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.
@@ -43,6 +44,7 @@
     private RestServerParameters restServerParameters;
     private PdpParameters pdpParameters;
     private PolicyModelsProviderParameters databaseProviderParameters;
+    private boolean savePdpStatisticsInDb;
     private TopicParameterGroup topicParameterGroup;
     // API, Distribution Health Check restClient parameters.
     private List<BusTopicParams> healthCheckRestClientParameters;
diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java
index 4272e49..7b5c2f0 100644
--- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java
+++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
+ *  Modifications 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.
@@ -111,7 +112,8 @@
             this.papParameterGroup = papParameterGroup;
             this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES);
             this.reqIdDispatcher = new RequestIdDispatcher<>(PdpStatus.class, REQ_ID_NAMES);
-            this.pdpHeartbeatListener = new PdpHeartbeatListener(papParameterGroup.getPdpParameters());
+            this.pdpHeartbeatListener = new PdpHeartbeatListener(papParameterGroup.getPdpParameters(),
+                            papParameterGroup.isSavePdpStatisticsInDb());
 
         } catch (final RuntimeException e) {
             throw new PolicyPapRuntimeException(e);
diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/PdpHeartbeatListenerTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/PdpHeartbeatListenerTest.java
index d2dc195..ce34993 100644
--- a/main/src/test/java/org/onap/policy/pap/main/comm/PdpHeartbeatListenerTest.java
+++ b/main/src/test/java/org/onap/policy/pap/main/comm/PdpHeartbeatListenerTest.java
@@ -2,6 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2021 Nordix Foundation.
  *  Modifications Copyright (C) 2020 AT&T Intellectual Property.
+ *  Modifications 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.
@@ -26,11 +27,9 @@
 import static org.junit.Assert.assertSame;
 
 import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.time.Instant;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Date;
 import java.util.List;
 import java.util.stream.Collectors;
 import org.junit.Test;
@@ -71,7 +70,7 @@
     @Test
     public void testPdpHeartbeatListener() throws CoderException, PfModelException {
         addGroups("PdpGroups.json");
-        pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters());
+        pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters(), true);
 
         // Testing pdp registration success case
         final PdpStatus status1 = new PdpStatus();
@@ -224,7 +223,7 @@
         PdpParameters params = testData.getPapParameterGroup(1).getPdpParameters();
         List<ToscaConceptIdentifier> polsUndep = policies.stream().map(ToscaPolicy::getIdentifier)
                 .collect(Collectors.toList());
-        PdpStatusMessageHandler handler = new PdpStatusMessageHandler(params);
+        PdpStatusMessageHandler handler = new PdpStatusMessageHandler(params, true);
         PdpUpdate update10 = handler.createPdpUpdateMessage(
                 status3.getPdpGroup(), new PdpSubGroup(), "pdp_2",
                 null, policies, policies, polsUndep);
@@ -237,7 +236,7 @@
     @Test
     public void testPdpStatistics() throws CoderException, PfModelException, ParseException {
         addGroups("PdpGroups.json");
-        pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters());
+        pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters(), true);
         timeStamp = Instant.parse("2021-02-12T17:48:01.029211400Z");
 
         // init default pdp group
@@ -348,6 +347,30 @@
 
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status5);
         verifyPdpStatistics(null, DEFAULT_GROUP, null, 1);
+
+        // Test pdp statistics save disabled case, sending valid pdp status but count should still remain 1
+        pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters(), false);
+        timeStamp = Instant.parse("2021-02-12T17:48:05.029211400Z");
+        final PdpStatus status7 = new PdpStatus();
+        status7.setName(PDP_NAME);
+        status7.setState(PdpState.ACTIVE);
+        status7.setPdpGroup(DEFAULT_GROUP);
+        status7.setPdpType(APEX_TYPE);
+        status7.setHealthy(PdpHealthStatus.HEALTHY);
+        status7.setPdpSubgroup(APEX_TYPE);
+        final List<ToscaConceptIdentifier> idents7 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
+        status7.setPolicies(idents7);
+
+        PdpStatistics pdpStatistics07 = new PdpStatistics();
+        pdpStatistics07.setPdpInstanceId(PDP_NAME);
+        pdpStatistics07.setPdpGroupName(DEFAULT_GROUP);
+        pdpStatistics07.setPdpSubGroupName(APEX_TYPE);
+        pdpStatistics07.setTimeStamp(timeStamp);
+        status7.setStatistics(pdpStatistics07);
+        pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status7);
+        verifyPdpStatistics(PDP_NAME, DEFAULT_GROUP, null, 1);
+
     }
 
     private void verifyPdpGroup(final String name, final int count) throws PfModelException {
diff --git a/main/src/test/resources/parameters/MinimumParameters.json b/main/src/test/resources/parameters/MinimumParameters.json
index 69492e9..e479adb 100644
--- a/main/src/test/resources/parameters/MinimumParameters.json
+++ b/main/src/test/resources/parameters/MinimumParameters.json
@@ -26,6 +26,7 @@
         "databasePassword": "P01icY",
         "persistenceUnit": "PdpGroupTest"
     },
+    "savePdpStatisticsInDb": true,
     "topicParameterGroup": {
         "topicSources" : [{
             "topic" : "POLICY-PDP-PAP",
diff --git a/main/src/test/resources/parameters/PapConfigParameters.json b/main/src/test/resources/parameters/PapConfigParameters.json
index 111a635..54d72e0 100644
--- a/main/src/test/resources/parameters/PapConfigParameters.json
+++ b/main/src/test/resources/parameters/PapConfigParameters.json
@@ -27,6 +27,7 @@
         "databasePassword": "UDAxaWNZ",
         "persistenceUnit": "ToscaConceptTest"
     },
+    "savePdpStatisticsInDb": true,
     "topicParameterGroup": {
         "topicSources" : [{
             "topic" : "POLICY-PDP-PAP",
diff --git a/main/src/test/resources/parameters/PapConfigParametersStd.json b/main/src/test/resources/parameters/PapConfigParametersStd.json
index a11782b..0541326 100644
--- a/main/src/test/resources/parameters/PapConfigParametersStd.json
+++ b/main/src/test/resources/parameters/PapConfigParametersStd.json
@@ -28,6 +28,7 @@
         "databasePassword": "UDAxaWNZ",
         "persistenceUnit": "ToscaConceptTest"
     },
+    "savePdpStatisticsInDb": true,
     "topicParameterGroup": {
         "topicSources" : [{
             "topic" : "POLICY-PDP-PAP",
diff --git a/packages/policy-pap-tarball/src/main/resources/etc/defaultConfig.json b/packages/policy-pap-tarball/src/main/resources/etc/defaultConfig.json
index f0bee8a..2f37cb6 100644
--- a/packages/policy-pap-tarball/src/main/resources/etc/defaultConfig.json
+++ b/packages/policy-pap-tarball/src/main/resources/etc/defaultConfig.json
@@ -28,6 +28,7 @@
         "databasePassword": "policy_user",
         "persistenceUnit": "PolicyMariaDb"
     },
+    "savePdpStatisticsInDb": true,
     "topicParameterGroup": {
         "topicSources" : [{
             "topic" : "POLICY-PDP-PAP",
diff --git a/packages/policy-pap-tarball/src/main/resources/etc/s3pConfig.json b/packages/policy-pap-tarball/src/main/resources/etc/s3pConfig.json
deleted file mode 100644
index cc06793..0000000
--- a/packages/policy-pap-tarball/src/main/resources/etc/s3pConfig.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
-    "name": "PapGroup",
-    "restServerParameters": {
-        "host": "0.0.0.0",
-        "port": 6969,
-        "userName": "healthcheck",
-        "password": "zb!XztG34",
-        "https": true,
-        "aaf": false
-    },
-    "topicParameterGroup": {
-        "topicSources" : [{
-            "topic" : "POLICY-PDP-PAP",
-            "servers" : [ "message-router" ],
-            "topicCommInfrastructure" : "dmaap"
-        }],
-        "topicSinks" : [{
-            "topic" : "POLICY-PDP-PAP",
-            "servers" : [ "message-router" ],
-            "topicCommInfrastructure" : "dmaap"
-        }]
-    }
-}
\ No newline at end of file