Update totalPoliciesCount statistic 80/90180/1
authorJim Hahn <jrh3@att.com>
Wed, 19 Jun 2019 18:24:31 +0000 (14:24 -0400)
committerJim Hahn <jrh3@att.com>
Wed, 19 Jun 2019 18:24:31 +0000 (14:24 -0400)
Modified the code to set the policy count statistic at start-up
and whenever the policy list is changed.
Also fixed a couple of checkstyle issues in guard.

Change-Id: I92017fe64cd5d19c36908347193cc21d183f051e
Issue-ID: POLICY-1844
Signed-off-by: Jim Hahn <jrh3@att.com>
applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java
applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/LegacyGuardTranslator.java
applications/guard/src/test/java/org/onap/policy/xacml/pdp/application/guard/CoordinationTest.java
main/src/main/java/org/onap/policy/pdpx/main/comm/XacmlPdpUpdatePublisher.java
main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManager.java
main/src/main/java/org/onap/policy/pdpx/main/rest/XacmlPdpStatisticsManager.java
main/src/main/java/org/onap/policy/pdpx/main/startstop/XacmlPdpActivator.java
main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java
main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java

index 41c1428..a93e281 100644 (file)
@@ -72,6 +72,7 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
         Map<String, Object> policyProps = toscaPolicy.getProperties();
         LOGGER.debug("path = {}", coordinationFunctionPath);
         LOGGER.debug("props = {}", policyProps);
+        @SuppressWarnings("unchecked")
         List<String> controlLoop = (List<String>) policyProps.get("controlLoop");
         CoordinationDirective cd = new CoordinationDirective();
         cd.setCoordinationFunction(type);
index 2917aab..932db95 100644 (file)
@@ -766,6 +766,7 @@ public class LegacyGuardTranslator implements ToscaPolicyTranslator {
         return theInt;
     }
 
+    @SuppressWarnings("unused")
     private static AdviceExpressionsType generateRequestIdAdvice() {
         AdviceExpressionType adviceExpression = new AdviceExpressionType();
         adviceExpression.setAppliesTo(EffectType.PERMIT);
index 9edf11d..ee99290 100644 (file)
@@ -70,6 +70,7 @@ public class CoordinationTest {
     private static File propertiesFile;
     private static XacmlApplicationServiceProvider service;
     private static DecisionRequest requestCl1Node1;
+    @SuppressWarnings("unused")
     private static DecisionRequest requestCl1Node2;
     private static DecisionRequest requestCl2Node1;
     private static DecisionRequest requestCl2Node2;
index a26f4b1..686a8ed 100644 (file)
@@ -29,6 +29,7 @@ import org.onap.policy.models.pdp.concepts.PdpUpdate;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.pdpx.main.XacmlState;
 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
+import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -80,6 +81,12 @@ public class XacmlPdpUpdatePublisher {
             }
         }
 
+        // update the policy count statistic
+        XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
+        if (stats != null) {
+            stats.setTotalPolicyCount(appManager.getPolicyCount());
+        }
+
         sendPdpUpdate(state.updateInternalState(message));
     }
 
index 6a5555b..52b4e00 100644 (file)
@@ -212,6 +212,15 @@ public class XacmlPdpApplicationManager {
         return types;
     }
 
+    /**
+     * Gets the number of policies currently deployed.
+     *
+     * @return the number of policies currently deployed
+     */
+    public int getPolicyCount() {
+        return mapLoadedPolicies.size();
+    }
+
     private void initializeApplicationPath(Path basePath, XacmlApplicationServiceProvider application)
             throws XacmlApplicationException {
         //
index 6d04305..a696ea4 100644 (file)
@@ -53,12 +53,15 @@ public class XacmlPdpStatisticsManager {
     }
 
     /**
-     * Method to update the xacml pdp total policies count.
+     * Method to set the xacml pdp total policies count. This
+     * doesn't really increment, it depends on the applications
+     * that are loaded. Which can be dynamic.
      *
      * @return the total
      */
-    public long updateTotalPoliciesCount() {
-        return ++totalPoliciesCount;
+    public long setTotalPolicyCount(long newCount) {
+        totalPoliciesCount = newCount;
+        return totalPoliciesCount;
     }
 
     /**
index 5f014a1..8af412d 100644 (file)
@@ -100,6 +100,7 @@ public class XacmlPdpActivator extends ServiceManagerContainer {
             XacmlPdpStatisticsManager stats = new XacmlPdpStatisticsManager();
             XacmlPdpStatisticsManager.setCurrent(stats);
             stats.setTotalPolicyTypesCount(appmgr.getPolicyTypeCount());
+            stats.setTotalPolicyCount(appmgr.getPolicyCount());
 
             state = new XacmlState(appmgr);
 
index ba8f004..7865851 100644 (file)
@@ -44,6 +44,8 @@ public class TestXacmlPdpRestServer extends CommonRest {
     private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
     private static final String STATISTICS_ENDPOINT = "statistics";
 
+    private int nupdates = 0;
+
     @Test
     public void testHealthCheckSuccess() throws Exception {
         LOGGER.info("***************************** Running testHealthCheckSuccess *****************************");
@@ -111,7 +113,9 @@ public class TestXacmlPdpRestServer extends CommonRest {
 
     private void updateXacmlPdpStatistics() {
         XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
-        stats.updateTotalPoliciesCount();
+        ++nupdates;
+        stats.setTotalPolicyCount(nupdates);
+        stats.setTotalPolicyTypesCount(nupdates);
         stats.updatePermitDecisionsCount();
         stats.updateDenyDecisionsCount();
         stats.updateIndeterminantDecisionsCount();
@@ -121,6 +125,7 @@ public class TestXacmlPdpRestServer extends CommonRest {
     private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
         assertEquals(code, report.getCode());
         assertEquals(count, report.getTotalPoliciesCount());
+        assertEquals(count, report.getTotalPolicyTypesCount());
         assertEquals(count, report.getPermitDecisionsCount());
         assertEquals(count, report.getDenyDecisionsCount());
         assertEquals(count, report.getIndeterminantDecisionsCount());
index 250f21e..030b907 100644 (file)
@@ -37,6 +37,8 @@ public class TestXacmlPdpStatistics extends CommonRest {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpStatistics.class);
 
+    private int nupdates = 0;
+
     @Test
     public void testXacmlPdpStatistics_200() throws Exception {
         LOGGER.info("*************************** Running testXacmlPdpStatistics_200 ***************************");
@@ -64,7 +66,9 @@ public class TestXacmlPdpStatistics extends CommonRest {
     private void updateXacmlPdpStatistics() {
         XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
 
-        stats.updateTotalPoliciesCount();
+        ++nupdates;
+        stats.setTotalPolicyCount(nupdates);
+        stats.setTotalPolicyTypesCount(nupdates);
         stats.updatePermitDecisionsCount();
         stats.updateDenyDecisionsCount();
         stats.updateIndeterminantDecisionsCount();
@@ -74,6 +78,7 @@ public class TestXacmlPdpStatistics extends CommonRest {
     private void validateReport(final StatisticsReport report, final int count, final int code) {
         assertEquals(code, report.getCode());
         assertEquals(count, report.getTotalPoliciesCount());
+        assertEquals(count, report.getTotalPolicyTypesCount());
         assertEquals(count, report.getPermitDecisionsCount());
         assertEquals(count, report.getDenyDecisionsCount());
         assertEquals(count, report.getIndeterminantDecisionsCount());