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.parameters;
24 import java.util.TreeMap;
25 import java.util.function.Function;
26 import lombok.EqualsAndHashCode;
29 import org.onap.policy.common.parameters.BeanValidator;
30 import org.onap.policy.common.parameters.ValidationResult;
31 import org.onap.policy.common.parameters.annotations.NotNull;
32 import org.onap.policy.controlloop.actorserviceprovider.Util;
35 * Superclass for Actor parameters that have default values in "this" object, and
36 * operation-specific values in {@link #operations}.
41 public class CommonActorParams {
43 * Name of the "operations" field contained within actor parameters.
45 public static final String OPERATIONS_FIELD = "operations";
48 * Maps the operation name to its parameters.
51 protected Map<String, Map<String, Object>> operations;
55 * Extracts a specific operation's parameters from "this".
57 * @param name name of the item containing "this"
58 * @return a function to extract an operation's parameters from "this". Note: the
59 * returned function is not thread-safe
61 public Function<String, Map<String, Object>> makeOperationParameters(String name) {
63 Map<String, Object> defaultParams = Util.translateToMap(name, this);
64 defaultParams.remove(OPERATIONS_FIELD);
66 return operationName -> {
67 Map<String, Object> specificParams = operations.get(operationName);
68 if (specificParams == null) {
72 // start with a copy of defaults and overlay with specific
73 Map<String, Object> subparams = new TreeMap<>(defaultParams);
74 subparams.putAll(specificParams);
76 return Util.translateToMap(name + "." + operationName, subparams);
81 * Validates the parameters.
83 * @param name name of the object containing these parameters
85 * @throws IllegalArgumentException if the parameters are invalid
87 public CommonActorParams doValidation(String name) {
88 ValidationResult result = validate(name);
89 if (!result.isValid()) {
90 throw new ParameterValidationRuntimeException("invalid parameters", result);
97 * Validates the parameters.
99 * @param resultName name of the result
101 * @return the validation result
103 public ValidationResult validate(String resultName) {
104 return new BeanValidator().validateTop(resultName, this);