5f432704cac9d317ffb0cb15ac8fea4fe07da444
[policy/models.git] / models-interactions / model-actors / actor.aai / src / main / java / org / onap / policy / controlloop / actor / aai / AaiCustomQueryOperation.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.concurrent.CompletableFuture;
25 import javax.ws.rs.client.Entity;
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.aai.AaiCqResponse;
30 import org.onap.policy.common.utils.coder.StandardCoderObject;
31 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
32 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
33 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * A&AI Custom Query. Stores the {@link AaiCqResponse} in the context. In addition, if the
40  * context does not contain the "tenant" data for the vserver, then it will request that,
41  * as well.
42  */
43 public class AaiCustomQueryOperation extends HttpOperation<String> {
44     private static final Logger logger = LoggerFactory.getLogger(AaiCustomQueryOperation.class);
45
46     public static final String NAME = "CustomQuery";
47
48     public static final String RESOURCE_LINK = "resource-link";
49     public static final String RESULT_DATA = "result-data";
50
51     private static final String PREFIX = "/aai/v16";
52
53     /**
54      * Constructs the object.
55      *
56      * @param params operation parameters
57      * @param operator operator that created this operation
58      */
59     public AaiCustomQueryOperation(ControlLoopOperationParams params, HttpOperator operator) {
60         super(params, operator, String.class);
61     }
62
63     /**
64      * Queries the vserver, if necessary.
65      */
66     @Override
67     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
68         String vserver = params.getTargetEntity();
69
70         ControlLoopOperationParams tenantParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
71                         .operation(AaiGetOperation.TENANT).payload(null).retry(null).timeoutSec(null).build();
72
73         return params.getContext().obtain(AaiGetOperation.getTenantKey(vserver), tenantParams);
74     }
75
76     @Override
77     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
78
79         Map<String, String> request = makeRequest();
80
81         Entity<Map<String, String>> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
82
83         Map<String, Object> headers = makeHeaders();
84
85         headers.put("Accept", MediaType.APPLICATION_JSON);
86         String url = makeUrl();
87
88         logRestRequest(url, request);
89
90         // @formatter:off
91         return handleResponse(outcome, url,
92             callback -> operator.getClient().put(callback, makePath(), entity, headers));
93         // @formatter:on
94     }
95
96     /**
97      * Constructs the custom query using the previously retrieved tenant data.
98      */
99     private Map<String, String> makeRequest() {
100         String vserver = params.getTargetEntity();
101         StandardCoderObject tenant = params.getContext().getProperty(AaiGetOperation.getTenantKey(vserver));
102
103         String resourceLink = tenant.getString(RESULT_DATA, 0, RESOURCE_LINK);
104         if (resourceLink == null) {
105             throw new IllegalArgumentException("cannot perform custom query - no resource-link");
106         }
107
108         resourceLink = resourceLink.replace(PREFIX, "");
109
110         return Map.of("start", resourceLink, "query", "query/closed-loop");
111     }
112
113     @Override
114     protected Map<String, Object> makeHeaders() {
115         return AaiUtil.makeHeaders(params);
116     }
117
118     /**
119      * Injects the response into the context.
120      */
121     @Override
122     protected void postProcessResponse(OperationOutcome outcome, String url, Response rawResponse, String response) {
123
124         logger.info("{}: caching response for {}", getFullName(), params.getRequestId());
125         params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, new AaiCqResponse(response));
126     }
127 }