Use BidirectionalTopicClient from policy-common
[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.endpoints.event.comm.Topic.CommInfrastructure;
30 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
31 import org.onap.policy.common.utils.coder.StandardCoderObject;
32 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
33 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
34 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
35 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
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     // operation names
50     public static final String TENANT = "Tenant";
51
52     // property prefixes
53     private static final String TENANT_KEY_PREFIX = AaiConstants.CONTEXT_PREFIX + TENANT + ".";
54
55     /**
56      * Operation names supported by this operator.
57      */
58     public static final Set<String> OPERATIONS = Set.of(TENANT);
59
60
61     /**
62      * Responses that are retrieved from A&AI are placed in the operation context under
63      * the name "${propertyPrefix}.${targetEntity}".
64      */
65     private final String propertyPrefix;
66
67     /**
68      * Constructs the object.
69      *
70      * @param params operation parameters
71      * @param operator operator that created this operation
72      */
73     public AaiGetOperation(ControlLoopOperationParams params, HttpOperator operator) {
74         super(params, operator, StandardCoderObject.class);
75         this.propertyPrefix = operator.getFullName() + ".";
76     }
77
78     /**
79      * Gets the "context key" for the tenant query response associated with the given
80      * target entity.
81      *
82      * @param targetEntity target entity
83      * @return the "context key" for the response associated with the given target
84      */
85     public static String getTenantKey(String targetEntity) {
86         return (TENANT_KEY_PREFIX + targetEntity);
87     }
88
89     @Override
90     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
91
92         Map<String, Object> headers = makeHeaders();
93
94         headers.put("Accept", MediaType.APPLICATION_JSON);
95         String url = makeUrl();
96
97         logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
98
99         // @formatter:off
100         return handleResponse(outcome, url,
101             callback -> operator.getClient().get(callback, makePath(), headers));
102         // @formatter:on
103     }
104
105     @Override
106     protected Map<String, Object> makeHeaders() {
107         return AaiUtil.makeHeaders(params);
108     }
109
110     @Override
111     public String makePath() {
112         return (operator.getPath() + "/" + params.getTargetEntity());
113     }
114
115     /**
116      * Injects the response into the context.
117      */
118     @Override
119     protected void postProcessResponse(OperationOutcome outcome, String url, Response rawResponse,
120                     StandardCoderObject response) {
121         String entity = params.getTargetEntity();
122
123         logger.info("{}: caching response of {} for {}", getFullName(), entity, params.getRequestId());
124
125         params.getContext().setProperty(propertyPrefix + entity, response);
126     }
127
128     /**
129      * Provides a default retry value, if none specified.
130      */
131     @Override
132     protected int getRetry(Integer retry) {
133         return (retry == null ? DEFAULT_RETRY : retry);
134     }
135 }