275c8bc4e26a7bf5007eb0262ba2e7e34a786cba
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / parameters / HttpActorParams.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.function.Function;
25 import lombok.Data;
26 import org.onap.policy.common.parameters.BeanValidationResult;
27 import org.onap.policy.common.parameters.BeanValidator;
28 import org.onap.policy.common.parameters.ValidationResult;
29 import org.onap.policy.common.parameters.annotations.Min;
30 import org.onap.policy.common.parameters.annotations.NotBlank;
31 import org.onap.policy.common.parameters.annotations.NotNull;
32 import org.onap.policy.controlloop.actorserviceprovider.Util;
33
34 /**
35  * Parameters used by Actors that connect to a server via HTTP. This contains the
36  * parameters that are common to all of the operations. Only the path changes for each
37  * operation, thus it includes a mapping from operation name to path.
38  */
39 @Data
40 @NotNull
41 @NotBlank
42 public class HttpActorParams {
43
44     /**
45      * Name of the HttpClient, as found in the HttpClientFactory.
46      */
47     private String clientName;
48
49     /**
50      * Amount of time, in seconds to wait for the HTTP request to complete, where zero
51      * indicates that it should wait forever. The default is zero.
52      */
53     @Min(0)
54     private int timeoutSec = 0;
55
56     /**
57      * Maps the operation name to its URI path.
58      */
59     private Map<String, String> path;
60
61     /**
62      * Extracts a specific operation's parameters from "this".
63      *
64      * @param name name of the item containing "this"
65      * @return a function to extract an operation's parameters from "this". Note: the
66      *         returned function is not thread-safe
67      */
68     public Function<String, Map<String, Object>> makeOperationParameters(String name) {
69         HttpParams subparams = HttpParams.builder().clientName(getClientName()).timeoutSec(getTimeoutSec()).build();
70
71         return operation -> {
72             String subpath = path.get(operation);
73             if (subpath == null) {
74                 return null;
75             }
76
77             subparams.setPath(subpath);
78             return Util.translateToMap(name + "." + operation, subparams);
79         };
80     }
81
82     /**
83      * Validates the parameters.
84      *
85      * @param name name of the object containing these parameters
86      * @return "this"
87      * @throws IllegalArgumentException if the parameters are invalid
88      */
89     public HttpActorParams doValidation(String name) {
90         ValidationResult result = validate(name);
91         if (!result.isValid()) {
92             throw new ParameterValidationRuntimeException("invalid parameters", result);
93         }
94
95         return this;
96     }
97
98     /**
99      * Validates the parameters.
100      *
101      * @param resultName name of the result
102      *
103      * @return the validation result
104      */
105     public ValidationResult validate(String resultName) {
106         BeanValidationResult result = new BeanValidator().validateTop(resultName, this);
107
108         result.validateMap("path", path, (result2, entry) -> result2.validateNotNull(entry.getKey(), entry.getValue()));
109
110         return result;
111     }
112 }