Populate the PdpStatistics data in heartbeat 95/99595/6
authorshaoqiu <tim.huang@est.tech>
Fri, 13 Dec 2019 07:10:29 +0000 (15:10 +0800)
committershaoqiu <tim.huang@est.tech>
Wed, 8 Jan 2020 02:11:17 +0000 (10:11 +0800)
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>
16 files changed:
model/engine-model/src/main/java/org/onap/policy/apex/model/enginemodel/concepts/AxEngineStats.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexMain.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexPolicyStatisticsManager.java [new file with mode: 0644]
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/EngineService.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineServiceImpl.java
services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorker.java
services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyEngineService.java
services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorkerTest.java
services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java
services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java
services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java
services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java
services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java
services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java
services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java

index a662744..d01da46 100644 (file)
@@ -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;
     }
 
index 6c86c1e..169f815 100644 (file)
@@ -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<AxEngineModel> getEngineStats() {
+        List<AxEngineModel> engineStats = null;
+        if (apexEngineService != null) {
+            engineStats = apexEngineService.getEngineStats();
+        }
+        return engineStats;
+    }
+
     /**
      * Terminate the Apex engine.
      *
index 14b57b2..cea70ce 100644 (file)
@@ -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 (file)
index 0000000..0200af3
--- /dev/null
@@ -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();
+    }
+}
index ef17a8e..48595c5 100644 (file)
@@ -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=========================================================
  */
 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
      *
@@ -218,6 +220,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.
      *
index f5e36e8..a558b99 100644 (file)
@@ -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.
 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<AxEngineModel> getEngineStats() {
+        List<AxEngineModel> engineStats = new ArrayList<>();
+        for (final EngineService engine : engineWorkerMap.values()) {
+            engineStats.addAll(engine.getEngineStats());
+        }
+        return engineStats;
+    }
+
     /**
      * {@inheritDoc}.
      */
index e00515b..5b5c7f4 100644 (file)
@@ -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<AxEngineModel> getEngineStats() {
+        List<AxEngineModel> 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);
         }
index ef54e8e..f917fcf 100644 (file)
@@ -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<AxEngineModel> getEngineStats() {
+        throw new NotImplementedException("Not implemented on dummy class");
+    }
+
     /**
      * {@inheritDoc}.
      */
index a6040d9..dee58b8 100644 (file)
@@ -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<ApexEvent> 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<ApexEvent> 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());
+
     }
 }
index 2a3031a..4a5fb36 100644 (file)
@@ -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);
     }
 
     /**
index 6a5bb17..8150ff9 100644 (file)
@@ -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<AxEngineModel> getEngineStats() {
+        List<AxEngineModel> engineStats = null;
+        if (null != apexMain && apexMain.isAlive()) {
+            engineStats = apexMain.getEngineStats();
+        }
+        return engineStats;
+    }
+
     /**
      * Method to check whether the apex engine is running or not.
      */
index 697f3d4..e82e347 100644 (file)
@@ -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.
 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<PdpEngineWorkerStatistics> getEngineWorkerStats(@NonNull final ApexEngineHandler apexEngineHandler) {
+        List<PdpEngineWorkerStatistics> pdpEngineWorkerStats = new ArrayList<>();
+        List<AxEngineModel> 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.
      *
index fd95b47..46d04f6 100644 (file)
@@ -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;
     }
 
index 33ac81f..aa5a645 100644 (file)
@@ -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;
     }
 
index 2fcfe88..43d36f8 100644 (file)
@@ -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();
     }
 
index 7f7de3b..17f909f 100644 (file)
@@ -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