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 --- .../model/enginemodel/concepts/AxEngineStats.java | 6 +- .../apex/service/engine/main/ApexActivator.java | 15 +- .../policy/apex/service/engine/main/ApexMain.java | 19 ++- .../engine/main/ApexPolicyStatisticsManager.java | 175 +++++++++++++++++++++ .../apex/service/engine/runtime/EngineService.java | 25 ++- .../engine/runtime/impl/EngineServiceImpl.java | 19 ++- .../service/engine/runtime/impl/EngineWorker.java | 23 ++- .../service/engine/engdep/DummyEngineService.java | 18 ++- .../engine/runtime/impl/EngineWorkerTest.java | 28 +++- .../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 +- 16 files changed, 441 insertions(+), 37 deletions(-) create mode 100644 services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexPolicyStatisticsManager.java diff --git a/model/engine-model/src/main/java/org/onap/policy/apex/model/enginemodel/concepts/AxEngineStats.java b/model/engine-model/src/main/java/org/onap/policy/apex/model/enginemodel/concepts/AxEngineStats.java index a66274431..d01da460b 100644 --- a/model/engine-model/src/main/java/org/onap/policy/apex/model/enginemodel/concepts/AxEngineStats.java +++ b/model/engine-model/src/main/java/org/onap/policy/apex/model/enginemodel/concepts/AxEngineStats.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. @@ -43,6 +43,7 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; import org.onap.policy.common.utils.validation.Assertions; +import lombok.Getter; /** * This class is a java bean that is used to record statistics on Apex engines as they execute. @@ -87,6 +88,7 @@ public class AxEngineStats extends AxConcept { private long upTime; @Transient + @Getter private transient long lastEnterTime; @Column @@ -294,7 +296,7 @@ public class AxEngineStats extends AxConcept { * * @return the time at which the policy engine was last started */ - private long getLastStart() { + public long getLastStart() { return lastStart; } 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; @@ -265,6 +267,17 @@ public class ApexActivator { } } + /** + * Get the Apex engine worker stats. + */ + public List getEngineStats() { + List engineStats = null; + if (apexEngineService != null) { + engineStats = apexEngineService.getEngineStats(); + } + return engineStats; + } + /** * Terminate the Apex engine. * 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 getEngineStats() { + List 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. - * + * *

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. - * + * *

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. - * + * *

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. - * + * *

Note: an engine can be both not stopped and not started, for example, when it is starting or * stopping * @@ -218,6 +220,13 @@ public interface EngineService { */ String getStatus(AxArtifactKey engineKey) throws ApexException; + /** + * Get the engine status information. + * + * @return the Apex status information + */ + List getEngineStats(); + /** * This method gets the runtime information of all Apex engines in the engine service. * 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,11 +707,23 @@ 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 getEngineStats() { + List engineStats = new ArrayList<>(); + for (final EngineService engine : engineWorkerMap.values()) { + engineStats.addAll(engine.getEngineStats()); + } + return engineStats; + } + /** * {@inheritDoc}. */ 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; @@ -461,6 +464,16 @@ final class EngineWorker implements EngineService { } } + /** + * {@inheritDoc}. + */ + @Override + public List getEngineStats() { + List engineStats = new ArrayList<>(); + engineStats.add(engine.getEngineStatus()); + return engineStats; + } + /** * {@inheritDoc}. */ @@ -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); } diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyEngineService.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyEngineService.java index ef54e8e0e..f917fcf48 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyEngineService.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyEngineService.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 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========================================================= */ @@ -27,6 +28,7 @@ import java.util.List; import org.apache.commons.lang3.NotImplementedException; 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; import org.onap.policy.apex.service.engine.runtime.ApexEventListener; @@ -94,7 +96,7 @@ public class DummyEngineService implements EngineService { @Override public AxArtifactKey getApexModelKey() { modelKeyGetCalled++; - + return new AxArtifactKey("DummyApexModelKey:0.0.1"); } @@ -124,6 +126,14 @@ public class DummyEngineService implements EngineService { throw new NotImplementedException("Not implemented on dummy class"); } + /** + * {@inheritDoc}. + */ + @Override + public List getEngineStats() { + throw new NotImplementedException("Not implemented on dummy class"); + } + /** * {@inheritDoc}. */ diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorkerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorkerTest.java index a6040d969..dee58b8e9 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorkerTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorkerTest.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 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========================================================= */ @@ -55,7 +56,9 @@ import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState; import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; import org.onap.policy.apex.model.utilities.TextFileUtils; import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager; import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.common.utils.services.Registry; /** * Test the engine worker class. @@ -69,7 +72,7 @@ public class EngineWorkerTest { /** * Read the models into strings. - * + * * @throws IOException on model reading errors * @throws ApexModelException on model reading exceptions */ @@ -129,6 +132,7 @@ public class EngineWorkerTest { "org.onap.policy.apex.service.engine.runtime.impl.DummySfe"); engineParameters.getExecutorParameterMap().put("MVEL", jsExecutorParameters); ParameterService.register(engineParameters); + } /** @@ -151,6 +155,7 @@ public class EngineWorkerTest { @Test public void testEngineWorker() { + BlockingQueue eventQueue = new LinkedBlockingQueue<>(); EngineWorker worker = new EngineWorker(new AxArtifactKey("Worker", "0.0.1"), eventQueue, atFactory); @@ -330,6 +335,8 @@ public class EngineWorkerTest { @Test public void testApexImplModelWIthModel() throws ApexException { + Registry.newRegistry(); + Registry.register(ApexPolicyStatisticsManager.REG_APEX_PDP_POLICY_COUNTER, new ApexPolicyStatisticsManager()); BlockingQueue eventQueue = new LinkedBlockingQueue<>(); EngineWorker worker = new EngineWorker(new AxArtifactKey("Worker", "0.0.1"), eventQueue, atFactory); @@ -444,5 +451,18 @@ public class EngineWorkerTest { } assertNotNull(worker.getApexModelKey()); + + final ApexPolicyStatisticsManager policyCounter = ApexPolicyStatisticsManager.getInstanceFromRegistry(); + assertNotNull(policyCounter); + assertEquals(policyCounter.getPolicyExecutedCount(), + policyCounter.getPolicyExecutedFailCount() + policyCounter.getPolicyExecutedSuccessCount()); + policyCounter.resetAllStatistics(); + assertEquals(0, policyCounter.getPolicyExecutedCount()); + assertEquals(0, policyCounter.getPolicyExecutedFailCount()); + assertEquals(0, policyCounter.getPolicyExecutedSuccessCount()); + assertEquals(0, policyCounter.getPolicyDeployCount()); + assertEquals(0, policyCounter.getPolicyDeployFailCount()); + assertEquals(0, policyCounter.getPolicyDeploySuccessCount()); + } } 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