Clean up and enhancement of Actor re-design
[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.AccessLevel;
25 import lombok.Getter;
26 import org.onap.policy.common.endpoints.http.client.HttpClient;
27 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
28 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
29 import org.onap.policy.common.parameters.ValidationResult;
30 import org.onap.policy.controlloop.actorserviceprovider.Util;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
33
34 /**
35  * Operator that uses HTTP. The operator's parameters must be a {@link HttpParams}.
36  */
37 public class HttpOperator extends OperatorPartial {
38
39     @Getter(AccessLevel.PROTECTED)
40     private HttpClient client;
41
42     @Getter
43     private long timeoutSec;
44
45     /**
46      * URI path for this particular operation.
47      */
48     @Getter
49     private String path;
50
51
52     /**
53      * Constructs the object.
54      *
55      * @param actorName name of the actor with which this operator is associated
56      * @param name operation name
57      */
58     public HttpOperator(String actorName, String name) {
59         super(actorName, name);
60     }
61
62     /**
63      * Translates the parameters to an {@link HttpParams} and then extracts the relevant
64      * values.
65      */
66     @Override
67     protected void doConfigure(Map<String, Object> parameters) {
68         HttpParams params = Util.translate(getFullName(), parameters, HttpParams.class);
69         ValidationResult result = params.validate(getFullName());
70         if (!result.isValid()) {
71             throw new ParameterValidationRuntimeException("invalid parameters", result);
72         }
73
74         client = getClientFactory().get(params.getClientName());
75         path = params.getPath();
76         timeoutSec = params.getTimeoutSec();
77     }
78
79     // these may be overridden by junits
80
81     protected HttpClientFactory getClientFactory() {
82         return HttpClientFactoryInstance.getClientFactory();
83     }
84 }