Merge "Add PNF support to new CDS actor"
[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.Map;
24 import java.util.Map.Entry;
25 import java.util.concurrent.CompletableFuture;
26 import javax.ws.rs.client.Invocation.Builder;
27 import javax.ws.rs.client.WebTarget;
28 import javax.ws.rs.core.Response;
29 import org.onap.policy.common.utils.coder.StandardCoderObject;
30 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
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 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Superclass of A&AI operators that use "get" to perform their request and store their
39  * response within the context as a {@link StandardCoderObject}. The property name under
40  * which they are stored is ${actor}.${operation}.${targetEntity}.
41  */
42 public class AaiGetOperation extends HttpOperation<StandardCoderObject> {
43     private static final Logger logger = LoggerFactory.getLogger(AaiGetOperation.class);
44
45     public static final int DEFAULT_RETRY = 3;
46
47
48     /**
49      * Responses that are retrieved from A&AI are placed in the operation context under
50      * the name "${propertyPrefix}.${targetEntity}".
51      */
52     private final String propertyPrefix;
53
54     /**
55      * Constructs the object.
56      *
57      * @param params operation parameters
58      * @param config configuration for this operation
59      */
60     public AaiGetOperation(ControlLoopOperationParams params, HttpConfig config) {
61         super(params, config, StandardCoderObject.class);
62         this.propertyPrefix = getFullName() + ".";
63     }
64
65     @Override
66     public void generateSubRequestId(int attempt) {
67         setSubRequestId(String.valueOf(attempt));
68     }
69
70     /**
71      * Adds a query parameter to a web target.
72      *
73      * @param web target to which the parameter should be added
74      * @param str the separator and parameter are appended here, for logging purposes
75      * @param separator separator to be added to "str"; that's its only use
76      * @param name parameter name
77      * @param value parameter value
78      * @return "web"
79      */
80     protected WebTarget addQuery(WebTarget web, StringBuilder str, String separator, String name, String value) {
81         str.append(separator);
82         str.append(name);
83         str.append('=');
84         str.append(value);
85
86         return web.queryParam(name, value);
87     }
88
89     /**
90      * Adds headers to the web builder.
91      *
92      * @param webldr builder to which the headers should be added
93      * @param headers headers to be added
94      */
95     protected void addHeaders(Builder webldr, Map<String, Object> headers) {
96         for (Entry<String, Object> header : headers.entrySet()) {
97             webldr.header(header.getKey(), header.getValue());
98         }
99     }
100
101     @Override
102     protected Map<String, Object> makeHeaders() {
103         return AaiUtil.makeHeaders(params);
104     }
105
106     /**
107      * Injects the response into the context.
108      */
109     @Override
110     protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
111                     Response rawResponse, StandardCoderObject response) {
112         String entity = params.getTargetEntity();
113
114         logger.info("{}: caching response of {} for {}", getFullName(), entity, params.getRequestId());
115
116         params.getContext().setProperty(propertyPrefix + entity, response);
117
118         return super.postProcessResponse(outcome, url, rawResponse, response);
119     }
120
121     /**
122      * Provides a default retry value, if none specified.
123      */
124     @Override
125     protected int getRetry(Integer retry) {
126         return (retry == null ? DEFAULT_RETRY : retry);
127     }
128 }