More actor clean-up
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpOperator.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 lombok.Getter;
25 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
26 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
27 import org.onap.policy.common.parameters.ValidationResult;
28 import org.onap.policy.controlloop.actorserviceprovider.Operation;
29 import org.onap.policy.controlloop.actorserviceprovider.Util;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
34
35 /**
36  * Operator that uses HTTP. The operator's parameters must be an {@link HttpParams}.
37  */
38 public class HttpOperator extends OperatorPartial {
39
40     /**
41      * Function to make an operation.
42      */
43     @SuppressWarnings("rawtypes")
44     private final OperationMaker<HttpConfig, HttpOperation> operationMaker;
45
46     /**
47      * Current configuration. This is set by {@link #doConfigure(Map)}.
48      */
49     @Getter
50     private HttpConfig currentConfig;
51
52
53     /**
54      * Constructs the object.
55      *
56      * @param actorName name of the actor with which this operator is associated
57      * @param name operation name
58      */
59     protected HttpOperator(String actorName, String name) {
60         this(actorName, name, null);
61     }
62
63     /**
64      * Constructs the object.
65      *
66      * @param actorName name of the actor with which this operator is associated
67      * @param name operation name
68      * @param operationMaker function to make an operation
69      */
70     public HttpOperator(String actorName, String name,
71                     @SuppressWarnings("rawtypes") OperationMaker<HttpConfig, HttpOperation> operationMaker) {
72         super(actorName, name);
73         this.operationMaker = operationMaker;
74     }
75
76     /**
77      * Translates the parameters to an {@link HttpParams} and then extracts the relevant
78      * values.
79      */
80     @Override
81     protected void doConfigure(Map<String, Object> parameters) {
82         currentConfig = makeConfiguration(parameters);
83     }
84
85     /**
86      * Makes a new configuration using the specified parameters.
87      *
88      * @param parameters operator parameters
89      * @return a new configuration
90      */
91     protected HttpConfig makeConfiguration(Map<String, Object> parameters) {
92         HttpParams params = Util.translate(getFullName(), parameters, HttpParams.class);
93         ValidationResult result = params.validate(getFullName());
94         if (!result.isValid()) {
95             throw new ParameterValidationRuntimeException("invalid parameters", result);
96         }
97
98         return new HttpConfig(getBlockingExecutor(), params, getClientFactory());
99     }
100
101     @Override
102     public Operation buildOperation(ControlLoopOperationParams params) {
103         if (operationMaker == null) {
104             throw new UnsupportedOperationException("cannot make operation for " + getFullName());
105         }
106
107         verifyRunning();
108
109         return operationMaker.apply(params, currentConfig);
110     }
111
112     // these may be overridden by junit tests
113
114     protected HttpClientFactory getClientFactory() {
115         return HttpClientFactoryInstance.getClientFactory();
116     }
117 }