2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.apps.controlloop.feature.trans;
23 import com.google.common.cache.CacheBuilder;
24 import com.google.common.cache.CacheLoader;
25 import com.google.common.cache.LoadingCache;
26 import com.google.common.cache.RemovalListener;
27 import java.time.Instant;
28 import java.time.ZonedDateTime;
29 import java.util.ArrayList;
30 import java.util.List;
32 import java.util.Objects;
33 import java.util.UUID;
34 import java.util.concurrent.TimeUnit;
37 import org.apache.commons.collections4.CollectionUtils;
38 import org.onap.policy.controlloop.ControlLoopNotificationType;
39 import org.onap.policy.controlloop.ControlLoopOperation;
40 import org.onap.policy.controlloop.VirtualControlLoopNotification;
41 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
42 import org.onap.policy.drools.system.PolicyController;
43 import org.onap.policy.drools.system.PolicyEngineConstants;
44 import org.onap.policy.drools.utils.logging.MdcTransaction;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Control Loop Metrics Tracker Implementation.
51 class CacheBasedControlLoopMetricsManager implements ControlLoopMetrics {
53 private static final String UNEXPECTED_NOTIFICATION_TYPE = "unexpected notification type {} in notification {}";
55 private static final Logger logger = LoggerFactory.getLogger(CacheBasedControlLoopMetricsManager.class);
57 private LoadingCache<UUID, VirtualControlLoopNotification> cache;
60 private long cacheSize = ControlLoopMetricsFeature.CL_CACHE_TRANS_SIZE_DEFAULT;
64 private long transactionTimeout = ControlLoopMetricsFeature.CL_CACHE_TRANS_TIMEOUT_SECONDS_DEFAULT;
67 * Numeric response code.
69 private static final Map<String, String> note2code = Map.of(
70 ControlLoopNotificationType.ACTIVE.name(), "100",
71 ControlLoopNotificationType.REJECTED.name(), "200",
72 ControlLoopNotificationType.OPERATION.name(), "300",
73 ControlLoopNotificationType.OPERATION_SUCCESS.name(), "301",
74 ControlLoopNotificationType.OPERATION_FAILURE.name(), "302",
75 ControlLoopNotificationType.FINAL_FAILURE.name(), "400",
76 ControlLoopNotificationType.FINAL_SUCCESS.name(), "401",
77 ControlLoopNotificationType.FINAL_OPENLOOP.name(), "402"
80 private static final String UNKNOWN_RESPONSE_CODE = "900";
82 public CacheBasedControlLoopMetricsManager() {
84 var properties = SystemPersistenceConstants.getManager()
85 .getProperties(ControlLoopMetricsFeature.CONFIGURATION_PROPERTIES_NAME);
91 Long.parseLong(properties.getProperty(ControlLoopMetricsFeature.CL_CACHE_TRANS_SIZE_PROPERTY,
92 "" + ControlLoopMetricsFeature.CL_CACHE_TRANS_SIZE_DEFAULT));
93 } catch (Exception e) {
94 logger.warn("{}:{} property cannot be accessed", ControlLoopMetricsFeature.CONFIGURATION_PROPERTIES_NAME,
95 ControlLoopMetricsFeature.CL_CACHE_TRANS_SIZE_PROPERTY, e);
98 /* transaction timeout */
101 this.transactionTimeout = Long
102 .parseLong(properties.getProperty(ControlLoopMetricsFeature.CL_CACHE_TRANS_TIMEOUT_SECONDS_PROPERTY,
103 "" + ControlLoopMetricsFeature.CL_CACHE_TRANS_TIMEOUT_SECONDS_DEFAULT));
104 } catch (Exception e) {
105 logger.warn("{}:{} property cannot be accessed", ControlLoopMetricsFeature.CONFIGURATION_PROPERTIES_NAME,
106 ControlLoopMetricsFeature.CL_CACHE_TRANS_TIMEOUT_SECONDS_PROPERTY, e);
109 resetCache(this.cacheSize, this.transactionTimeout);
113 public void resetCache(long cacheSize, long transactionTimeout) {
114 this.cacheSize = cacheSize;
115 this.transactionTimeout = transactionTimeout;
117 CacheLoader<UUID, VirtualControlLoopNotification> loader = new CacheLoader<>() {
120 public VirtualControlLoopNotification load(UUID key) {
125 RemovalListener<UUID, VirtualControlLoopNotification> listener = notification -> {
126 if (notification.wasEvicted()) {
127 evicted(notification.getValue());
128 } else if (logger.isInfoEnabled()) {
129 logger.info("REMOVAL: {} because of {}", notification.getValue().getRequestId(),
130 notification.getCause().name());
134 synchronized (this) {
135 if (this.cache != null) {
136 this.cache.cleanUp();
137 this.cache.invalidateAll();
140 this.cache = CacheBuilder.newBuilder().maximumSize(this.cacheSize)
141 .expireAfterWrite(transactionTimeout, TimeUnit.SECONDS).removalListener(listener).build(loader);
146 public void refresh() {
147 this.cache.cleanUp();
151 public List<UUID> getTransactionIds() {
152 return new ArrayList<>(this.cache.asMap().keySet());
156 public List<VirtualControlLoopNotification> getTransactions() {
157 return new ArrayList<>(this.cache.asMap().values());
161 public void transactionEvent(PolicyController controller, VirtualControlLoopNotification notification) {
162 if (!isNotificationValid(notification)) {
166 setNotificationValues(controller, notification);
168 switch (notification.getNotification()) {
173 endTransaction(controller, notification);
177 case OPERATION_SUCCESS:
178 case OPERATION_FAILURE:
179 /* any other value is an in progress transaction */
180 inProgressTransaction(notification);
184 logger.warn(UNEXPECTED_NOTIFICATION_TYPE,
185 notification.getNotification(), notification);
190 private boolean isNotificationValid(VirtualControlLoopNotification notification) {
191 if (notification == null || notification.getRequestId() == null || notification.getNotification() == null) {
192 logger.warn("Invalid notification: {}", notification);
199 private void setNotificationValues(PolicyController controller, VirtualControlLoopNotification notification) {
200 if (notification.getNotificationTime() == null) {
201 notification.setNotificationTime(ZonedDateTime.now());
204 notification.setFrom(notification.getFrom() + ":" + controller.getName()
205 + ":" + controller.getDrools().getCanonicalSessionNames());
209 public VirtualControlLoopNotification getTransaction(UUID requestId) {
210 return cache.getIfPresent(requestId);
214 public void removeTransaction(UUID requestId) {
215 cache.invalidate(requestId);
219 * Tracks an in progress control loop transaction.
221 * @param notification control loop notification
223 protected void inProgressTransaction(VirtualControlLoopNotification notification) {
224 if (cache.getIfPresent(notification.getRequestId()) == null) {
225 cache.put(notification.getRequestId(), notification);
228 this.metric(notification);
232 * End of a control loop transaction.
234 * @param controller controller
235 * @param notification control loop notification
237 protected void endTransaction(PolicyController controller, VirtualControlLoopNotification notification) {
238 ZonedDateTime startTime;
239 VirtualControlLoopNotification startNotification = cache.getIfPresent(notification.getRequestId());
240 startTime = Objects.requireNonNullElse(startNotification, notification).getNotificationTime();
242 this.transaction(controller, notification, startTime);
243 if (startNotification != null) {
244 removeTransaction(startNotification.getRequestId());
248 protected void evicted(VirtualControlLoopNotification notification) {
250 .newTransaction(notification.getRequestId().toString(), notification.getFrom())
251 .setServiceName(notification.getClosedLoopControlName())
252 .setServiceInstanceId(notification.getPolicyName() + ":" + notification.getPolicyVersion())
253 .setProcessKey("" + notification.getAai())
254 .setTargetEntity(notification.getTargetType() + "." + notification.getTarget())
255 .setResponseCode(UNKNOWN_RESPONSE_CODE)
256 .setStartTime(notification.getNotificationTime().toInstant())
257 .setEndTime(Instant.now())
258 .setResponseDescription("EVICTED")
259 .setStatusCode(false)
260 .setCustomField1((notification.getNotification() != null)
261 ? notification.getNotification().name() : "")
262 .setCustomField2(notification.getPolicyScope())
263 .setClientIpAddress(notification.getClosedLoopEventClient())
270 public void setMaxCacheSize(long cacheSize) {
271 this.cacheSize = cacheSize;
275 public long getCacheOccupancy() {
276 return this.cache.size();
279 protected void metric(VirtualControlLoopNotification notification) {
280 var trans = getMdcTransaction(notification);
281 List<ControlLoopOperation> operations = notification.getHistory();
282 switch (notification.getNotification()) {
284 trans.setStatusCode(true).metric().resetTransaction();
287 operation(trans.setStatusCode(true), operations).metric().resetTransaction();
289 case OPERATION_SUCCESS:
290 operation(trans.setStatusCode(true), operations).metric().transaction().resetTransaction();
292 case OPERATION_FAILURE:
293 operation(trans.setStatusCode(false), operations).metric().transaction().resetTransaction();
297 logger.warn(UNEXPECTED_NOTIFICATION_TYPE,
298 notification.getNotification(), notification);
303 private MdcTransaction getMdcTransaction(VirtualControlLoopNotification notification) {
304 return MdcTransaction
305 .newTransaction(notification.getRequestId().toString(), notification.getFrom())
306 .setServiceName(notification.getClosedLoopControlName())
307 .setServiceInstanceId(notification.getPolicyName() + ":" + notification.getPolicyVersion())
308 .setProcessKey("" + notification.getAai())
309 .setTargetEntity(notification.getTargetType() + "." + notification.getTarget())
310 .setResponseCode((notification.getNotification() != null)
311 ? notificationTypeToResponseCode(notification.getNotification().name())
312 : UNKNOWN_RESPONSE_CODE)
313 .setCustomField1((notification.getNotification() != null)
314 ? notification.getNotification().name() : "")
315 .setCustomField2(notification.getPolicyScope())
316 .setResponseDescription(notification.getMessage())
317 .setClientIpAddress(notification.getClosedLoopEventClient());
320 protected MdcTransaction operation(MdcTransaction trans, List<ControlLoopOperation> operations) {
321 if (CollectionUtils.isEmpty(operations)) {
325 ControlLoopOperation operation = operations.get(operations.size() - 1);
327 if (operation.getActor() != null) {
328 trans.setTargetServiceName(operation.getActor() + "." + operation.getOperation());
331 if (operation.getTarget() != null) {
332 trans.setTargetVirtualEntity(operation.getTarget());
335 if (operation.getSubRequestId() != null) {
336 trans.setInvocationId(operation.getSubRequestId());
339 if (operation.getOutcome() != null) {
340 trans.setResponseDescription(operation.getOutcome() + ":" + operation.getMessage());
343 if (operation.getStart() != null) {
344 trans.setStartTime(operation.getStart());
347 if (operation.getEnd() != null) {
348 trans.setEndTime(operation.getEnd());
354 protected void transaction(PolicyController controller,
355 VirtualControlLoopNotification notification, ZonedDateTime startTime) {
356 MdcTransaction trans = getMdcTransaction(notification)
357 .setStartTime(startTime.toInstant())
358 .setEndTime(notification.getNotificationTime().toInstant());
360 switch (notification.getNotification()) {
364 trans.setStatusCode(true);
369 trans.setStatusCode(false);
373 logger.warn(UNEXPECTED_NOTIFICATION_TYPE,
374 notification.getNotification(), notification);
379 PolicyEngineConstants.getManager().transaction(controller.getName(),
380 notification.getClosedLoopControlName(), trans.getMetric());
381 } catch (RuntimeException rex) {
382 logger.info("error pegging control loop transaction: {}", trans.getMetric(), rex);
385 trans.transaction().resetTransaction();
389 public String toString() {
390 return "CacheBasedControlLoopMetricsManager{" + "cacheSize=" + cacheSize
391 + ",transactionTimeout="
394 + getCacheOccupancy()
398 private String notificationTypeToResponseCode(String notificationType) {
399 String code = note2code.get(notificationType);
400 return Objects.requireNonNullElse(code, UNKNOWN_RESPONSE_CODE);