2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.controlloop.actorserviceprovider.controlloop;
23 import java.io.Serializable;
25 import java.util.UUID;
26 import java.util.concurrent.CompletableFuture;
27 import java.util.concurrent.ConcurrentHashMap;
28 import lombok.AccessLevel;
30 import lombok.NonNull;
32 import org.onap.policy.controlloop.VirtualControlLoopEvent;
33 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
37 * Context associated with a control loop event.
41 public class ControlLoopEventContext implements Serializable {
42 private static final long serialVersionUID = 1L;
45 private final VirtualControlLoopEvent event;
48 * Enrichment data extracted from the event. Never {@code null}, though it may be
51 private final Map<String, String> enrichment;
54 * Set of properties that have been stored in the context.
56 @Getter(AccessLevel.NONE)
57 @Setter(AccessLevel.NONE)
58 private Map<String, Serializable> properties = new ConcurrentHashMap<>();
61 * When {@link #obtain(String, ControlLoopOperationParams)} is invoked and the
62 * specified property is not found in {@link #properties}, it is retrieved. This holds
63 * the futures for the operations retrieving the properties.
65 @Getter(AccessLevel.NONE)
66 @Setter(AccessLevel.NONE)
67 private transient Map<String, CompletableFuture<OperationOutcome>> retrievers = new ConcurrentHashMap<>();
70 * Request ID extracted from the event, or a generated value if the event has no
71 * request id; never {@code null}.
73 private final UUID requestId;
77 * Constructs the object.
79 * @param event event with which this is associated
81 public ControlLoopEventContext(@NonNull VirtualControlLoopEvent event) {
83 this.requestId = (event.getRequestId() != null ? event.getRequestId() : UUID.randomUUID());
84 this.enrichment = (event.getAai() != null ? event.getAai() : Map.of());
88 * Determines if the context contains a property.
90 * @param name name of the property of interest
91 * @return {@code true} if the context contains the property, {@code false} otherwise
93 public boolean contains(String name) {
94 return properties.containsKey(name);
98 * Gets a property, casting it to the desired type.
100 * @param <T> desired type
101 * @param name name of the property whose value is to be retrieved
102 * @return the property's value, or {@code null} if it does not yet have a value
104 @SuppressWarnings("unchecked")
105 public <T> T getProperty(String name) {
106 return (T) properties.get(name);
110 * Sets a property's value.
112 * @param name property name
113 * @param value new property value
115 public void setProperty(String name, Serializable value) {
116 properties.put(name, value);
120 * Obtains the given property.
122 * @param name name of the desired property
123 * @param params parameters needed to perform the operation to retrieve the desired
125 * @return a future for retrieving the property, {@code null} if the property has
126 * already been retrieved
128 public CompletableFuture<OperationOutcome> obtain(String name, ControlLoopOperationParams params) {
129 if (properties.containsKey(name)) {
134 * Return any existing future, if it wasn't canceled. Otherwise, start a new
139 CompletableFuture<OperationOutcome> oldFuture =
140 retrievers.computeIfPresent(name, (key, future) -> future.isCancelled() ? null : future);
143 if (oldFuture != null) {
148 * Note: must NOT invoke params.start() within retrievers.compute(), as start()
149 * may invoke obtain() which would cause a recursive update to the retrievers map.
151 CompletableFuture<OperationOutcome> future = params.start();
153 if ((oldFuture = retrievers.putIfAbsent(name, future)) != null) {
154 future.cancel(false);