Moving common polling code into HttpOperation
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpPollingOperator.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.impl;
22
23 import java.util.Map;
24 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
25 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
26 import org.onap.policy.common.parameters.ValidationResult;
27 import org.onap.policy.controlloop.actorserviceprovider.Util;
28 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
29 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingParams;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
31
32 /**
33  * Operator that uses HTTP and polls for the request completion status. The operator's
34  * parameters must be an {@link HttpPollingParams}.
35  */
36 public class HttpPollingOperator extends TypedOperator<HttpPollingConfig, HttpOperation<?>> {
37
38     /**
39      * Constructs the object.
40      *
41      * @param actorName name of the actor with which this operator is associated
42      * @param name operation name
43      */
44     protected HttpPollingOperator(String actorName, String name) {
45         this(actorName, name, null);
46     }
47
48     /**
49      * Constructs the object.
50      *
51      * @param actorName name of the actor with which this operator is associated
52      * @param name operation name
53      * @param operationMaker function to make an operation
54      */
55     public HttpPollingOperator(String actorName, String name,
56                     OperationMaker<HttpPollingConfig, HttpOperation<?>> operationMaker) {
57         super(actorName, name, operationMaker);
58     }
59
60     /**
61      * Makes a new configuration using the specified parameters.
62      *
63      * @param parameters operator parameters
64      * @return a new configuration
65      */
66     protected HttpPollingConfig makeConfiguration(Map<String, Object> parameters) {
67         HttpPollingParams params = Util.translate(getFullName(), parameters, HttpPollingParams.class);
68         ValidationResult result = params.validate(getFullName());
69         if (!result.isValid()) {
70             throw new ParameterValidationRuntimeException("invalid parameters", result);
71         }
72
73         return new HttpPollingConfig(getBlockingExecutor(), params, getClientFactory());
74     }
75
76     // these may be overridden by junit tests
77
78     protected HttpClientFactory getClientFactory() {
79         return HttpClientFactoryInstance.getClientFactory();
80     }
81 }