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 #operation}.
41 public class CommonActorParams {
44 * Maps the operation name to its parameters.
47 protected Map<String, Map<String, Object>> operation;
51 * Extracts a specific operation's parameters from "this".
53 * @param name name of the item containing "this"
54 * @return a function to extract an operation's parameters from "this". Note: the
55 * returned function is not thread-safe
57 public Function<String, Map<String, Object>> makeOperationParameters(String name) {
59 Map<String, Object> defaultParams = Util.translateToMap(name, this);
60 defaultParams.remove("operation");
62 return operationName -> {
63 Map<String, Object> specificParams = operation.get(operationName);
64 if (specificParams == null) {
68 // start with a copy of defaults and overlay with specific
69 Map<String, Object> subparams = new TreeMap<>(defaultParams);
70 subparams.putAll(specificParams);
72 return Util.translateToMap(name + "." + operationName, subparams);
77 * Validates the parameters.
79 * @param name name of the object containing these parameters
81 * @throws IllegalArgumentException if the parameters are invalid
83 public CommonActorParams doValidation(String name) {
84 ValidationResult result = validate(name);
85 if (!result.isValid()) {
86 throw new ParameterValidationRuntimeException("invalid parameters", result);
93 * Validates the parameters.
95 * @param resultName name of the result
97 * @return the validation result
99 public ValidationResult validate(String resultName) {
100 return new BeanValidator().validateTop(resultName, this);