Java 17 Upgrade
[policy/models.git] / models-interactions / model-actors / actor.aai / src / main / java / org / onap / policy / controlloop / actor / aai / AaiGetOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.aai;
23
24 import jakarta.ws.rs.client.Invocation.Builder;
25 import jakarta.ws.rs.client.WebTarget;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import org.onap.policy.common.utils.coder.StandardCoderObject;
30 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
31 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
34
35 /**
36  * Superclass of A&AI operators that use "get" to perform their request and store their
37  * response within the context as a {@link StandardCoderObject}. The property name under
38  * which they are stored is ${actor}.${operation}.${targetEntity}.
39  */
40 public class AaiGetOperation extends HttpOperation<StandardCoderObject> {
41     public static final int DEFAULT_RETRY = 3;
42
43     private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.AAI_TARGET_ENTITY);
44
45     /**
46      * Constructs the object.
47      *
48      * @param params operation parameters
49      * @param config configuration for this operation
50      */
51     public AaiGetOperation(ControlLoopOperationParams params, HttpConfig config) {
52         super(params, config, StandardCoderObject.class, PROPERTY_NAMES);
53     }
54
55     @Override
56     public void generateSubRequestId(int attempt) {
57         setSubRequestId(String.valueOf(attempt));
58     }
59
60     /**
61      * Adds a query parameter to a web target.
62      *
63      * @param web target to which the parameter should be added
64      * @param str the separator and parameter are appended here, for logging purposes
65      * @param separator separator to be added to "str"; that's its only use
66      * @param name parameter name
67      * @param value parameter value
68      * @return "web"
69      */
70     protected WebTarget addQuery(WebTarget web, StringBuilder str, String separator, String name, String value) {
71         str.append(separator);
72         str.append(name);
73         str.append('=');
74         str.append(value);
75
76         return web.queryParam(name, value);
77     }
78
79     /**
80      * Adds headers to the web builder.
81      *
82      * @param webldr builder to which the headers should be added
83      * @param headers headers to be added
84      */
85     protected void addHeaders(Builder webldr, Map<String, Object> headers) {
86         for (Entry<String, Object> header : headers.entrySet()) {
87             webldr.header(header.getKey(), header.getValue());
88         }
89     }
90
91     @Override
92     protected Map<String, Object> makeHeaders() {
93         return AaiUtil.makeHeaders(params);
94     }
95
96     /**
97      * Provides a default retry value, if none specified.
98      */
99     @Override
100     protected int getRetry(Integer retry) {
101         return (retry == null ? DEFAULT_RETRY : retry);
102     }
103 }