Add SO actor
[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 java.util.concurrent.TimeUnit;
25 import java.util.function.BiFunction;
26 import lombok.Getter;
27 import org.onap.policy.common.endpoints.http.client.HttpClient;
28 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
29 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
30 import org.onap.policy.common.parameters.ValidationResult;
31 import org.onap.policy.controlloop.actorserviceprovider.Operation;
32 import org.onap.policy.controlloop.actorserviceprovider.Util;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
35 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
36
37 /**
38  * Operator that uses HTTP. The operator's parameters must be an {@link HttpParams}.
39  */
40 @Getter
41 public abstract class HttpOperator extends OperatorPartial {
42
43     private HttpClient client;
44
45     /**
46      * Default timeout, in milliseconds, if none specified in the request.
47      */
48     private long timeoutMs;
49
50     /**
51      * URI path for this particular operation. Includes a leading "/".
52      */
53     private String path;
54
55
56     /**
57      * Constructs the object.
58      *
59      * @param actorName name of the actor with which this operator is associated
60      * @param name operation name
61      */
62     public HttpOperator(String actorName, String name) {
63         super(actorName, name);
64     }
65
66     /**
67      * Makes an operator that will construct operations.
68      *
69      * @param <T> response type
70      * @param actorName actor name
71      * @param operation operation name
72      * @param operationMaker function to make an operation
73      * @return a new operator
74      */
75     public static <T> HttpOperator makeOperator(String actorName, String operation,
76                     BiFunction<ControlLoopOperationParams, HttpOperator, HttpOperation<T>> operationMaker) {
77
78         return new HttpOperator(actorName, operation) {
79             @Override
80             public Operation buildOperation(ControlLoopOperationParams params) {
81                 return operationMaker.apply(params, this);
82             }
83         };
84     }
85
86     /**
87      * Translates the parameters to an {@link HttpParams} and then extracts the relevant
88      * values.
89      */
90     @Override
91     protected void doConfigure(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         doConfigure(params);
99     }
100
101     /**
102      * Configures the operator using the specified parameters.
103      *
104      * @param params operator parameters
105      */
106     protected void doConfigure(HttpParams params) {
107         client = getClientFactory().get(params.getClientName());
108         path = params.getPath();
109         timeoutMs = TimeUnit.MILLISECONDS.convert(params.getTimeoutSec(), TimeUnit.SECONDS);
110     }
111
112     // these may be overridden by junit tests
113
114     protected HttpClientFactory getClientFactory() {
115         return HttpClientFactoryInstance.getClientFactory();
116     }
117 }