Change payload to Map<String,Object> so it's more versatile
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / parameters / CommonActorParams.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.parameters;
22
23 import java.util.Map;
24 import java.util.TreeMap;
25 import java.util.function.Function;
26 import lombok.EqualsAndHashCode;
27 import lombok.Getter;
28 import lombok.Setter;
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;
33
34 /**
35  * Superclass for Actor parameters that have default values in "this" object, and
36  * operation-specific values in {@link #operations}.
37  */
38 @Getter
39 @Setter
40 @EqualsAndHashCode
41 public class CommonActorParams {
42     /**
43      * Name of the "operations" field contained within actor parameters.
44      */
45     public static final String OPERATIONS_FIELD = "operations";
46
47     /**
48      * Maps the operation name to its parameters.
49      */
50     @NotNull
51     protected Map<String, Map<String, Object>> operations;
52
53
54     /**
55      * Extracts a specific operation's parameters from "this".
56      *
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
60      */
61     public Function<String, Map<String, Object>> makeOperationParameters(String name) {
62
63         Map<String, Object> defaultParams = Util.translateToMap(name, this);
64         defaultParams.remove(OPERATIONS_FIELD);
65
66         return operationName -> {
67             Map<String, Object> specificParams = operations.get(operationName);
68             if (specificParams == null) {
69                 return null;
70             }
71
72             // start with a copy of defaults and overlay with specific
73             Map<String, Object> subparams = new TreeMap<>(defaultParams);
74             subparams.putAll(specificParams);
75
76             return Util.translateToMap(name + "." + operationName, subparams);
77         };
78     }
79
80     /**
81      * Validates the parameters.
82      *
83      * @param name name of the object containing these parameters
84      * @return "this"
85      * @throws IllegalArgumentException if the parameters are invalid
86      */
87     public CommonActorParams doValidation(String name) {
88         ValidationResult result = validate(name);
89         if (!result.isValid()) {
90             throw new ParameterValidationRuntimeException("invalid parameters", result);
91         }
92
93         return this;
94     }
95
96     /**
97      * Validates the parameters.
98      *
99      * @param resultName name of the result
100      *
101      * @return the validation result
102      */
103     public ValidationResult validate(String resultName) {
104         return new BeanValidator().validateTop(resultName, this);
105     }
106 }