diff options
author | Liam Fallon <liam.fallon@ericsson.com> | 2018-09-14 10:15:40 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-09-14 10:15:40 +0000 |
commit | dbffbe7e96ae04ca75f0bb143c483c284cd88cb8 (patch) | |
tree | ffa5dfaaba021fc713f884c4432f6f358274ce23 /main/src | |
parent | 4b045ef40ee55394cc0a352c5460573e64be531b (diff) | |
parent | a03bfe0ac168a61a8c34329b2bfb73e360b812e1 (diff) |
Merge "Adding statistics endpoint to distribution"
Diffstat (limited to 'main/src')
5 files changed, 423 insertions, 0 deletions
diff --git a/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestController.java b/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestController.java index 95cfdd5c..d7e0f9f5 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestController.java +++ b/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestController.java @@ -57,4 +57,14 @@ public class DistributionRestController { public Response healthcheck() { return Response.status(Response.Status.OK).entity(new HealthCheckProvider().performHealthCheck()).build(); } + + @GET + @Path("statistics") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Fetch current statistics", + notes = "Provides current statistics of the Policy Distribution component", + response = StatisticsReport.class) + public Response statistics() { + return Response.status(Response.Status.OK).entity(new StatisticsProvider().fetchCurrentStatistics()).build(); + } } diff --git a/main/src/main/java/org/onap/policy/distribution/main/rest/StatisticsProvider.java b/main/src/main/java/org/onap/policy/distribution/main/rest/StatisticsProvider.java new file mode 100644 index 00000000..7532ac61 --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/rest/StatisticsProvider.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.main.rest; + +import org.onap.policy.distribution.main.startstop.DistributionActivator; +import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager; + +/** + * Class to fetch statistics of distribution service. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class StatisticsProvider { + + /** + * Returns the current statistics of distribution service. + * + * @return Report containing statistics of distribution service + */ + public StatisticsReport fetchCurrentStatistics() { + final StatisticsReport report = new StatisticsReport(); + report.setCode(DistributionActivator.isAlive() ? 200 : 500); + report.setTotalDistributionCount(DistributionStatisticsManager.getTotalDistributionCount()); + report.setDistributionSuccessCount(DistributionStatisticsManager.getDistributionSuccessCount()); + report.setDistributionFailureCount(DistributionStatisticsManager.getDistributionFailureCount()); + report.setTotalDownloadCount(DistributionStatisticsManager.getTotalDownloadCount()); + report.setDownloadSuccessCount(DistributionStatisticsManager.getDownloadSuccessCount()); + report.setDownloadFailureCount(DistributionStatisticsManager.getDownloadFailureCount()); + return report; + } +} diff --git a/main/src/main/java/org/onap/policy/distribution/main/rest/StatisticsReport.java b/main/src/main/java/org/onap/policy/distribution/main/rest/StatisticsReport.java new file mode 100644 index 00000000..4cbbcd70 --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/rest/StatisticsReport.java @@ -0,0 +1,188 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.main.rest; + +/** + * Class to represent statistics report of distribution service. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class StatisticsReport { + + private int code; + private long totalDistributionCount; + private long distributionSuccessCount; + private long distributionFailureCount; + private long totalDownloadCount; + private long downloadSuccessCount; + private long downloadFailureCount; + + /** + * Returns the code of this {@link StatisticsReport} instance. + * + * @return the code + */ + public int getCode() { + return code; + } + + /** + * Set code in this {@link StatisticsReport} instance. + * + * @param code the code to set + */ + public void setCode(final int code) { + this.code = code; + } + + /** + * Returns the totalDistributionCount of this {@link StatisticsReport} instance. + * + * @return the totalDistributionCount + */ + public long getTotalDistributionCount() { + return totalDistributionCount; + } + + /** + * Set totalDistributionCount in this {@link StatisticsReport} instance. + * + * @param totalDistributionCount the totalDistributionCount to set + */ + public void setTotalDistributionCount(final long totalDistributionCount) { + this.totalDistributionCount = totalDistributionCount; + } + + /** + * Returns the distributionSuccessCount of this {@link StatisticsReport} instance. + * + * @return the distributionSuccessCount + */ + public long getDistributionSuccessCount() { + return distributionSuccessCount; + } + + /** + * Set distributionSuccessCount in this {@link StatisticsReport} instance. + * + * @param distributionSuccessCount the distributionSuccessCount to set + */ + public void setDistributionSuccessCount(final long distributionSuccessCount) { + this.distributionSuccessCount = distributionSuccessCount; + } + + /** + * Returns the distributionFailureCount of this {@link StatisticsReport} instance. + * + * @return the distributionFailureCount + */ + public long getDistributionFailureCount() { + return distributionFailureCount; + } + + /** + * Set distributionFailureCount in this {@link StatisticsReport} instance. + * + * @param distributionFailureCount the distributionFailureCount to set + */ + public void setDistributionFailureCount(final long distributionFailureCount) { + this.distributionFailureCount = distributionFailureCount; + } + + /** + * Returns the totalDownloadCount of this {@link StatisticsReport} instance. + * + * @return the totalDownloadCount + */ + public long getTotalDownloadCount() { + return totalDownloadCount; + } + + /** + * Set totalDownloadCount in this {@link StatisticsReport} instance. + * + * @param totalDownloadCount the totalDownloadCount to set + */ + public void setTotalDownloadCount(final long totalDownloadCount) { + this.totalDownloadCount = totalDownloadCount; + } + + /** + * Returns the downloadSuccessCount of this {@link StatisticsReport} instance. + * + * @return the downloadSuccessCount + */ + public long getDownloadSuccessCount() { + return downloadSuccessCount; + } + + /** + * Set downloadSuccessCount in this {@link StatisticsReport} instance. + * + * @param downloadSuccessCount the downloadSuccessCount to set + */ + public void setDownloadSuccessCount(final long downloadSuccessCount) { + this.downloadSuccessCount = downloadSuccessCount; + } + + /** + * Returns the downloadFailureCount of this {@link StatisticsReport} instance. + * + * @return the downloadFailureCount + */ + public long getDownloadFailureCount() { + return downloadFailureCount; + } + + /** + * Set downloadFailureCount in this {@link StatisticsReport} instance. + * + * @param downloadFailureCount the downloadFailureCount to set + */ + public void setDownloadFailureCount(final long downloadFailureCount) { + this.downloadFailureCount = downloadFailureCount; + } + + + /** + * {@inheritDoc}. + */ + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("StatisticsReport [code="); + builder.append(getCode()); + builder.append(", totalDistributionCount="); + builder.append(getTotalDistributionCount()); + builder.append(", distributionSuccessCount="); + builder.append(getDistributionSuccessCount()); + builder.append(", distributionFailureCount="); + builder.append(getDistributionFailureCount()); + builder.append(", totalDownloadCount="); + builder.append(getTotalDownloadCount()); + builder.append(", downloadSuccessCount="); + builder.append(getDownloadSuccessCount()); + builder.append(", downloadFailureCount="); + builder.append(getDownloadFailureCount()); + builder.append("]"); + return builder.toString(); + } +} diff --git a/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java b/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java new file mode 100644 index 00000000..92ae14a8 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionStatistics.java @@ -0,0 +1,129 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.main.rest; + +import static org.junit.Assert.assertEquals; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.Test; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.distribution.main.parameters.CommonTestData; +import org.onap.policy.distribution.main.parameters.RestServerParameters; +import org.onap.policy.distribution.main.startstop.Main; +import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager; + +/** + * Class to perform unit test of {@link DistributionRestController}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class TestDistributionStatistics { + + private static final Logger LOGGER = FlexLogger.getLogger(TestDistributionStatistics.class); + + + @Test + public void testDistributionStatistics_200() throws PolicyDistributionException, InterruptedException { + final Main main = startDistributionService(); + StatisticsReport report = getDistributionStatistics(); + + validateReport(report, 0, 200); + updateDistributionStatistics(); + report = getDistributionStatistics(); + validateReport(report, 1, 200); + stopDistributionService(main); + DistributionStatisticsManager.resetAllStatistics(); + } + + @Test + public void testDistributionStatistics_500() throws InterruptedException { + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + restServerParams.setName(CommonTestData.DISTRIBUTION_GROUP_NAME); + + final DistributionRestServer restServer = new DistributionRestServer(restServerParams); + restServer.start(); + final StatisticsReport report = getDistributionStatistics(); + + validateReport(report, 0, 500); + restServer.shutdown(); + DistributionStatisticsManager.resetAllStatistics(); + } + + + private Main startDistributionService() { + final String[] distributionConfigParameters = + { "-c", "parameters/DistributionConfigParameters.json" }; + return new Main(distributionConfigParameters); + } + + private void stopDistributionService(final Main main) throws PolicyDistributionException { + main.shutdown(); + } + + private StatisticsReport getDistributionStatistics() throws InterruptedException { + StatisticsReport response = null; + final ClientConfig clientConfig = new ClientConfig(); + + final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); + clientConfig.register(feature); + + final Client client = ClientBuilder.newClient(clientConfig); + final WebTarget webTarget = client.target("http://localhost:6969/statistics"); + + final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + while (response == null) { + try { + response = invocationBuilder.get(StatisticsReport.class); + } catch (final Exception exp) { + LOGGER.info("the server is not started yet. We will retry again"); + } + } + return response; + } + + private void updateDistributionStatistics() { + DistributionStatisticsManager.updateTotalDistributionCount(); + DistributionStatisticsManager.updateDistributionSuccessCount(); + DistributionStatisticsManager.updateDistributionFailureCount(); + DistributionStatisticsManager.updateTotalDownloadCount(); + DistributionStatisticsManager.updateDownloadSuccessCount(); + DistributionStatisticsManager.updateDownloadFailureCount(); + } + + private void validateReport(final StatisticsReport report, final int count, final int code) { + assertEquals(code, report.getCode()); + assertEquals(count, report.getTotalDistributionCount()); + assertEquals(count, report.getDistributionSuccessCount()); + assertEquals(count, report.getDistributionFailureCount()); + assertEquals(count, report.getTotalDownloadCount()); + assertEquals(count, report.getDownloadSuccessCount()); + assertEquals(count, report.getDownloadFailureCount()); + } +} diff --git a/main/src/test/java/org/onap/policy/distribution/main/rest/TestStatisticsReport.java b/main/src/test/java/org/onap/policy/distribution/main/rest/TestStatisticsReport.java new file mode 100644 index 00000000..64d2e5c5 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/rest/TestStatisticsReport.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.distribution.main.rest; + +import com.openpojo.reflection.filters.FilterClassName; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; + +import org.junit.Test; +import org.onap.policy.common.utils.validation.ToStringTester; + +/** + * Class to perform unit testing of {@link StatisticsReport}. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class TestStatisticsReport { + + @Test + public void testStatisticsReport() { + final Validator validator = ValidatorBuilder.create().with(new ToStringTester()).with(new SetterMustExistRule()) + .with(new SetterTester()).with(new GetterTester()).build(); + validator.validate(StatisticsReport.class.getPackage().getName(), + new FilterClassName(StatisticsReport.class.getName())); + } +} |