2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023-2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.apps.controlloop.feature.trans;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertNull;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import java.nio.file.Path;
33 import java.util.UUID;
34 import java.util.concurrent.TimeUnit;
35 import org.junit.jupiter.api.AfterAll;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.controlloop.ControlLoopNotificationType;
42 import org.onap.policy.controlloop.VirtualControlLoopNotification;
43 import org.onap.policy.controlloop.util.Serialization;
44 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
45 import org.onap.policy.drools.system.PolicyController;
46 import org.onap.policy.drools.system.PolicyEngineConstants;
49 * ControlLoopMetrics Tests.
51 class ControlLoopMetricsFeatureTest {
53 private static final String POLICY_CL_MGT = "POLICY-CL-MGT";
54 private static final Path configPath = SystemPersistenceConstants.getManager().getConfigurationPath();
55 private static PolicyController testController;
57 private static void resetStats() {
58 PolicyEngineConstants.getManager().getStats().getGroupStat().setAverageExecutionTime(0d);
59 PolicyEngineConstants.getManager().getStats().getGroupStat().setLastExecutionTime(0L);
60 PolicyEngineConstants.getManager().getStats().getGroupStat().setLastStart(0L);
61 PolicyEngineConstants.getManager().getStats().getGroupStat().setPolicyExecutedCount(0L);
62 PolicyEngineConstants.getManager().getStats().getGroupStat().setPolicyExecutedFailCount(0L);
63 PolicyEngineConstants.getManager().getStats().getGroupStat().setPolicyExecutedSuccessCount(0L);
64 PolicyEngineConstants.getManager().getStats().getGroupStat().setTotalElapsedTime(0d);
71 public static void setUp() {
72 SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
73 testController = PolicyEngineConstants.getManager().createPolicyController("metrics",
74 SystemPersistenceConstants.getManager().getControllerProperties("metrics"));
78 public static void tearDown() {
79 SystemPersistenceConstants.getManager().setConfigurationDir(configPath.toString());
84 public void beforeTest() {
89 void testCacheDefaults() {
90 assertEquals(3, ControlLoopMetricsManager.getManager().getCacheSize());
91 assertEquals(2, ControlLoopMetricsManager.getManager().getTransactionTimeout());
92 assertEquals(0, ControlLoopMetricsManager.getManager().getCacheOccupancy());
96 void testInvalidNotifications() {
97 var feature = new ControlLoopMetricsFeature();
98 var notification = new VirtualControlLoopNotification();
99 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, notification);
100 this.testCacheDefaults();
102 var requestId = UUID.randomUUID();
103 notification.setRequestId(requestId);
105 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, notification);
106 assertNull(ControlLoopMetricsManager.getManager().getTransaction(requestId));
107 this.testCacheDefaults();
111 void testValidActiveNotification() {
112 var feature = new ControlLoopMetricsFeature();
113 var notification = new VirtualControlLoopNotification();
114 var requestId = UUID.randomUUID();
115 notification.setRequestId(requestId);
116 notification.setNotification(ControlLoopNotificationType.ACTIVE);
118 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, notification);
119 assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(requestId));
120 assertTrue(ControlLoopMetricsManager.getManager().getTransaction(requestId).getFrom()
121 .contains(testController.getName()));
122 assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(requestId).getNotificationTime());
123 assertEquals(1, ControlLoopMetricsManager.getManager().getCacheOccupancy());
125 /* wait for the entries to expire */
126 await().atMost(ControlLoopMetricsManager.getManager().getTransactionTimeout() + 1, TimeUnit.SECONDS)
127 .until(() -> ControlLoopMetricsManager.getManager().getTransaction(requestId) == null);
129 this.testCacheDefaults();
134 var notification = this.generateNotification();
135 new ControlLoopMetricsFeature().beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT,
138 assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
140 ControlLoopMetricsManager.getManager().resetCache(ControlLoopMetricsManager.getManager().getCacheSize(),
141 ControlLoopMetricsManager.getManager().getTransactionTimeout());
142 assertNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
143 this.testCacheDefaults();
147 void testRemoveTransaction() {
148 var notification = this.generateNotification();
149 assertNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
150 ControlLoopMetricsManager.getManager().removeTransaction(notification.getRequestId());
152 ControlLoopMetricsManager.getManager().transactionEvent(testController, notification);
153 assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
154 ControlLoopMetricsManager.getManager().removeTransaction(notification.getRequestId());
155 assertNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
159 void testEviction() {
160 var feature = new ControlLoopMetricsFeature();
161 for (int i = 0; i < ControlLoopMetricsManager.getManager().getCacheSize(); i++) {
162 var notification = generateNotification();
163 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, notification);
164 assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(notification.getRequestId()));
167 assertEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(),
168 ControlLoopMetricsManager.getManager().getCacheOccupancy());
170 var overflowNotification = generateNotification();
171 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, overflowNotification);
172 assertEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(),
173 ControlLoopMetricsManager.getManager().getCacheOccupancy());
174 assertNotNull(ControlLoopMetricsManager.getManager().getTransaction(overflowNotification.getRequestId()));
175 assertEquals(ControlLoopMetricsManager.getManager().getTransactionIds().size(),
176 ControlLoopMetricsManager.getManager().getCacheSize());
177 assertEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(),
178 ControlLoopMetricsManager.getManager().getCacheSize());
179 assertFalse(ControlLoopMetricsManager.getManager().getTransactionIds().isEmpty());
180 assertFalse(ControlLoopMetricsManager.getManager().getTransactions().isEmpty());
182 /* wait for the entries to expire */
183 await().atMost(ControlLoopMetricsManager.getManager().getTransactionTimeout() + 1, TimeUnit.SECONDS)
184 .until(() -> ControlLoopMetricsManager.getManager().getTransactions().isEmpty());
186 ControlLoopMetricsManager.getManager().refresh();
187 assertEquals(ControlLoopMetricsManager.getManager().getTransactionIds().size(),
188 ControlLoopMetricsManager.getManager().getCacheOccupancy());
189 assertNotEquals(ControlLoopMetricsManager.getManager().getCacheOccupancy(),
190 ControlLoopMetricsManager.getManager().getCacheSize());
191 assertTrue(ControlLoopMetricsManager.getManager().getTransactionIds().isEmpty());
192 assertTrue(ControlLoopMetricsManager.getManager().getTransactions().isEmpty());
194 this.testCacheDefaults();
197 private VirtualControlLoopNotification generateNotification() {
198 var notification = new VirtualControlLoopNotification();
199 var requestId = UUID.randomUUID();
200 notification.setRequestId(requestId);
201 notification.setNotification(ControlLoopNotificationType.ACTIVE);
206 void getSequenceNumber() {
207 var feature = new ControlLoopMetricsFeature();
208 assertEquals(ControlLoopMetricsFeature.FEATURE_SEQUENCE_PRIORITY, feature.getSequenceNumber());
212 void testSuccessControlLoop() {
213 var feature = new ControlLoopMetricsFeature();
215 var activeNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-active.json");
217 Serialization.gsonPretty.fromJson(activeNotification, VirtualControlLoopNotification.class);
218 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, active);
219 assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
221 var opStartNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-operation.json");
223 Serialization.gsonPretty.fromJson(opStartNotification, VirtualControlLoopNotification.class);
224 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, opStart);
225 assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
227 var permitNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-permit.json");
229 Serialization.gsonPretty.fromJson(permitNotification, VirtualControlLoopNotification.class);
230 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, permit);
231 assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
233 var restartNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-restart.json");
235 Serialization.gsonPretty.fromJson(restartNotification, VirtualControlLoopNotification.class);
236 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, restart);
237 assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
239 var restartSuccessNotification =
240 ResourceUtils.getResourceAsString("policy-cl-mgt-restart-success.json");
242 Serialization.gsonPretty.fromJson(restartSuccessNotification, VirtualControlLoopNotification.class);
243 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, restartSuccess);
244 assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
246 var finalSuccessNotification =
247 ResourceUtils.getResourceAsString("policy-cl-mgt-final-success.json");
249 Serialization.gsonPretty.fromJson(finalSuccessNotification, VirtualControlLoopNotification.class);
250 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, finalSuccess);
251 assertEquals(0, ControlLoopMetricsManager.getManager().getTransactionIds().size());
253 PolicyEngineConstants.getManager().getStats().getGroupStat().getPolicyExecutedSuccessCount());
255 PolicyEngineConstants.getManager().getStats().getGroupStat().getPolicyExecutedFailCount());
256 assertEquals(1, PolicyEngineConstants.getManager().getStats().getGroupStat().getPolicyExecutedCount());
257 assertEquals(1587409937684L,
258 PolicyEngineConstants.getManager().getStats().getGroupStat().getLastExecutionTime());
260 PolicyEngineConstants.getManager().getStats().getGroupStat().getAverageExecutionTime(), 0.0d);
261 assertEquals(1587409937223L,
262 PolicyEngineConstants.getManager().getStats().getGroupStat().getLastStart());
264 PolicyEngineConstants.getManager().getStats().getGroupStat().getTotalElapsedTime(), 0.0d);
268 void testUntrackedNotifications() throws InterruptedException {
269 var feature = new ControlLoopMetricsFeature();
271 var finalSuccessNotification =
272 ResourceUtils.getResourceAsString("policy-cl-mgt-final-success.json");
274 Serialization.gsonPretty.fromJson(finalSuccessNotification, VirtualControlLoopNotification.class);
275 finalSuccess.setRequestId(UUID.randomUUID());
276 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, finalSuccess);
277 assertEquals(0, ControlLoopMetricsManager.getManager().getTransactionIds().size());
279 var opStartNotification =
280 ResourceUtils.getResourceAsString("policy-cl-mgt-operation.json");
282 Serialization.gsonPretty.fromJson(opStartNotification, VirtualControlLoopNotification.class);
283 feature.beforeDeliver(testController, CommInfrastructure.NOOP, POLICY_CL_MGT, opStart);
284 assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
286 Thread.sleep((ControlLoopMetricsManager.getManager().getTransactionTimeout() + 1) * 1000L); // NOSONAR
287 assertEquals(0, ControlLoopMetricsManager.getManager().getTransactionIds().size());