Merge "Add A&AI actor and some operators"
[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.Set;
25 import java.util.concurrent.CompletableFuture;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import org.onap.policy.aai.AaiConstants;
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.impl.HttpOperator;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
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     // operation names
48     public static final String TENANT = "Tenant";
49
50     // property prefixes
51     private static final String TENANT_KEY_PREFIX = AaiConstants.CONTEXT_PREFIX + TENANT + ".";
52
53     /**
54      * Operation names supported by this operator.
55      */
56     public static final Set<String> OPERATIONS = Set.of(TENANT);
57
58
59     /**
60      * Responses that are retrieved from A&AI are placed in the operation context under
61      * the name "${propertyPrefix}.${targetEntity}".
62      */
63     private final String propertyPrefix;
64
65     /**
66      * Constructs the object.
67      *
68      * @param params operation parameters
69      * @param operator operator that created this operation
70      */
71     public AaiGetOperation(ControlLoopOperationParams params, HttpOperator operator) {
72         super(params, operator, StandardCoderObject.class);
73         this.propertyPrefix = operator.getFullName() + ".";
74     }
75
76     /**
77      * Gets the "context key" for the tenant query response associated with the given
78      * target entity.
79      *
80      * @param targetEntity target entity
81      * @return the "context key" for the response associated with the given target
82      */
83     public static String getTenantKey(String targetEntity) {
84         return (TENANT_KEY_PREFIX + targetEntity);
85     }
86
87     @Override
88     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
89
90         Map<String, Object> headers = makeHeaders();
91
92         headers.put("Accept", MediaType.APPLICATION_JSON);
93         String url = makeUrl();
94
95         logRestRequest(url, null);
96
97         // @formatter:off
98         return handleResponse(outcome, url,
99             callback -> operator.getClient().get(callback, makePath(), headers));
100         // @formatter:on
101     }
102
103     @Override
104     protected Map<String, Object> makeHeaders() {
105         return AaiUtil.makeHeaders(params);
106     }
107
108     @Override
109     public String makePath() {
110         return (operator.getPath() + "/" + params.getTargetEntity());
111     }
112
113     /**
114      * Injects the response into the context.
115      */
116     @Override
117     protected void postProcessResponse(OperationOutcome outcome, String url, Response rawResponse,
118                     StandardCoderObject response) {
119         String entity = params.getTargetEntity();
120
121         logger.info("{}: caching response of {} for {}", getFullName(), entity, params.getRequestId());
122
123         params.getContext().setProperty(propertyPrefix + entity, 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 }