aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/controlloop/ControlLoopEventContext.java
blob: 8099ea7c2da8f0a8c5803640b94f180d3579948b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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.controlloop.actorserviceprovider.controlloop;

import java.io.Serializable;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;

/**
 * Context associated with a control loop event.
 */
@Getter
@Setter
public class ControlLoopEventContext implements Serializable {
    private static final long serialVersionUID = 1L;


    private final VirtualControlLoopEvent event;

    /**
     * Enrichment data extracted from the event. Never {@code null}, though it may be
     * immutable.
     */
    private final Map<String, String> enrichment;

    /**
     * Set of properties that have been stored in the context.
     */
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private Map<String, Serializable> properties = new ConcurrentHashMap<>();

    /**
     * When {@link #obtain(String, ControlLoopOperationParams)} is invoked and the
     * specified property is not found in {@link #properties}, it is retrieved. This holds
     * the futures for the operations retrieving the properties.
     */
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private transient Map<String, CompletableFuture<OperationOutcome>> retrievers = new ConcurrentHashMap<>();

    /**
     * Request ID extracted from the event, or a generated value if the event has no
     * request id; never {@code null}.
     */
    private final UUID requestId;


    /**
     * Constructs the object.
     *
     * @param event event with which this is associated
     */
    public ControlLoopEventContext(@NonNull VirtualControlLoopEvent event) {
        this.event = event;
        this.requestId = (event.getRequestId() != null ? event.getRequestId() : UUID.randomUUID());
        this.enrichment = (event.getAai() != null ? event.getAai() : Map.of());
    }

    /**
     * Determines if the context contains a property.
     *
     * @param name name of the property of interest
     * @return {@code true} if the context contains the property, {@code false} otherwise
     */
    public boolean contains(String name) {
        return properties.containsKey(name);
    }

    /**
     * Gets a property, casting it to the desired type.
     *
     * @param <T> desired type
     * @param name name of the property whose value is to be retrieved
     * @return the property's value, or {@code null} if it does not yet have a value
     */
    @SuppressWarnings("unchecked")
    public <T> T getProperty(String name) {
        return (T) properties.get(name);
    }

    /**
     * Sets a property's value.
     *
     * @param name property name
     * @param value new property value
     */
    public void setProperty(String name, Serializable value) {
        properties.put(name, value);
    }

    /**
     * Obtains the given property.
     *
     * @param name name of the desired property
     * @param params parameters needed to perform the operation to retrieve the desired
     *        property
     * @return a future for retrieving the property, {@code null} if the property has
     *         already been retrieved
     */
    public CompletableFuture<OperationOutcome> obtain(String name, ControlLoopOperationParams params) {
        if (properties.containsKey(name)) {
            return null;
        }

        /*
         * Return any existing future, if it wasn't canceled. Otherwise, start a new
         * request.
         */

        // @formatter:off
        CompletableFuture<OperationOutcome> oldFuture =
            retrievers.computeIfPresent(name, (key, future) -> future.isCancelled() ? null : future);
        // @formatter:on

        if (oldFuture != null) {
            return oldFuture;
        }

        /*
         * Note: must NOT invoke params.start() within retrievers.compute(), as start()
         * may invoke obtain() which would cause a recursive update to the retrievers map.
         */
        CompletableFuture<OperationOutcome> future = params.start();

        if ((oldFuture = retrievers.putIfAbsent(name, future)) != null) {
            future.cancel(false);
            return oldFuture;
        }

        return future;
    }
}