2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.parameters;
24 import java.util.UUID;
25 import java.util.concurrent.CompletableFuture;
26 import java.util.concurrent.Executor;
27 import java.util.concurrent.ForkJoinPool;
28 import java.util.function.Consumer;
29 import lombok.AllArgsConstructor;
30 import lombok.Builder;
31 import lombok.EqualsAndHashCode;
33 import org.onap.policy.common.parameters.BeanValidationResult;
34 import org.onap.policy.common.parameters.BeanValidator;
35 import org.onap.policy.common.parameters.annotations.NotNull;
36 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
37 import org.onap.policy.controlloop.actorserviceprovider.Operation;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
39 import org.onap.policy.controlloop.actorserviceprovider.TargetType;
40 import org.onap.policy.controlloop.actorserviceprovider.Util;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Parameters for control loop operations. The executor defaults to
46 * {@link ForkJoinPool#commonPool()}, but may be overridden.
49 @Builder(toBuilder = true)
52 public class ControlLoopOperationParams {
53 private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationParams.class);
56 * Optional keys within the "targetEntityIds" map.
58 public static final String PARAMS_ENTITY_RESOURCEID = "resourceID";
59 public static final String PARAMS_ENTITY_MODEL_INVARIANT_ID = "modelInvariantId";
60 public static final String PARAMS_ENTITY_MODEL_VERSION_ID = "modelVersionId";
61 public static final String PARAMS_ENTITY_MODEL_NAME = "modelName";
62 public static final String PARAMS_ENTITY_MODEL_VERSION = "modelVersion";
63 public static final String PARAMS_ENTITY_MODEL_CUSTOMIZATION_ID = "modelCustomizationId";
72 * Actor service in which to find the actor/operation.
75 private ActorService actorService;
78 * Request ID with which all actor operations are associated. Used to track requests
79 * across various components/servers.
83 private UUID requestId;
86 * Executor to use to run the operation.
90 private Executor executor = ForkJoinPool.commonPool();
96 private String operation;
99 * Payload data for the request.
101 private Map<String, Object> payload;
104 * Number of retries allowed, or {@code null} if no retries.
106 private Integer retry;
109 * The Target Type information, extracted from the Policy. May be {@code null}, depending
110 * on the requirement of the operation to be invoked.
112 private TargetType targetType;
115 * Target entitiy ids, extracted from the Policy. May be (@code null}, depending on
116 * the requirement of the operation to be invoked.
118 private Map<String, String> targetEntityIds;
121 * Timeout, in seconds, or {@code null} if no timeout. Zero and negative values also
125 private Integer timeoutSec = 300;
128 * The function to invoke when the operation starts. This is optional.
130 * Note: this may be invoked multiple times, but with different actor/operations. That
131 * may happen if the current operation requires other operations to be performed first
132 * (e.g., A&AI queries, guard checks).
134 private Consumer<OperationOutcome> startCallback;
137 * The function to invoke when the operation completes. This is optional.
139 * Note: this may be invoked multiple times, but with different actor/operations. That
140 * may happen if the current operation requires other operations to be performed first
141 * (e.g., A&AI queries, guard checks).
143 private Consumer<OperationOutcome> completeCallback;
146 * Starts the specified operation.
148 * @return a future that will return the result of the operation
149 * @throws IllegalArgumentException if the parameters are invalid
151 public CompletableFuture<OperationOutcome> start() {
152 return build().start();
156 * Builds the specified operation.
158 * @return a new operation
159 * @throws IllegalArgumentException if the parameters are invalid
161 public Operation build() {
162 BeanValidationResult result = validate();
163 if (!result.isValid()) {
164 logger.warn("parameter error in operation {}.{} for {}:\n{}", getActor(), getOperation(), getRequestId(),
166 throw new IllegalArgumentException("invalid parameters");
171 .getActor(getActor())
172 .getOperator(getOperation())
173 .buildOperation(this);
178 * Makes an operation outcome, populating it from the parameters.
180 * @return a new operation outcome
182 public OperationOutcome makeOutcome() {
183 var outcome = new OperationOutcome();
184 outcome.setActor(getActor());
185 outcome.setOperation(getOperation());
191 * Invokes the callback to indicate that the operation has started. Any exceptions
192 * generated by the callback are logged, but not re-thrown.
194 * @param operation the operation that is being started
196 public void callbackStarted(OperationOutcome operation) {
197 logger.info("started operation {}.{} for {}", operation.getActor(), operation.getOperation(), getRequestId());
199 if (startCallback != null) {
200 Util.runFunction(() -> startCallback.accept(operation), "{}.{}: start-callback threw an exception for {}",
201 operation.getActor(), operation.getOperation(), getRequestId());
206 * Invokes the callback to indicate that the operation has completed. Any exceptions
207 * generated by the callback are logged, but not re-thrown.
209 * @param operation the operation that is being started
211 public void callbackCompleted(OperationOutcome operation) {
212 logger.info("completed operation {}.{} outcome={} for {}", operation.getActor(), operation.getOperation(),
213 operation.getResult(), getRequestId());
215 if (completeCallback != null) {
216 Util.runFunction(() -> completeCallback.accept(operation),
217 "{}.{}: complete-callback threw an exception for {}", operation.getActor(),
218 operation.getOperation(), getRequestId());
223 * Validates the parameters.
225 * @return the validation result
227 public BeanValidationResult validate() {
228 return new BeanValidator().validateTop(ControlLoopOperationParams.class.getSimpleName(), this);