report.setName(NAME);
report.setUrl(URL);
- boolean alive = PapActivator.isAlive();
+ boolean alive = PapActivator.getCurrent().isAlive();
report.setHealthy(alive);
report.setCode(alive ? 200 : 500);
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 AT&T Intellectual Property.
* ================================================================================
* 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.pap.main.rest;
+import java.util.concurrent.atomic.AtomicLong;
import lombok.Getter;
/**
public class PapStatisticsManager {
@Getter
- private static long totalPdpCount;
- @Getter
- private static long totalPdpGroupCount;
- @Getter
- private static long totalPolicyDeployCount;
- @Getter
- private static long policyDeploySuccessCount;
- @Getter
- private static long policyDeployFailureCount;
- @Getter
- private static long totalPolicyDownloadCount;
- @Getter
- private static long policyDownloadSuccessCount;
- @Getter
- private static long policyDownloadFailureCount;
+ private static final PapStatisticsManager instance = new PapStatisticsManager();
- private PapStatisticsManager() {
- throw new IllegalStateException("Instantiation of the class is not allowed");
+ private final AtomicLong totalPdpCount = new AtomicLong(0);
+ private final AtomicLong totalPdpGroupCount = new AtomicLong(0);
+ private final AtomicLong totalPolicyDeployCount = new AtomicLong(0);
+ private final AtomicLong policyDeploySuccessCount = new AtomicLong(0);
+ private final AtomicLong policyDeployFailureCount = new AtomicLong(0);
+ private final AtomicLong totalPolicyDownloadCount = new AtomicLong(0);
+ private final AtomicLong policyDownloadSuccessCount = new AtomicLong(0);
+ private final AtomicLong policyDownloadFailureCount = new AtomicLong(0);
+
+ /**
+ * Constructs the object.
+ */
+ protected PapStatisticsManager() {
+ super();
}
/**
*
* @return the updated value of totalPdpCount
*/
- public static long updateTotalPdpCount() {
- return ++totalPdpCount;
+ public long updateTotalPdpCount() {
+ return totalPdpCount.incrementAndGet();
}
/**
*
* @return the updated value of totalPdpGroupCount
*/
- public static long updateTotalPdpGroupCount() {
- return ++totalPdpGroupCount;
+ public long updateTotalPdpGroupCount() {
+ return totalPdpGroupCount.incrementAndGet();
}
/**
*
* @return the updated value of totalPolicyDeployCount
*/
- public static long updateTotalPolicyDeployCount() {
- return ++totalPolicyDeployCount;
+ public long updateTotalPolicyDeployCount() {
+ return totalPolicyDeployCount.incrementAndGet();
}
/**
*
* @return the updated value of policyDeploySuccessCount
*/
- public static long updatePolicyDeploySuccessCount() {
- return ++policyDeploySuccessCount;
+ public long updatePolicyDeploySuccessCount() {
+ return policyDeploySuccessCount.incrementAndGet();
}
/**
*
* @return the updated value of policyDeployFailureCount
*/
- public static long updatePolicyDeployFailureCount() {
- return ++policyDeployFailureCount;
+ public long updatePolicyDeployFailureCount() {
+ return policyDeployFailureCount.incrementAndGet();
}
/**
*
* @return the updated value of totalPolicyDownloadCount
*/
- public static long updateTotalPolicyDownloadCount() {
- return ++totalPolicyDownloadCount;
+ public long updateTotalPolicyDownloadCount() {
+ return totalPolicyDownloadCount.incrementAndGet();
}
/**
*
* @return the updated value of policyDownloadSuccessCount
*/
- public static long updatePolicyDownloadSuccessCount() {
- return ++policyDownloadSuccessCount;
+ public long updatePolicyDownloadSuccessCount() {
+ return policyDownloadSuccessCount.incrementAndGet();
}
/**
*
* @return the updated value of policyDownloadFailureCount
*/
- public static long updatePolicyDownloadFailureCount() {
- return ++policyDownloadFailureCount;
+ public long updatePolicyDownloadFailureCount() {
+ return policyDownloadFailureCount.incrementAndGet();
}
/**
* Reset all the statistics counts to 0.
*/
- public static void resetAllStatistics() {
- totalPdpCount = 0L;
- totalPdpGroupCount = 0L;
- totalPolicyDeployCount = 0L;
- policyDeploySuccessCount = 0L;
- policyDeployFailureCount = 0L;
- totalPolicyDownloadCount = 0L;
- policyDownloadSuccessCount = 0L;
- policyDownloadFailureCount = 0L;
+ public void resetAllStatistics() {
+ totalPdpCount.set(0L);
+ totalPdpGroupCount.set(0L);
+ totalPolicyDeployCount.set(0L);
+ policyDeploySuccessCount.set(0L);
+ policyDeployFailureCount.set(0L);
+ totalPolicyDownloadCount.set(0L);
+ policyDownloadSuccessCount.set(0L);
+ policyDownloadFailureCount.set(0L);
+ }
+
+ public long getTotalPdpCount() {
+ return totalPdpCount.get();
+ }
+
+ public long getTotalPdpGroupCount() {
+ return totalPdpGroupCount.get();
+ }
+
+ public long getTotalPolicyDeployCount() {
+ return totalPolicyDeployCount.get();
+ }
+
+ public long getPolicyDeploySuccessCount() {
+ return policyDeploySuccessCount.get();
+ }
+
+ public long getPolicyDeployFailureCount() {
+ return policyDeployFailureCount.get();
+ }
+
+ public long getTotalPolicyDownloadCount() {
+ return totalPolicyDownloadCount.get();
+ }
+
+ public long getPolicyDownloadSuccessCount() {
+ return policyDownloadSuccessCount.get();
+ }
+
+ public long getPolicyDownloadFailureCount() {
+ return policyDownloadFailureCount.get();
}
}
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 AT&T Intellectual Property.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
public StatisticsReport fetchCurrentStatistics() {
final StatisticsReport report = new StatisticsReport();
- report.setCode(PapActivator.isAlive() ? 200 : 500);
- report.setTotalPdpCount(PapStatisticsManager.getTotalPdpCount());
- report.setTotalPdpGroupCount(PapStatisticsManager.getTotalPdpGroupCount());
- report.setTotalPolicyDownloadCount(PapStatisticsManager.getTotalPolicyDownloadCount());
- report.setPolicyDownloadSuccessCount(PapStatisticsManager.getPolicyDownloadSuccessCount());
- report.setPolicyDownloadFailureCount(PapStatisticsManager.getPolicyDownloadFailureCount());
- report.setTotalPolicyDeployCount(PapStatisticsManager.getTotalPolicyDeployCount());
- report.setPolicyDeploySuccessCount(PapStatisticsManager.getPolicyDeploySuccessCount());
- report.setPolicyDeployFailureCount(PapStatisticsManager.getPolicyDeployFailureCount());
+ report.setCode(PapActivator.getCurrent().isAlive() ? 200 : 500);
+
+ PapStatisticsManager mgr = PapStatisticsManager.getInstance();
+ report.setTotalPdpCount(mgr.getTotalPdpCount());
+ report.setTotalPdpGroupCount(mgr.getTotalPdpGroupCount());
+ report.setTotalPolicyDownloadCount(mgr.getTotalPolicyDownloadCount());
+ report.setPolicyDownloadSuccessCount(mgr.getPolicyDownloadSuccessCount());
+ report.setPolicyDownloadFailureCount(mgr.getPolicyDownloadFailureCount());
+ report.setTotalPolicyDeployCount(mgr.getTotalPolicyDeployCount());
+ report.setPolicyDeploySuccessCount(mgr.getPolicyDeploySuccessCount());
+ report.setPolicyDeployFailureCount(mgr.getPolicyDeployFailureCount());
+
return report;
}
}
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 AT&T Intellectual Property.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// Now, create the activator for the policy pap service
activator = new PapActivator(parameterGroup);
+ PapActivator.setCurrent(activator);
// Start the activator
try {
package org.onap.policy.pap.main.startstop;
+import lombok.Getter;
+import lombok.Setter;
import org.onap.policy.common.parameters.ParameterService;
import org.onap.policy.pap.main.PolicyPapException;
import org.onap.policy.pap.main.parameters.PapParameterGroup;
private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class);
private final PapParameterGroup papParameterGroup;
- private static volatile boolean alive = false;
+
+ /**
+ * The current activator. This is initialized to a dummy instance used until the real
+ * one has been configured.
+ */
+ @Getter
+ @Setter
+ private static volatile PapActivator current = new PapActivator(null);
+
+ @Getter
+ @Setter(lombok.AccessLevel.PRIVATE)
+ private volatile boolean alive = false;
+
private PapRestServer restServer;
/**
LOGGER.debug("Policy pap starting as a service . . .");
startPapRestServer();
registerToParameterService(papParameterGroup);
- PapActivator.setAlive(true);
+ setAlive(true);
LOGGER.debug("Policy pap started as a service");
} catch (final Exception exp) {
LOGGER.error("Policy pap service startup failed", exp);
public void terminate() throws PolicyPapException {
try {
deregisterToParameterService(papParameterGroup);
- PapActivator.setAlive(false);
+ setAlive(false);
// Stop the pap rest server
restServer.stop();
ParameterService.deregister(papParameterGroup.getName());
}
- /**
- * Returns the alive status of pap service.
- *
- * @return the alive
- */
- public static boolean isAlive() {
- return alive;
- }
-
- /**
- * Change the alive status of pap service.
- *
- * @param status the status
- */
- private static void setAlive(final boolean status) {
- alive = status;
- }
-
/**
* Starts the pap rest server using configuration parameters.
*
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 AT&T Intellectual Property.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
@After
public void teardown() {
+ PapStatisticsManager.getInstance().resetAllStatistics();
+
try {
if (NetworkUtil.isTcpPortOpen("localhost", 6969, 1, 1000L)) {
if (main != null) {
}
@Test
- public void testHealthCheckFailure() throws InterruptedException, IOException {
+ public void testHealthCheckFailure() throws Exception {
final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
restServerParams.setName(CommonTestData.PAP_GROUP_NAME);
restServer = new PapRestServer(restServerParams);
- try {
- restServer.start();
- final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
- final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
- validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report);
- assertTrue(restServer.isAlive());
- assertTrue(restServer.toString().startsWith("PapRestServer [servers="));
- } catch (final Exception exp) {
- LOGGER.error("testHealthCheckFailure failed", exp);
- fail("Test should not throw an exception");
- }
+ restServer.start();
+ final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT);
+ final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
+ validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report);
+ assertTrue(restServer.isAlive());
+ assertTrue(restServer.toString().startsWith("PapRestServer [servers="));
}
@Test
invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
report = invocationBuilder.get(StatisticsReport.class);
validateStatisticsReport(report, 1, 200);
- PapStatisticsManager.resetAllStatistics();
} catch (final Exception exp) {
LOGGER.error("testPapStatistics_200 failed", exp);
fail("Test should not throw an exception");
final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT);
final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
validateStatisticsReport(report, 0, 500);
- PapStatisticsManager.resetAllStatistics();
} catch (final Exception exp) {
LOGGER.error("testPapStatistics_500 failed", exp);
fail("Test should not throw an exception");
}
@Test
- public void testPapStatisticsConstructorIsPrivate() {
- try {
- final Constructor<PapStatisticsManager> constructor = PapStatisticsManager.class.getDeclaredConstructor();
- assertTrue(Modifier.isPrivate(constructor.getModifiers()));
- constructor.setAccessible(true);
- constructor.newInstance();
- } catch (final Exception exp) {
- assertTrue(exp.getCause().toString().contains("Instantiation of the class is not allowed"));
- }
+ public void testPapStatisticsConstructorIsProtected() throws Exception {
+ final Constructor<PapStatisticsManager> constructor = PapStatisticsManager.class.getDeclaredConstructor();
+ assertTrue(Modifier.isProtected(constructor.getModifiers()));
}
private Main startPapService(final boolean http) {
}
private void updateDistributionStatistics() {
- PapStatisticsManager.updateTotalPdpCount();
- PapStatisticsManager.updateTotalPdpGroupCount();
- PapStatisticsManager.updateTotalPolicyDeployCount();
- PapStatisticsManager.updatePolicyDeploySuccessCount();
- PapStatisticsManager.updatePolicyDeployFailureCount();
- PapStatisticsManager.updateTotalPolicyDownloadCount();
- PapStatisticsManager.updatePolicyDownloadSuccessCount();
- PapStatisticsManager.updatePolicyDownloadFailureCount();
+ PapStatisticsManager mgr = PapStatisticsManager.getInstance();
+
+ mgr.updateTotalPdpCount();
+ mgr.updateTotalPdpGroupCount();
+ mgr.updateTotalPolicyDeployCount();
+ mgr.updatePolicyDeploySuccessCount();
+ mgr.updatePolicyDeployFailureCount();
+ mgr.updateTotalPolicyDownloadCount();
+ mgr.updatePolicyDownloadSuccessCount();
+ mgr.updatePolicyDownloadFailureCount();
}
private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
--- /dev/null
+/*
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.pap.main.rest;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+
+import org.junit.Test;
+
+public class TestPapStatisticsManager {
+
+ @Test
+ public void test() {
+ PapStatisticsManager mgr = PapStatisticsManager.getInstance();
+ assertNotNull(mgr);
+
+ // should return the same manager
+ assertSame(mgr, PapStatisticsManager.getInstance());
+
+ // work with a new object so we don't have to worry about initial counts
+ mgr = new PapStatisticsManager();
+
+ // try each update
+
+ assertEquals(0, mgr.getTotalPdpCount());
+ assertEquals(1, mgr.updateTotalPdpCount());
+ assertEquals(1, mgr.getTotalPdpCount());
+
+ assertEquals(0, mgr.getTotalPdpGroupCount());
+ assertEquals(1, mgr.updateTotalPdpGroupCount());
+ assertEquals(1, mgr.getTotalPdpGroupCount());
+
+ assertEquals(0, mgr.getTotalPolicyDeployCount());
+ assertEquals(1, mgr.updateTotalPolicyDeployCount());
+ assertEquals(1, mgr.getTotalPolicyDeployCount());
+
+ assertEquals(0, mgr.getPolicyDeploySuccessCount());
+ assertEquals(1, mgr.updatePolicyDeploySuccessCount());
+ assertEquals(1, mgr.getPolicyDeploySuccessCount());
+
+ assertEquals(0, mgr.getPolicyDeployFailureCount());
+ assertEquals(1, mgr.updatePolicyDeployFailureCount());
+ assertEquals(1, mgr.getPolicyDeployFailureCount());
+
+ assertEquals(0, mgr.getTotalPolicyDownloadCount());
+ assertEquals(1, mgr.updateTotalPolicyDownloadCount());
+ assertEquals(1, mgr.getTotalPolicyDownloadCount());
+
+ assertEquals(0, mgr.getPolicyDownloadSuccessCount());
+ assertEquals(1, mgr.updatePolicyDownloadSuccessCount());
+ assertEquals(1, mgr.getPolicyDownloadSuccessCount());
+
+ assertEquals(0, mgr.getPolicyDownloadFailureCount());
+ assertEquals(1, mgr.updatePolicyDownloadFailureCount());
+ assertEquals(1, mgr.getPolicyDownloadFailureCount());
+
+ // now check reset
+ mgr.resetAllStatistics();
+
+ assertEquals(0, mgr.getPolicyDeployFailureCount());
+ assertEquals(0, mgr.getTotalPdpCount());
+ assertEquals(0, mgr.getTotalPdpGroupCount());
+ assertEquals(0, mgr.getTotalPolicyDeployCount());
+ assertEquals(0, mgr.getPolicyDeploySuccessCount());
+ assertEquals(0, mgr.getPolicyDeployFailureCount());
+ assertEquals(0, mgr.getTotalPolicyDownloadCount());
+ assertEquals(0, mgr.getPolicyDownloadSuccessCount());
+ assertEquals(0, mgr.getPolicyDownloadFailureCount());
+ }
+}
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2019 AT&T Intellectual Property.
* ================================================================================
* 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.pap.main.startstop;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.After;
+import org.junit.Before;
import org.junit.Test;
import org.onap.policy.pap.main.PolicyPapException;
import org.onap.policy.pap.main.parameters.CommonTestData;
private static final Logger LOGGER = LoggerFactory.getLogger(TestPapActivator.class);
private PapActivator activator;
+ /**
+ * Initializes an activator.
+ * @throws Exception if an error occurs
+ */
+ @Before
+ public void setUp() throws Exception {
+ final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
+ final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters);
+ final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
+ activator = new PapActivator(parGroup);
+ }
+
/**
* Method for cleanup after each test.
*/
@Test
public void testPapActivator() throws PolicyPapException {
- final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
- final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters);
- final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
- activator = new PapActivator(parGroup);
try {
+ assertFalse(activator.isAlive());
activator.initialize();
+ assertTrue(activator.isAlive());
assertTrue(activator.getParameterGroup().isValid());
assertEquals(CommonTestData.PAP_GROUP_NAME, activator.getParameterGroup().getName());
} catch (final Exception exp) {
@Test(expected = PolicyPapException.class)
public void testPapActivatorError() throws PolicyPapException {
- final String[] papConfigParameters = { "-c", "parameters/PapConfigParameters.json" };
- final PapCommandLineArguments arguments = new PapCommandLineArguments(papConfigParameters);
- final PapParameterGroup parGroup = new PapParameterHandler().getParameters(arguments);
- activator = new PapActivator(parGroup);
activator.initialize();
assertTrue(activator.getParameterGroup().isValid());
activator.initialize();
}
+
+ @Test
+ public void testGetCurrent_testSetCurrent() {
+ assertNotNull(PapActivator.getCurrent());
+
+ PapActivator.setCurrent(activator);
+
+ assertSame(activator, PapActivator.getCurrent());
+ }
}