ca3a55ced8a6aeee96efdf80a523451fd73d900b
[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-2021 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.Entity;
28 import javax.ws.rs.client.Invocation.Builder;
29 import javax.ws.rs.client.WebTarget;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import org.onap.policy.aai.AaiCqResponse;
33 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
35 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
37 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
38 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
41
42 /**
43  * A&AI Custom Query. Stores the {@link AaiCqResponse} in the context. In addition, if the
44  * context does not contain the "tenant" data for the vserver, then it will request that,
45  * as well. Note: this ignores the "target entity" in the parameters as this query always
46  * applies to the vserver, thus the target entity may be set to an empty string.
47  */
48 public class AaiCustomQueryOperation extends HttpOperation<String> {
49     public static final String NAME = AaiCqResponse.OPERATION;
50
51     public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
52     public static final String RESOURCE_LINK = "resource-link";
53     public static final String RESULT_DATA = "result-data";
54
55     private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.AAI_VSERVER_LINK);
56
57     /**
58      * Constructs the object.
59      *
60      * @param params operation parameters
61      * @param config configuration for this operation
62      */
63     public AaiCustomQueryOperation(ControlLoopOperationParams params, HttpConfig config) {
64         super(params, config, String.class, PROPERTY_NAMES);
65     }
66
67     @Override
68     public void generateSubRequestId(int attempt) {
69         setSubRequestId(String.valueOf(attempt));
70     }
71
72     @Override
73     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
74         outcome.setSubRequestId(String.valueOf(attempt));
75
76         final Map<String, String> request = makeRequest();
77         Map<String, Object> headers = makeHeaders();
78
79         var str = new StringBuilder(getClient().getBaseUrl());
80
81         String path = getPath();
82         WebTarget web = getClient().getWebTarget().path(path);
83         str.append(path);
84
85         web = addQuery(web, str, "?", "format", "resource");
86
87         Builder webldr = web.request();
88         for (Entry<String, Object> header : headers.entrySet()) {
89             webldr.header(header.getKey(), header.getValue());
90         }
91
92         var url = str.toString();
93
94         String strRequest = prettyPrint(request);
95         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
96
97         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
98
99         return handleResponse(outcome, url, callback -> webldr.async().put(entity, callback));
100     }
101
102     private WebTarget addQuery(WebTarget web, StringBuilder str, String separator, String name, String value) {
103         str.append(separator);
104         str.append(name);
105         str.append('=');
106         str.append(value);
107
108         return web.queryParam(name, value);
109     }
110
111     /**
112      * Constructs the custom query using the previously retrieved tenant data.
113      */
114     private Map<String, String> makeRequest() {
115         return Map.of("start", getVserverLink(), "query", "query/closed-loop");
116     }
117
118     /**
119      * Gets the vserver link, first checking the properties, and then the tenant data.
120      *
121      * @return the vserver link
122      */
123     protected String getVserverLink() {
124         return getRequiredProperty(OperationProperties.AAI_VSERVER_LINK, "vserver link");
125     }
126
127     @Override
128     protected Map<String, Object> makeHeaders() {
129         return AaiUtil.makeHeaders(params);
130     }
131
132     @Override
133     public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
134                     String response) {
135
136         super.setOutcome(outcome, result, rawResponse, response);
137
138         if (response != null) {
139             outcome.setResponse(new AaiCqResponse(response));
140         }
141
142         return outcome;
143     }
144 }