Use BidirectionalTopicClient from policy-common
[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 #operation}.
37  */
38 @Getter
39 @Setter
40 @EqualsAndHashCode
41 public class CommonActorParams {
42
43     /**
44      * Maps the operation name to its parameters.
45      */
46     @NotNull
47     protected Map<String, Map<String, Object>> operation;
48
49
50     /**
51      * Extracts a specific operation's parameters from "this".
52      *
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
56      */
57     public Function<String, Map<String, Object>> makeOperationParameters(String name) {
58
59         Map<String, Object> defaultParams = Util.translateToMap(name, this);
60         defaultParams.remove("operation");
61
62         return operationName -> {
63             Map<String, Object> specificParams = operation.get(operationName);
64             if (specificParams == null) {
65                 return null;
66             }
67
68             // start with a copy of defaults and overlay with specific
69             Map<String, Object> subparams = new TreeMap<>(defaultParams);
70             subparams.putAll(specificParams);
71
72             return Util.translateToMap(name + "." + operationName, subparams);
73         };
74     }
75
76     /**
77      * Validates the parameters.
78      *
79      * @param name name of the object containing these parameters
80      * @return "this"
81      * @throws IllegalArgumentException if the parameters are invalid
82      */
83     public CommonActorParams doValidation(String name) {
84         ValidationResult result = validate(name);
85         if (!result.isValid()) {
86             throw new ParameterValidationRuntimeException("invalid parameters", result);
87         }
88
89         return this;
90     }
91
92     /**
93      * Validates the parameters.
94      *
95      * @param resultName name of the result
96      *
97      * @return the validation result
98      */
99     public ValidationResult validate(String resultName) {
100         return new BeanValidator().validateTop(resultName, this);
101     }
102 }