/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021-2022 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.
package org.onap.policy.pdpx.main;
+import java.time.Instant;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.onap.policy.common.utils.network.NetworkUtil;
import org.onap.policy.models.pdp.concepts.PdpMessage;
import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
+import org.onap.policy.models.pdp.concepts.PdpStatistics;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
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.pdpx.main.rest.XacmlPdpApplicationManager;
+import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public synchronized PdpStatus genHeartbeat() {
// first, update status fields
status.setHealthy(XacmlPdpActivator.getCurrent().isAlive() ? PdpHealthStatus.HEALTHY
- : PdpHealthStatus.NOT_HEALTHY);
+ : PdpHealthStatus.NOT_HEALTHY);
- return new PdpStatus(status);
+ PdpStatus heartbeat = new PdpStatus(status);
+ heartbeat.setStatistics(getStatistics());
+ return heartbeat;
+ }
+
+ /**
+ * Generates statistics to be used in a heart beat message.
+ *
+ * @return statistics for heart beat message
+ */
+ protected PdpStatistics getStatistics() {
+ XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
+ if (stats == null) {
+ LOGGER.warn("XacmlPdpStatisticsManager is null");
+ return null;
+ }
+ stats.setTotalPolicyCount(appManager.getPolicyCount());
+
+ PdpStatistics pdpStats = new PdpStatistics();
+ pdpStats.setPdpGroupName(this.status.getPdpGroup());
+ pdpStats.setPdpSubGroupName(this.status.getPdpSubgroup());
+ pdpStats.setTimeStamp(Instant.ofEpochSecond(this.status.getTimestampMs()));
+
+ pdpStats.setPolicyExecutedCount(stats.getPermitDecisionsCount() + stats.getDenyDecisionsCount());
+ pdpStats.setPolicyExecutedSuccessCount(stats.getPermitDecisionsCount());
+ pdpStats.setPolicyExecutedFailCount(stats.getDenyDecisionsCount());
+
+ pdpStats.setPolicyDeployCount(stats.getDeploySuccessCount() + stats.getDeployFailureCount());
+ pdpStats.setPolicyDeploySuccessCount(stats.getDeploySuccessCount());
+ pdpStats.setPolicyDeployFailCount(stats.getDeployFailureCount());
+
+ pdpStats.setPolicyUndeployCount(stats.getUndeploySuccessCount() + stats.getUndeployFailureCount());
+ pdpStats.setPolicyUndeploySuccessCount(stats.getUndeploySuccessCount());
+ pdpStats.setPolicyUndeployFailCount(stats.getUndeployFailureCount());
+ return pdpStats;
}
/**
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2022 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.
List<ToscaConceptIdentifier> toBeUndeployedIds =
Optional.ofNullable(message.getPoliciesToBeUndeployed()).orElse(Collections.emptyList());
+ var stats = XacmlPdpStatisticsManager.getCurrent();
+
// Undeploy policies
for (ToscaConceptIdentifier policyId: toBeUndeployedIds) {
ToscaPolicy policy = deployedPolicies.get(policyId);
if (policy == null) {
LOGGER.warn("attempt to undeploy policy that has not been previously deployed: {}", policyId);
+ stats.updateUndeployFailureCount();
} else if (toBeDeployedPolicies.containsKey(policyId)) {
LOGGER.warn("not undeploying policy, as it also appears in the deployment list: {}", policyId);
+ stats.updateUndeployFailureCount();
} else {
appManager.removeUndeployedPolicy(policy);
+ stats.updateUndeploySuccessCount();
}
}
if (!deployedPolicies.containsKey(policy.getIdentifier())) {
try {
appManager.loadDeployedPolicy(policy);
+ stats.updateDeploySuccessCount();
} catch (XacmlApplicationException e) {
// Failed to load policy, return error(s) to PAP
LOGGER.error("Failed to load policy: {}", policy, e);
errorMessage.append("Failed to load policy: " + policy + ": "
- + e.getMessage() + XacmlPolicyUtils.LINE_SEPARATOR);
+ + e.getMessage() + XacmlPolicyUtils.LINE_SEPARATOR);
+ stats.updateDeployFailureCount();
}
}
}
// update the policy count statistic
- var stats = XacmlPdpStatisticsManager.getCurrent();
- if (stats != null) {
- stats.setTotalPolicyCount(appManager.getPolicyCount());
- }
+ stats.setTotalPolicyCount(appManager.getPolicyCount());
PdpStatus status = state.updateInternalState(message, errorMessage.toString());
LOGGER.debug("Returning current deployed policies: {} ", status.getPolicies());
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021-2022 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.
import lombok.Getter;
import lombok.Setter;
+import lombok.Synchronized;
/**
* Class to hold statistical data for xacmlPdp component.
- *
*/
-@Getter
+@Getter(onMethod_ = @Synchronized)
public class XacmlPdpStatisticsManager {
@Getter
@Setter
private long errorCount;
private long permitDecisionsCount;
private long denyDecisionsCount;
+ private long deploySuccessCount;
+ private long deployFailureCount;
+ private long undeploySuccessCount;
+ private long undeployFailureCount;
private long indeterminantDecisionsCount;
private long notApplicableDecisionsCount;
*
* @return the total
*/
+ @Synchronized
public long setTotalPolicyTypesCount(long newCount) {
totalPolicyTypesCount = newCount;
return totalPolicyTypesCount;
*
* @return the total
*/
+ @Synchronized
public long setTotalPolicyCount(long newCount) {
totalPoliciesCount = newCount;
return totalPoliciesCount;
*
* @return the errorDecisionsCount
*/
+ @Synchronized
public long updateErrorCount() {
return ++errorCount;
}
*
* @return the permitDecisionsCount
*/
+ @Synchronized
public long updatePermitDecisionsCount() {
return ++permitDecisionsCount;
}
*
* @return the denyDecisionsCount
*/
+ @Synchronized
public long updateDenyDecisionsCount() {
return ++denyDecisionsCount;
}
+ /**
+ * Method to update the number of successful deploys.
+ *
+ * @return the deploySuccessCount
+ */
+ @Synchronized
+ public long updateDeploySuccessCount() {
+ return ++deploySuccessCount;
+ }
+
+ /**
+ * Method to update the number of failed deploys.
+ *
+ * @return the deployFailureCount
+ */
+ @Synchronized
+ public long updateDeployFailureCount() {
+ return ++deployFailureCount;
+ }
+
+ /**
+ * Method to update the number of successful undeploys.
+ *
+ * @return the undeploySuccessCount
+ */
+ @Synchronized
+ public long updateUndeploySuccessCount() {
+ return ++undeploySuccessCount;
+ }
+
+ /**
+ * Method to update the number of failed undeploys.
+ *
+ * @return the undeployFailureCount
+ */
+ @Synchronized
+ public long updateUndeployFailureCount() {
+ return ++undeployFailureCount;
+ }
+
/**
* Method to update the number of indeterminant decisions.
*
* @return the indeterminantDecisionsCount
*/
+ @Synchronized
public long updateIndeterminantDecisionsCount() {
return ++indeterminantDecisionsCount;
}
*
* @return the notApplicableDecisionsCount
*/
+ @Synchronized
public long updateNotApplicableDecisionsCount() {
return ++notApplicableDecisionsCount;
}
/**
* Reset all the statistics counts to 0.
*/
+ @Synchronized
public void resetAllStatistics() {
totalPolicyTypesCount = 0L;
totalPoliciesCount = 0L;
errorCount = 0L;
permitDecisionsCount = 0L;
denyDecisionsCount = 0L;
+ deploySuccessCount = 0L;
+ deployFailureCount = 0L;
+ undeploySuccessCount = 0L;
+ undeployFailureCount = 0L;
indeterminantDecisionsCount = 0L;
notApplicableDecisionsCount = 0L;
}
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2022 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.
private long totalErrorCount;
private long permitDecisionsCount;
private long denyDecisionsCount;
+ private long deploySuccessCount;
+ private long deployFailureCount;
+ private long undeploySuccessCount;
+ private long undeployFailureCount;
private long indeterminantDecisionsCount;
private long notApplicableDecisionsCount;
}
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021-2022 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.
report.setTotalErrorCount(stats.getErrorCount());
report.setPermitDecisionsCount(stats.getPermitDecisionsCount());
report.setDenyDecisionsCount(stats.getDenyDecisionsCount());
+ report.setDeploySuccessCount(stats.getDeploySuccessCount());
+ report.setDeployFailureCount(stats.getDeployFailureCount());
+ report.setUndeploySuccessCount(stats.getUndeploySuccessCount());
+ report.setUndeployFailureCount(stats.getUndeployFailureCount());
report.setIndeterminantDecisionsCount(stats.getIndeterminantDecisionsCount());
report.setNotApplicableDecisionsCount(stats.getNotApplicableDecisionsCount());
return report;
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021-2022 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import java.util.Arrays;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
+import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
+import org.onap.policy.models.pdp.concepts.PdpStatistics;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
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.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.pdpx.main.comm.XacmlPdpUpdatePublisher;
import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
+import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
@RunWith(MockitoJUnitRunner.class)
pdpName = XacmlState.PDP_NAME;
XacmlPdpActivator.setCurrent(act);
-
state = new XacmlState(appmgr, GROUP, PDP_TYPE);
}
assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
}
+ @Test
+ public void testGetStatistics() {
+ XacmlPdpStatisticsManager statmgr = new XacmlPdpStatisticsManager();
+ XacmlPdpStatisticsManager.setCurrent(statmgr);
+
+ ToscaPolicy policy1 = mock(ToscaPolicy.class);
+ ToscaPolicy policy2 = mock(ToscaPolicy.class);
+ ToscaConceptIdentifier ident = new ToscaConceptIdentifier("undeployed", "2.3.4");
+ when(policy2.getIdentifier()).thenReturn(ident);
+
+ PdpUpdate message = new PdpUpdate();
+ message.setPoliciesToBeDeployed(Arrays.asList(policy1));
+ message.setPoliciesToBeUndeployed(Arrays.asList(policy2.getIdentifier()));
+
+ TopicSinkClient client = Mockito.mock(TopicSinkClient.class);
+ XacmlPdpUpdatePublisher publisher = new XacmlPdpUpdatePublisher(client, state, appmgr);
+ publisher.handlePdpUpdate(message);
+
+ PdpStatistics stats = state.getStatistics();
+ assertTrue(stats != null);
+ assertEquals(GROUP, stats.getPdpGroupName());
+ assertEquals(stats.getPolicyDeployCount(), 1);
+ assertEquals(stats.getPolicyDeploySuccessCount(), 1);
+ assertEquals(stats.getPolicyDeployFailCount(), 0);
+ assertEquals(stats.getPolicyUndeployCount(), 1);
+ assertEquals(stats.getPolicyUndeployFailCount(), 1);
+ assertEquals(stats.getPolicyUndeploySuccessCount(), 0);
+
+ PdpStatistics test = new PdpStatistics();
+ test.setTimeStamp(stats.getTimeStamp());
+ test.setPdpGroupName(GROUP);
+ test.setPolicyDeployCount(1);
+ test.setPolicyDeploySuccessCount(1);
+ test.setPolicyUndeployCount(1);
+ test.setPolicyUndeployFailCount(1);
+
+ assertEquals(stats.toString(), test.toString());
+ }
+
@Test
public void testUpdateInternalStatePdpStateChange() {
PdpStateChange req = new PdpStateChange();
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021-2022 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.
private PdpUpdate failurePdpUpdate;
private XacmlPdpUpdatePublisher publisher;
+ private XacmlPdpStatisticsManager statmgr;
/**
when(client.send(any())).thenReturn(true);
publisher = new XacmlPdpUpdatePublisher(client, state, appmgr);
+
+ statmgr = new XacmlPdpStatisticsManager();
+ XacmlPdpStatisticsManager.setCurrent(statmgr);
}
@Test
public void testHandlePdpUpdate() throws XacmlApplicationException {
- XacmlPdpStatisticsManager statmgr = new XacmlPdpStatisticsManager();
- XacmlPdpStatisticsManager.setCurrent(statmgr);
-
publisher.handlePdpUpdate(update);
// two removed
verify(client).send(status);
}
- @Test
- public void testHandlePdpUpdate_NullStats() {
- XacmlPdpStatisticsManager.setCurrent(null);
-
- // should work without throwing an exception
- publisher.handlePdpUpdate(update);
-
- verify(client).send(status);
- }
-
@Test
public void testHandlePdpUpdate_SendFail() {