a0772238382df4c68e89b6bcc08fa5360de7a1cd
[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  * ================================================================================
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.actor.aai;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import javax.ws.rs.client.Invocation.Builder;
27 import javax.ws.rs.client.WebTarget;
28 import org.onap.policy.common.utils.coder.StandardCoderObject;
29 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
30 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
33
34 /**
35  * Superclass of A&AI operators that use "get" to perform their request and store their
36  * response within the context as a {@link StandardCoderObject}. The property name under
37  * which they are stored is ${actor}.${operation}.${targetEntity}.
38  */
39 public class AaiGetOperation extends HttpOperation<StandardCoderObject> {
40     public static final int DEFAULT_RETRY = 3;
41
42     private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.AAI_TARGET_ENTITY);
43
44     /**
45      * Constructs the object.
46      *
47      * @param params operation parameters
48      * @param config configuration for this operation
49      */
50     public AaiGetOperation(ControlLoopOperationParams params, HttpConfig config) {
51         super(params, config, StandardCoderObject.class, PROPERTY_NAMES);
52     }
53
54     @Override
55     public void generateSubRequestId(int attempt) {
56         setSubRequestId(String.valueOf(attempt));
57     }
58
59     /**
60      * Adds a query parameter to a web target.
61      *
62      * @param web target to which the parameter should be added
63      * @param str the separator and parameter are appended here, for logging purposes
64      * @param separator separator to be added to "str"; that's its only use
65      * @param name parameter name
66      * @param value parameter value
67      * @return "web"
68      */
69     protected WebTarget addQuery(WebTarget web, StringBuilder str, String separator, String name, String value) {
70         str.append(separator);
71         str.append(name);
72         str.append('=');
73         str.append(value);
74
75         return web.queryParam(name, value);
76     }
77
78     /**
79      * Adds headers to the web builder.
80      *
81      * @param webldr builder to which the headers should be added
82      * @param headers headers to be added
83      */
84     protected void addHeaders(Builder webldr, Map<String, Object> headers) {
85         for (Entry<String, Object> header : headers.entrySet()) {
86             webldr.header(header.getKey(), header.getValue());
87         }
88     }
89
90     @Override
91     protected Map<String, Object> makeHeaders() {
92         return AaiUtil.makeHeaders(params);
93     }
94
95     /**
96      * Provides a default retry value, if none specified.
97      */
98     @Override
99     protected int getRetry(Integer retry) {
100         return (retry == null ? DEFAULT_RETRY : retry);
101     }
102 }