diff options
author | jhh <jorge.hernandez-herrero@att.com> | 2020-04-23 13:18:48 -0500 |
---|---|---|
committer | jhh <jorge.hernandez-herrero@att.com> | 2020-04-23 15:48:55 -0500 |
commit | e8401780120dc035b074d932b89445cfce228634 (patch) | |
tree | 36acfa1d5f6e1a522940fd3da2648b8d6f6ee450 /controlloop/common/feature-controlloop-trans/src/test/java | |
parent | 3dde9f0b68fae53c14f8fe89e58022381f1f17e0 (diff) |
transaction tracking improvements with telemetry
Issue-ID: POLICY-2456
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I75b7e8e787d567cf120d7ae7d65d1bff12e8e24e
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Diffstat (limited to 'controlloop/common/feature-controlloop-trans/src/test/java')
3 files changed, 370 insertions, 12 deletions
diff --git a/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeatureTest.java b/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeatureTest.java index b713e8e8f..eeb382ae1 100644 --- a/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeatureTest.java +++ b/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/apps/controlloop/feature/trans/ControlLoopMetricsFeatureTest.java @@ -23,6 +23,7 @@ package org.onap.policy.drools.apps.controlloop.feature.trans; import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -34,8 +35,10 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.controlloop.ControlLoopNotificationType; import org.onap.policy.controlloop.VirtualControlLoopNotification; +import org.onap.policy.controlloop.util.Serialization; import org.onap.policy.drools.persistence.SystemPersistenceConstants; import org.onap.policy.drools.system.PolicyController; import org.onap.policy.drools.system.PolicyEngineConstants; @@ -87,7 +90,7 @@ public class ControlLoopMetricsFeatureTest { } @Test - public void testValidActiveNotification() throws InterruptedException { + public void testValidActiveNotification() { ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature(); VirtualControlLoopNotification notification = new VirtualControlLoopNotification(); UUID requestId = UUID.randomUUID(); @@ -99,7 +102,7 @@ public class ControlLoopMetricsFeatureTest { assertTrue(ControlLoopMetricsManager.getManager().getTransaction(requestId).getFrom() .contains(testController.getName())); assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(requestId).getNotificationTime()); - assertTrue(ControlLoopMetricsManager.getManager().getCacheOccupancy() == 1); + assertEquals(1, ControlLoopMetricsManager.getManager().getCacheOccupancy()); /* wait for the entries to expire */ await().atMost(ControlLoopMetricsManager.getManager().getTransactionTimeout() + 1, TimeUnit.SECONDS) @@ -135,7 +138,7 @@ public class ControlLoopMetricsFeatureTest { } @Test - public void testEviction() throws InterruptedException { + public void testEviction() { ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature(); for (int i = 0; i < ControlLoopMetricsManager.getManager().getCacheSize(); i++) { VirtualControlLoopNotification notification = generateNotification(); @@ -151,10 +154,10 @@ public class ControlLoopMetricsFeatureTest { assertEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(), ControlLoopMetricsManager.getManager().getCacheOccupancy()); assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(overflowNotification.getRequestId())); - assertTrue(ControlLoopMetricsManager.getManager().getTransactionIds().size() == ControlLoopMetricsManager - .getManager().getCacheSize()); - assertTrue(ControlLoopMetricsManager.getManager().getCacheOccupancy() == ControlLoopMetricsManager.getManager() - .getCacheSize()); + assertEquals(ControlLoopMetricsManager.getManager().getTransactionIds().size(), + ControlLoopMetricsManager.getManager().getCacheSize()); + assertEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(), + ControlLoopMetricsManager.getManager().getCacheSize()); assertFalse(ControlLoopMetricsManager.getManager().getTransactionIds().isEmpty()); assertFalse(ControlLoopMetricsManager.getManager().getTransactions().isEmpty()); @@ -163,10 +166,10 @@ public class ControlLoopMetricsFeatureTest { .until(() -> ControlLoopMetricsManager.getManager().getTransactions().isEmpty()); ControlLoopMetricsManager.getManager().refresh(); - assertTrue(ControlLoopMetricsManager.getManager().getTransactionIds().size() == ControlLoopMetricsManager - .getManager().getCacheOccupancy()); - assertFalse(ControlLoopMetricsManager.getManager().getCacheOccupancy() == ControlLoopMetricsManager.getManager() - .getCacheSize()); + assertEquals(ControlLoopMetricsManager.getManager().getTransactionIds().size(), + ControlLoopMetricsManager.getManager().getCacheOccupancy()); + assertNotEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(), + ControlLoopMetricsManager.getManager().getCacheSize()); assertTrue(ControlLoopMetricsManager.getManager().getTransactionIds().isEmpty()); assertTrue(ControlLoopMetricsManager.getManager().getTransactions().isEmpty()); @@ -184,6 +187,72 @@ public class ControlLoopMetricsFeatureTest { @Test public void getSequenceNumber() { ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature(); - assertTrue(feature.getSequenceNumber() == ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY); + assertEquals(feature.getSequenceNumber(), ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY); + } + + @Test + public void testSuccessControlLoop() { + ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature(); + + String activeNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-active.json"); + VirtualControlLoopNotification active = + Serialization.gsonPretty.fromJson(activeNotification, VirtualControlLoopNotification.class); + feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, active); + assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size()); + + String opStartNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-operation.json"); + VirtualControlLoopNotification opStart = + Serialization.gsonPretty.fromJson(opStartNotification, VirtualControlLoopNotification.class); + feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, opStart); + assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size()); + + String permitNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-permit.json"); + VirtualControlLoopNotification permit = + Serialization.gsonPretty.fromJson(permitNotification, VirtualControlLoopNotification.class); + feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, permit); + assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size()); + + String restartNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-restart.json"); + VirtualControlLoopNotification restart = + Serialization.gsonPretty.fromJson(restartNotification, VirtualControlLoopNotification.class); + feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, restart); + assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size()); + + String restartSuccessNotification = + ResourceUtils.getResourceAsString("policy-cl-mgt-restart-success.json"); + VirtualControlLoopNotification restartSuccess = + Serialization.gsonPretty.fromJson(restartSuccessNotification, VirtualControlLoopNotification.class); + feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, restartSuccess); + assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size()); + + String finalSuccessNotification = + ResourceUtils.getResourceAsString("policy-cl-mgt-final-success.json"); + VirtualControlLoopNotification finalSuccess = + Serialization.gsonPretty.fromJson(finalSuccessNotification, VirtualControlLoopNotification.class); + feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, finalSuccess); + assertEquals(0, ControlLoopMetricsManager.getManager().getTransactionIds().size()); + } + + @Test + public void testUntrackedNotifications() throws InterruptedException { + ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature(); + + String finalSuccessNotification = + ResourceUtils.getResourceAsString("policy-cl-mgt-final-success.json"); + VirtualControlLoopNotification finalSuccess = + Serialization.gsonPretty.fromJson(finalSuccessNotification, VirtualControlLoopNotification.class); + finalSuccess.setRequestId(UUID.randomUUID()); + feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, finalSuccess); + assertEquals(0, ControlLoopMetricsManager.getManager().getTransactionIds().size()); + + String opStartNotification = + ResourceUtils.getResourceAsString("policy-cl-mgt-operation.json"); + VirtualControlLoopNotification opStart = + Serialization.gsonPretty.fromJson(opStartNotification, VirtualControlLoopNotification.class); + feature.beforeDeliver(testController, CommInfrastructure.DMAAP, POLICY_CL_MGT, opStart); + assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size()); + + Thread.sleep((ControlLoopMetricsManager.getManager().getTransactionTimeout() + 1) * 1000L); // NOSONAR + assertEquals(0, ControlLoopMetricsManager.getManager().getTransactionIds().size()); } } diff --git a/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/server/restful/RestTransactionTracker.java b/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/server/restful/RestTransactionTracker.java new file mode 100644 index 000000000..b954167ee --- /dev/null +++ b/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/server/restful/RestTransactionTracker.java @@ -0,0 +1,129 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.server.restful; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import java.util.UUID; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler; +import org.onap.policy.controlloop.VirtualControlLoopNotification; +import org.onap.policy.drools.apps.controlloop.feature.trans.ControlLoopMetricsManager; + +/** + * REST Transaction Tracker. + */ + +@Path("/policy/pdp/engine/controllers") +@Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML}) +@Consumes({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML}) +@Api +public class RestTransactionTracker { + + /** + * GET transactions. + */ + + @GET + @Path("transactions") + @ApiOperation(value = "Retrieve in-progress transactions", responseContainer = "List") + public Response transactions() { + return Response.status(Response.Status.OK) + .entity(ControlLoopMetricsManager.getManager().getTransactions()).build(); + } + + /** + * GET one transaction. + */ + + @GET + @Path("transactions/{transactionId}") + @ApiOperation(value = "Retrieve an in-progress transaction", response = VirtualControlLoopNotification.class) + public Response transactionId( + @ApiParam(value = "UUID", required = true) @PathParam("transactionId") String transactionId) { + VirtualControlLoopNotification notification = + ControlLoopMetricsManager.getManager().getTransaction(UUID.fromString(transactionId)); + return Response.status((notification != null) ? Response.Status.OK : Response.Status.NOT_FOUND) + .entity(notification).build(); + } + + /** + * Resets the cache with a new cache size. + */ + + @PUT + @Path("transactions/cacheSize/{cacheSize}") + @ApiOperation(value = "Sets the cache size", response = Integer.class) + public Response cacheSize( + @ApiParam(value = "cache size", required = true) @PathParam("cacheSize") int cacheSize) { + ControlLoopMetricsManager.getManager().resetCache(cacheSize, + ControlLoopMetricsManager.getManager().getTransactionTimeout()); + return Response.status(Response.Status.OK) + .entity(ControlLoopMetricsManager.getManager().getCacheSize()).build(); + } + + /** + * GET the cache size. + */ + + @GET + @Path("transactions/cacheSize") + @ApiOperation(value = "Gets the cache size", response = Integer.class) + public Response cacheSize() { + return Response.status(Response.Status.OK) + .entity(ControlLoopMetricsManager.getManager().getCacheSize()).build(); + } + + /** + * Resets the cache with a new transaction timeout in seconds. + */ + + @PUT + @Path("transactions/timeout/{timeoutSecs}") + @ApiOperation(value = "Sets the timeout in seconds", response = Integer.class) + public Response timeout( + @ApiParam(value = "timeout", required = true) @PathParam("timeoutSecs") long timeoutSecs) { + ControlLoopMetricsManager.getManager().resetCache( + ControlLoopMetricsManager.getManager().getCacheSize(), timeoutSecs); + return Response.status(Response.Status.OK) + .entity(ControlLoopMetricsManager.getManager().getTransactionTimeout()).build(); + } + + /** + * GET the cache timeout. + */ + + @GET + @Path("transactions/timeout") + @ApiOperation(value = "Gets the cache timeout", response = Long.class) + public Response timeout() { + return Response.status(Response.Status.OK) + .entity(ControlLoopMetricsManager.getManager().getTransactionTimeout()).build(); + } +} diff --git a/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/server/restful/RestTransactionTrackerTest.java b/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/server/restful/RestTransactionTrackerTest.java new file mode 100644 index 000000000..2fa4ed532 --- /dev/null +++ b/controlloop/common/feature-controlloop-trans/src/test/java/org/onap/policy/drools/server/restful/RestTransactionTrackerTest.java @@ -0,0 +1,160 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.server.restful; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Collections; +import java.util.List; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Response; +import org.jetbrains.annotations.NotNull; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.Topic; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; +import org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; +import org.onap.policy.common.utils.network.NetworkUtil; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.controlloop.VirtualControlLoopNotification; +import org.onap.policy.controlloop.util.Serialization; +import org.onap.policy.drools.apps.controlloop.feature.trans.ControlLoopMetricsFeature; +import org.onap.policy.drools.apps.controlloop.feature.trans.ControlLoopMetricsManager; +import org.onap.policy.drools.persistence.SystemPersistenceConstants; +import org.onap.policy.drools.system.PolicyController; +import org.onap.policy.drools.system.PolicyEngineConstants; + +public class RestTransactionTrackerTest { + + private static PolicyController testController; + private static HttpClient client; + + @BeforeClass + public static void testBeforeClass() throws Exception { + SystemPersistenceConstants.getManager().setConfigurationDir("target/test-classes"); + + HttpServletServerFactoryInstance.getServerFactory().destroy(); + HttpClientFactoryInstance.getClientFactory().destroy(); + + HttpClientFactoryInstance.getClientFactory().build( + BusTopicParams.builder() + .clientName("trans") + .hostname("localhost") + .port(8769) + .basePath("policy/pdp/engine/controllers/transactions") + .managed(true) + .build()); + + HttpServletServer server = + HttpServletServerFactoryInstance + .getServerFactory() + .build("trans", "localhost", 8769, "/", true, true); + server.addServletClass("/*", RestTransactionTracker.class.getName()); + server.waitedStart(5000L); + assertTrue(NetworkUtil.isTcpPortOpen("localhost", 8769, 5, 10000L)); + + testController = PolicyEngineConstants.getManager().createPolicyController("metrics", + SystemPersistenceConstants.getManager().getControllerProperties("metrics")); + + client = HttpClientFactoryInstance.getClientFactory().get("trans"); + } + + @AfterClass + public static void testAfterClass() { + HttpClientFactoryInstance.getClientFactory().destroy(); + HttpServletServerFactoryInstance.getServerFactory().destroy(); + + SystemPersistenceConstants.getManager().setConfigurationDir(null); + } + + @Test + public void testConfiguration() { + equals(get("cacheSize", Response.Status.OK.getStatusCode()), Integer.class, 3); + equals(get("timeout", Response.Status.OK.getStatusCode()), Integer.class, 2); + + put("cacheSize/10", "", Response.Status.OK.getStatusCode()); + put("timeout/20", "", Response.Status.OK.getStatusCode()); + + equals(get("cacheSize", Response.Status.OK.getStatusCode()), Integer.class, 10); + equals(get("timeout", Response.Status.OK.getStatusCode()), Integer.class, 20); + + put("cacheSize/3", "", Response.Status.OK.getStatusCode()); + put("timeout/2", "", Response.Status.OK.getStatusCode()); + + equals(get("cacheSize", Response.Status.OK.getStatusCode()), Integer.class, 3); + equals(get("timeout", Response.Status.OK.getStatusCode()), Integer.class, 2); + } + + @Test + public void testTransactions() { + equals(get("/", Response.Status.OK.getStatusCode()), List.class, Collections.emptyList()); + + ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature(); + + assertTrue(HttpClient.getBody(get("/", Response.Status.OK.getStatusCode()), List.class).isEmpty()); + get("/664be3d2-6c12-4f4b-a3e7-c349acced200", Response.Status.NOT_FOUND.getStatusCode()); + + String activeNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-active.json"); + VirtualControlLoopNotification active = + Serialization.gsonPretty.fromJson(activeNotification, VirtualControlLoopNotification.class); + feature.beforeDeliver(testController, Topic.CommInfrastructure.DMAAP, "POLICY-CL-MGT", active); + assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size()); + + assertFalse(HttpClient.getBody(get("/", Response.Status.OK.getStatusCode()), List.class).isEmpty()); + notNull(get("/664be3d2-6c12-4f4b-a3e7-c349acced200", Response.Status.OK.getStatusCode()), String.class); + } + + private Response get(String contextPath, int statusCode) { + Response response = client.get(contextPath); + return checkResponse(statusCode, response); + } + + private Response put(String contextPath, String body, int statusCode) { + Response response = client.put(contextPath, Entity.json(body), Collections.emptyMap()); + return checkResponse(statusCode, response); + } + + private <T, Y> void equals(Response response, Class<T> clazz, Y expected) { + assertEquals(expected, HttpClient.getBody(response, clazz)); + } + + private <T> void notNull(Response response, Class<T> clazz) { + assertNotNull(HttpClient.getBody(response, clazz)); + } + + private <T> void empty(Response response, Class<T> clazz) { + assertNull(HttpClient.getBody(response, clazz)); + } + + @NotNull + private Response checkResponse(int statusCode, Response response) { + assertEquals(statusCode, response.getStatus()); + return response; + } +}
\ No newline at end of file |