diff options
author | shaoqiu <tim.huang@est.tech> | 2019-12-13 15:10:29 +0800 |
---|---|---|
committer | shaoqiu <tim.huang@est.tech> | 2020-01-08 10:11:17 +0800 |
commit | b2bb847016d780377ea4ff26b389885e9295e7ff (patch) | |
tree | 559cff3b4d638ae3a9cd91ea7e4d092a1ebe371a /services/services-engine/src/main/java | |
parent | 79a7ee5e295a5661b12f250495dbe311c8816107 (diff) |
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 <tim.huang@est.tech>
Diffstat (limited to 'services/services-engine/src/main/java')
6 files changed, 259 insertions, 17 deletions
diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java index 6c86c1eff..169f8154a 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications 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.service.engine.main; import java.io.IOException; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.stream.Stream; @@ -31,6 +32,7 @@ import lombok.Setter; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel; import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; import org.onap.policy.apex.model.policymodel.handling.PolicyModelMerger; import org.onap.policy.apex.model.utilities.TextFileUtils; @@ -266,6 +268,17 @@ public class ApexActivator { } /** + * Get the Apex engine worker stats. + */ + public List<AxEngineModel> getEngineStats() { + List<AxEngineModel> engineStats = null; + if (apexEngineService != null) { + engineStats = apexEngineService.getEngineStats(); + } + return engineStats; + } + + /** * Terminate the Apex engine. * * @throws ApexException on termination errors diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java index 14b57b2d1..cea70ce7a 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modification Copyright (C) 2019 Nordix Foundation. + * Modification 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. @@ -24,6 +24,7 @@ package org.onap.policy.apex.service.engine.main; import java.util.Arrays; import java.util.Base64; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; @@ -34,6 +35,7 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.service.ModelService; import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; +import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel; import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; import org.onap.policy.apex.service.parameters.ApexParameterHandler; import org.onap.policy.apex.service.parameters.ApexParameters; @@ -258,8 +260,19 @@ public class ApexMain { } /** - * The Class ApexMainShutdownHookClass terminates the Apex engine for the Apex service when its run method is - * called. + * Get the Engine Stats. + */ + public List<AxEngineModel> getEngineStats() { + List<AxEngineModel> engineStats = null; + if (activator != null) { + engineStats = activator.getEngineStats(); + } + return engineStats; + } + + /** + * The Class ApexMainShutdownHookClass terminates the Apex engine for the Apex service when its run + * method is called. */ private class ApexMainShutdownHookClass extends Thread { /** diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexPolicyStatisticsManager.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexPolicyStatisticsManager.java new file mode 100644 index 000000000..0200af37d --- /dev/null +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexPolicyStatisticsManager.java @@ -0,0 +1,175 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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.apex.service.engine.main; + +import java.util.concurrent.atomic.AtomicLong; +import org.onap.policy.common.utils.services.Registry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ApexPolicyStatisticsManager { + private static final Logger LOGGER = LoggerFactory.getLogger(ApexPolicyStatisticsManager.class); + public static final String REG_APEX_PDP_POLICY_COUNTER = "object:pdp/statistics/policy/counter"; + private final AtomicLong policyDeployCount = new AtomicLong(0); + private final AtomicLong policyDeploySuccessCount = new AtomicLong(0); + private final AtomicLong policyDeployFailCount = new AtomicLong(0); + private final AtomicLong policyExecutedCount = new AtomicLong(0); + private final AtomicLong policyExecutedSuccessCount = new AtomicLong(0); + private final AtomicLong policyExecutedFailCount = new AtomicLong(0); + + /** + * Constructs the object. + */ + public ApexPolicyStatisticsManager() { + super(); + } + + /** + * To get the ApexPolicyStatisticsManager in Registry. + * + * @return ApexPolicyStatisticsManager The obj in Registry. + */ + public static ApexPolicyStatisticsManager getInstanceFromRegistry() { + ApexPolicyStatisticsManager instance = null; + try { + instance = Registry.get(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER); + } catch (IllegalArgumentException e) { + LOGGER.debug("ApexPolicyStatisticsManager is not registered yet"); + } + return instance; + } + + + /** + * Update the policy deploy count. + */ + public void updatePolicyDeployCounter(final boolean isSuccessful) { + this.updatepPolicyDeployCount(); + if (!isSuccessful) { + this.updatePolicyDeployFailCount(); + } else { + this.updatePolicyDeploySuccessCount(); + } + } + + /** + * Update the policy executed count. + */ + public void updatePolicyExecutedCounter(final boolean isSuccessful) { + this.updatePolicyExecutedCount(); + if (isSuccessful) { + this.updatePolicyExecutedSuccessCount(); + } else { + this.updatePolicyExecutedFailCount(); + } + } + + /** + * Method to update the total policy deploy count. + * + * @return the updated value of policyDeployCount + */ + private long updatepPolicyDeployCount() { + return policyDeployCount.incrementAndGet(); + } + + /** + * Method to update the total policy deploy failed count. + * + * @return the updated value of totalPolicyDeployCount + */ + private long updatePolicyDeployFailCount() { + return policyDeployFailCount.incrementAndGet(); + } + + /** + * Method to update the policy deploy success count. + * + * @return the updated value of policyDeploySuccessCount + */ + private long updatePolicyDeploySuccessCount() { + return policyDeploySuccessCount.incrementAndGet(); + } + + + /** + * Method to update the total policy executed count. + * + * @return the updated value of policyExecutedCount + */ + private long updatePolicyExecutedCount() { + return policyExecutedCount.incrementAndGet(); + } + + /** + * Method to update the policy executed success count. + * + * @return the updated value of policyExecutedSuccessCount + */ + private long updatePolicyExecutedSuccessCount() { + return policyExecutedSuccessCount.incrementAndGet(); + } + + /** + * Method to update the policy executed failure count. + * + * @return the updated value of policyExecutedFailCount + */ + private long updatePolicyExecutedFailCount() { + return policyExecutedFailCount.incrementAndGet(); + } + + /** + * Reset all the statistics counts to 0. + */ + public void resetAllStatistics() { + policyDeployCount.set(0L); + policyDeployFailCount.set(0L); + policyDeploySuccessCount.set(0L); + policyExecutedCount.set(0L); + policyExecutedSuccessCount.set(0L); + policyExecutedFailCount.set(0L); + } + + public long getPolicyDeployCount() { + return policyDeployCount.get(); + } + + public long getPolicyDeployFailCount() { + return policyDeployFailCount.get(); + } + + public long getPolicyDeploySuccessCount() { + return policyDeploySuccessCount.get(); + } + + public long getPolicyExecutedCount() { + return policyExecutedCount.get(); + } + + public long getPolicyExecutedSuccessCount() { + return policyExecutedSuccessCount.get(); + } + + public long getPolicyExecutedFailCount() { + return policyExecutedFailCount.get(); + } +} diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/EngineService.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/EngineService.java index ef17a8eab..48595c5b0 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/EngineService.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/EngineService.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 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. * 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========================================================= */ @@ -21,9 +22,10 @@ package org.onap.policy.apex.service.engine.runtime; import java.util.Collection; - +import java.util.List; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel; import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState; import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; @@ -154,7 +156,7 @@ public interface EngineService { /** * This method checks if all Apex engines in the engine service are started. - * + * * <p>Note: an engine can be both not stopped and not started, for example, when it is starting or * stopping * @@ -164,7 +166,7 @@ public interface EngineService { /** * This method checks if an Apex engine in the engine service is started. - * + * * <p>Note: an engine can be both not stopped and not started, for example, when it is starting or * stopping * @@ -175,7 +177,7 @@ public interface EngineService { /** * This method checks if all Apex engines in the engine service are stopped. - * + * * <p>Note: an engine can be both not stopped and not started, for example, when it is starting or * stopping * @@ -185,7 +187,7 @@ public interface EngineService { /** * This method checks if an Apex engine in the engine service is stopped. - * + * * <p>Note: an engine can be both not stopped and not started, for example, when it is starting or * stopping * @@ -219,6 +221,13 @@ public interface EngineService { String getStatus(AxArtifactKey engineKey) throws ApexException; /** + * Get the engine status information. + * + * @return the Apex status information + */ + List<AxEngineModel> getEngineStats(); + + /** * This method gets the runtime information of all Apex engines in the engine service. * * @param engineKey the engine key diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImpl.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImpl.java index f5e36e864..a558b9946 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImpl.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImpl.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications 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,9 +22,11 @@ package org.onap.policy.apex.service.engine.runtime.impl; import java.io.ByteArrayInputStream; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.BlockingQueue; @@ -37,6 +39,7 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel; import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState; import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; import org.onap.policy.apex.service.engine.event.ApexEvent; @@ -704,13 +707,25 @@ public final class EngineServiceImpl implements EngineService, EngineServiceEven LOGGER.warn(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX); throw new ApexException(ENGINE_KEY_PREAMBLE + engineKey.getId() + NOT_FOUND_SUFFIX); } - // Return the information for this worker return engineWorkerMap.get(engineKey).getStatus(engineKey); } /** * {@inheritDoc}. + * + */ + @Override + public List<AxEngineModel> getEngineStats() { + List<AxEngineModel> engineStats = new ArrayList<>(); + for (final EngineService engine : engineWorkerMap.values()) { + engineStats.addAll(engine.getEngineStats()); + } + return engineStats; + } + + /** + * {@inheritDoc}. */ @Override public String getRuntimeInfo(final AxArtifactKey engineKey) throws ApexException { diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorker.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorker.java index e00515bd0..5b5c7f49b 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorker.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorker.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Copyright (C) 2019 Nordix Foundation. + * Modifications 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,8 +27,10 @@ import com.google.gson.JsonElement; import com.google.gson.JsonParser; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.BlockingQueue; @@ -54,6 +56,7 @@ import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState; import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; import org.onap.policy.apex.service.engine.event.ApexEvent; import org.onap.policy.apex.service.engine.event.impl.enevent.ApexEvent2EnEventConverter; +import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.apex.service.engine.runtime.ApexEventListener; import org.onap.policy.apex.service.engine.runtime.EngineService; import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface; @@ -465,6 +468,16 @@ final class EngineWorker implements EngineService { * {@inheritDoc}. */ @Override + public List<AxEngineModel> getEngineStats() { + List<AxEngineModel> engineStats = new ArrayList<>(); + engineStats.add(engine.getEngineStatus()); + return engineStats; + } + + /** + * {@inheritDoc}. + */ + @Override public String getRuntimeInfo(final AxArtifactKey engineKey) { // We'll build up the JSON string for runtime information bit by bit final StringBuilder runtimeJsonStringBuilder = new StringBuilder(); @@ -594,13 +607,13 @@ final class EngineWorker implements EngineService { LOGGER.debug("Engine {} processing interrupted ", engineWorkerKey); break; } - + boolean executedResult = false; try { if (event != null) { debugEventIfDebugEnabled(event); final EnEvent enevent = apexEnEventConverter.fromApexEvent(event); - engine.handleEvent(enevent); + executedResult = engine.handleEvent(enevent); } } catch (final ApexException e) { LOGGER.warn("Engine {} failed to process event {}", engineWorkerKey, event.toString(), e); @@ -608,6 +621,10 @@ final class EngineWorker implements EngineService { LOGGER.warn("Engine {} terminated processing event {}", engineWorkerKey, event.toString(), e); stopFlag = true; } + ApexPolicyStatisticsManager apexPolicyCounter = ApexPolicyStatisticsManager.getInstanceFromRegistry(); + if (!stopFlag && apexPolicyCounter != null) { + apexPolicyCounter.updatePolicyExecutedCounter(executedResult); + } } LOGGER.debug("Engine {} completed processing", engineWorkerKey); } |