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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/*-
* ============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.parameters;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
import java.util.function.Consumer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.onap.policy.common.parameters.BeanValidationResult;
import org.onap.policy.common.parameters.BeanValidator;
import org.onap.policy.common.parameters.annotations.NotNull;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
import org.onap.policy.controlloop.actorserviceprovider.ActorService;
import org.onap.policy.controlloop.actorserviceprovider.Operation;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.Util;
import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
import org.onap.policy.controlloop.policy.Target;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Parameters for control loop operations. The executor defaults to
* {@link ForkJoinPool#commonPool()}, but may be overridden.
*/
@Getter
@Builder(toBuilder = true)
@AllArgsConstructor
@EqualsAndHashCode
public class ControlLoopOperationParams {
private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationParams.class);
/**
* Actor name.
*/
@NotNull
private String actor;
/**
* Actor service in which to find the actor/operation.
*/
@NotNull
private ActorService actorService;
/**
* Event for which the operation applies.
*/
// TODO to be removed
private ControlLoopEventContext context;
/**
* If {@code null}, this value is extracted from the context.
*/
private UUID requestId;
/**
* Executor to use to run the operation.
*/
@NotNull
@Builder.Default
private Executor executor = ForkJoinPool.commonPool();
/**
* Operation name.
*/
@NotNull
private String operation;
/**
* Payload data for the request.
*/
private Map<String, Object> payload;
/**
* {@code True} if the preprocessing steps have already been executed, {@code false}
* otherwise.
*/
private boolean preprocessed;
/**
* Number of retries allowed, or {@code null} if no retries.
*/
private Integer retry;
/**
* The Target information, extracted from the Policy. May be {@code null}, depending
* on the requirement of the operation to be invoked.
*/
private Target target;
/**
* Target entity.
*/
// TODO to be removed
private String targetEntity;
/**
* Timeout, in seconds, or {@code null} if no timeout. Zero and negative values also
* imply no timeout.
*/
@Builder.Default
private Integer timeoutSec = 300;
/**
* The function to invoke when the operation starts. This is optional.
* <p/>
* Note: this may be invoked multiple times, but with different actor/operations. That
* may happen if the current operation requires other operations to be performed first
* (e.g., A&AI queries, guard checks).
*/
private Consumer<OperationOutcome> startCallback;
/**
* The function to invoke when the operation completes. This is optional.
* <p/>
* Note: this may be invoked multiple times, but with different actor/operations. That
* may happen if the current operation requires other operations to be performed first
* (e.g., A&AI queries, guard checks).
*/
private Consumer<OperationOutcome> completeCallback;
/**
* Starts the specified operation.
*
* @return a future that will return the result of the operation
* @throws IllegalArgumentException if the parameters are invalid
*/
public CompletableFuture<OperationOutcome> start() {
return build().start();
}
/**
* Builds the specified operation.
*
* @return a new operation
* @throws IllegalArgumentException if the parameters are invalid
*/
public Operation build() {
BeanValidationResult result = validate();
if (!result.isValid()) {
logger.warn("parameter error in operation {}.{} for {}:\n{}", getActor(), getOperation(), getRequestId(),
result.getResult());
throw new IllegalArgumentException("invalid parameters");
}
// @formatter:off
return actorService
.getActor(getActor())
.getOperator(getOperation())
.buildOperation(this);
// @formatter:on
}
/**
* Gets the requested ID of the associated event.
*
* @return the event's request ID, or {@code null} if no request ID is available
*/
public UUID getRequestId() {
if (requestId == null && context != null && context.getEvent() != null) {
// cache the request ID
requestId = context.getEvent().getRequestId();
}
return requestId;
}
/**
* Makes an operation outcome, populating it from the parameters.
*
* @return a new operation outcome
*/
// TODO to be removed
public OperationOutcome makeOutcome() {
return makeOutcome(getTargetEntity());
}
/**
* Makes an operation outcome, populating it from the parameters.
*
* @param targetEntity the target entity
*
* @return a new operation outcome
*/
public OperationOutcome makeOutcome(String targetEntity) {
OperationOutcome outcome = new OperationOutcome();
outcome.setActor(getActor());
outcome.setOperation(getOperation());
outcome.setTarget(targetEntity);
return outcome;
}
/**
* Invokes the callback to indicate that the operation has started. Any exceptions
* generated by the callback are logged, but not re-thrown.
*
* @param operation the operation that is being started
*/
public void callbackStarted(OperationOutcome operation) {
logger.info("started operation {}.{} for {}", operation.getActor(), operation.getOperation(), getRequestId());
if (startCallback != null) {
Util.runFunction(() -> startCallback.accept(operation), "{}.{}: start-callback threw an exception for {}",
operation.getActor(), operation.getOperation(), getRequestId());
}
}
/**
* Invokes the callback to indicate that the operation has completed. Any exceptions
* generated by the callback are logged, but not re-thrown.
*
* @param operation the operation that is being started
*/
public void callbackCompleted(OperationOutcome operation) {
logger.info("completed operation {}.{} outcome={} for {}", operation.getActor(), operation.getOperation(),
operation.getResult(), getRequestId());
if (completeCallback != null) {
Util.runFunction(() -> completeCallback.accept(operation),
"{}.{}: complete-callback threw an exception for {}", operation.getActor(),
operation.getOperation(), getRequestId());
}
}
/**
* Validates the parameters.
*
* @return the validation result
*/
public BeanValidationResult validate() {
BeanValidationResult result =
new BeanValidator().validateTop(ControlLoopOperationParams.class.getSimpleName(), this);
// validate that we have a request ID, or that we can get it from the context's
// event
if (context == null) {
// no context specified - invoker must provide a request ID then
result.validateNotNull("requestId", requestId);
} else if (requestId == null) {
// have a context, but no request ID - check the context's event for the
// request ID
BeanValidationResult contextResult = new BeanValidationResult("context", context);
VirtualControlLoopEvent event = context.getEvent();
contextResult.validateNotNull("event", event);
if (event != null) {
// cache the request id for later use
BeanValidationResult eventResult = new BeanValidationResult("event", event);
eventResult.validateNotNull("requestId", event.getRequestId());
contextResult.addResult(eventResult);
}
result.addResult(contextResult);
}
return result;
}
}
|