From b2bb847016d780377ea4ff26b389885e9295e7ff Mon Sep 17 00:00:00 2001 From: shaoqiu Date: Fri, 13 Dec 2019 15:10:29 +0800 Subject: Populate the PdpStatistics data in heartbeat Apex to populate the PdpStatistics data in every heartbeat sent to PAP Issue-ID: POLICY-2302 Change-Id: I2d3a6c1e8605d53bf754a135980527bd50762abd Signed-off-by: shaoqiu --- .../apex/services/onappf/ApexStarterMain.java | 7 +- .../services/onappf/handler/ApexEngineHandler.java | 14 +++- .../services/onappf/handler/PdpMessageHandler.java | 88 +++++++++++++++++++++- .../handler/PdpStateChangeMessageHandler.java | 9 ++- .../onappf/handler/PdpUpdateMessageHandler.java | 10 ++- .../apex/services/onappf/TestApexStarterMain.java | 6 +- .../onappf/comm/TestPdpStateChangeListener.java | 16 +++- 7 files changed, 140 insertions(+), 10 deletions(-) (limited to 'services/services-onappf') diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java index 2a3031a4a..4a5fb360e 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java @@ -1,8 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. 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. @@ -23,6 +22,7 @@ package org.onap.policy.apex.services.onappf; import java.util.Arrays; +import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; @@ -79,6 +79,7 @@ public class ApexStarterMain { // create the activator activator = new ApexStarterActivator(parameterGroup); Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator); + Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, new ApexPolicyStatisticsManager()); // Start the activator try { activator.initialize(); @@ -117,6 +118,8 @@ public class ApexStarterMain { if (activator != null && activator.isAlive()) { activator.terminate(); } + + Registry.unregister(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER); } /** diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java index 6a5bb17ff..8150ff9c5 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * 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 java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel; import org.onap.policy.apex.service.engine.main.ApexMain; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.common.utils.coder.CoderException; @@ -129,6 +130,17 @@ public class ApexEngineHandler { } } + /** + * Method to get the APEX engine statistics. + */ + public List getEngineStats() { + List engineStats = null; + if (null != apexMain && apexMain.isAlive()) { + engineStats = apexMain.getEngineStats(); + } + return engineStats; + } + /** * Method to check whether the apex engine is running or not. */ diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java index 697f3d47f..e82e34755 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,19 +21,30 @@ package org.onap.policy.apex.services.onappf.handler; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import lombok.NonNull; +import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel; +import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState; +import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.onap.policy.apex.services.onappf.parameters.PdpStatusParameters; import org.onap.policy.apex.services.onappf.parameters.ToscaPolicyTypeIdentifierParameters; import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics; import org.onap.policy.models.pdp.concepts.PdpResponseDetails; +import org.onap.policy.models.pdp.concepts.PdpStatistics; import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.models.pdp.enums.PdpEngineWorkerState; import org.onap.policy.models.pdp.enums.PdpHealthStatus; import org.onap.policy.models.pdp.enums.PdpResponseStatus; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * This class supports the handling of pdp messages. @@ -41,6 +52,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifi * @author Ajith Sreekumar (ajith.sreekumar@est.tech) */ public class PdpMessageHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(PdpMessageHandler.class); /** * Method to create PdpStatus message from the parameters which will be saved to the context. @@ -96,9 +108,83 @@ public class PdpMessageHandler { pdpStatus.setPolicies(pdpStatusContext.getPolicies()); pdpStatus.setPdpGroup(pdpStatusContext.getPdpGroup()); pdpStatus.setPdpSubgroup(pdpStatusContext.getPdpSubgroup()); + + ApexEngineHandler apexEngineHandler = null; + try { + apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER); + } catch (IllegalArgumentException e) { + LOGGER.warn(e.getMessage()); + } + + pdpStatus.setStatistics(getStatistics(pdpStatus, apexEngineHandler)); + + return pdpStatus; } + /** + * Method to get the statistics. + * + * @return PdpStatistics the pdp status message + */ + + private PdpStatistics getStatistics(final PdpStatus pdpStatusContext, final ApexEngineHandler apexEngineHandler) { + PdpStatistics pdpStatistics = new PdpStatistics(); + pdpStatistics.setPdpInstanceId(pdpStatusContext.getName()); + pdpStatistics.setTimeStamp(new Date()); + pdpStatistics.setPdpGroupName(pdpStatusContext.getPdpGroup()); + pdpStatistics.setPdpSubGroupName(pdpStatusContext.getPdpSubgroup()); + if (apexEngineHandler != null) { + pdpStatistics.setEngineStats(getEngineWorkerStats(apexEngineHandler)); + } + final ApexPolicyStatisticsManager apexPolicyCounter = ApexPolicyStatisticsManager.getInstanceFromRegistry(); + if (apexPolicyCounter != null) { + pdpStatistics.setPolicyDeploySuccessCount(apexPolicyCounter.getPolicyDeploySuccessCount()); + pdpStatistics.setPolicyDeployFailCount(apexPolicyCounter.getPolicyDeployFailCount()); + pdpStatistics.setPolicyDeployCount(apexPolicyCounter.getPolicyDeployCount()); + pdpStatistics.setPolicyExecutedCount(apexPolicyCounter.getPolicyExecutedCount()); + pdpStatistics.setPolicyExecutedSuccessCount(apexPolicyCounter.getPolicyExecutedSuccessCount()); + pdpStatistics.setPolicyExecutedFailCount(apexPolicyCounter.getPolicyExecutedFailCount()); + } + return pdpStatistics; + } + + private List getEngineWorkerStats(@NonNull final ApexEngineHandler apexEngineHandler) { + List pdpEngineWorkerStats = new ArrayList<>(); + List engineModels = apexEngineHandler.getEngineStats(); + if (engineModels != null) { + engineModels.forEach(engineModel -> { + PdpEngineWorkerStatistics workerStatistics = new PdpEngineWorkerStatistics(); + workerStatistics.setEngineWorkerState(transferEngineState(engineModel.getState())); + workerStatistics.setEngineId(engineModel.getId()); + workerStatistics.setEventCount(engineModel.getStats().getEventCount()); + workerStatistics.setAverageExecutionTime(engineModel.getStats().getAverageExecutionTime()); + workerStatistics.setEngineTimeStamp(engineModel.getStats().getTimeStamp()); + workerStatistics.setLastEnterTime(engineModel.getStats().getLastEnterTime()); + workerStatistics.setLastExecutionTime(engineModel.getStats().getLastExecutionTime()); + workerStatistics.setLastStart(engineModel.getStats().getLastStart()); + workerStatistics.setUpTime(engineModel.getStats().getUpTime()); + pdpEngineWorkerStats.add(workerStatistics); + }); + } + return pdpEngineWorkerStats; + } + + private PdpEngineWorkerState transferEngineState(@NonNull final AxEngineState state) { + switch (state) { + case STOPPING: + return PdpEngineWorkerState.STOPPING; + case STOPPED: + return PdpEngineWorkerState.STOPPED; + case READY: + return PdpEngineWorkerState.READY; + case EXECUTING: + return PdpEngineWorkerState.EXECUTING; + default: + return PdpEngineWorkerState.UNDEFINED; + } + } + /** * Method to get a final pdp status when the apex started is shutting down. * diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java index fd95b47b7..46d04f63f 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ package org.onap.policy.apex.services.onappf.handler; import java.util.HashSet; import java.util.List; +import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; @@ -145,6 +146,12 @@ public class PdpStateChangeMessageHandler { pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage()); } + final ApexPolicyStatisticsManager apexPolicyStatisticsManager = + ApexPolicyStatisticsManager.getInstanceFromRegistry(); + if (apexPolicyStatisticsManager != null) { + apexPolicyStatisticsManager + .updatePolicyDeployCounter(pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS); + } return pdpResponseDetails; } diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java index 33ac81f5d..aa5a6457e 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ package org.onap.policy.apex.services.onappf.handler; import java.util.Collections; import java.util.HashSet; import java.util.List; +import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; @@ -160,6 +161,7 @@ public class PdpUpdateMessageHandler { private PdpResponseDetails startApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg, final PdpMessageHandler pdpMessageHandler, ApexEngineHandler apexEngineHandler) { PdpResponseDetails pdpResponseDetails = null; + try { if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) { apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPolicies()); @@ -192,6 +194,12 @@ public class PdpUpdateMessageHandler { pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage()); } + final ApexPolicyStatisticsManager apexPolicyStatisticsManager = + ApexPolicyStatisticsManager.getInstanceFromRegistry(); + if (apexPolicyStatisticsManager != null) { + apexPolicyStatisticsManager + .updatePolicyDeployCounter(pdpResponseDetails.getResponseStatus() == PdpResponseStatus.SUCCESS); + } return pdpResponseDetails; } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java index 2fcfe886f..43d36f84d 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import static org.junit.Assert.assertTrue; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.apex.services.onappf.parameters.CommonTestData; import org.onap.policy.common.utils.services.Registry; @@ -71,7 +72,8 @@ public class TestApexStarterMain { // ensure items were added to the registry assertNotNull(Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class)); - + assertNotNull(Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, + ApexPolicyStatisticsManager.class)); apexStarter.shutdown(); } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java index 7f7de3b42..17f909fcc 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java @@ -1,8 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. 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. @@ -23,6 +22,7 @@ package org.onap.policy.apex.services.onappf.comm; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; @@ -35,6 +35,7 @@ import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.apex.services.onappf.ApexStarterActivator; import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; import org.onap.policy.apex.services.onappf.ApexStarterConstants; @@ -94,6 +95,7 @@ public class TestPdpStateChangeListener { activator = new ApexStarterActivator(parameterGroup); Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator); + Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, new ApexPolicyStatisticsManager()); activator.initialize(); } @@ -161,6 +163,16 @@ public class TestPdpStateChangeListener { pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); assertTrue(outContent.toString().contains("Apex engine started and policies are running.")); assertEquals(PdpState.ACTIVE, pdpStatus.getState()); + + final ApexPolicyStatisticsManager policyCounterManager = ApexPolicyStatisticsManager.getInstanceFromRegistry(); + assertNotNull(policyCounterManager); + assertEquals(policyCounterManager.getPolicyDeployCount(), + policyCounterManager.getPolicyDeploySuccessCount() + policyCounterManager.getPolicyDeployFailCount()); + + apexEngineHandler = + Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null); + assertNotNull(apexEngineHandler); + assertTrue(apexEngineHandler.getEngineStats().size() > 0); } @Test -- cgit 1.2.3-korg