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