Specify "Accept" header in A&AI requests
[policy/models.git] / models-interactions / model-actors / actor.aai / src / main / java / org / onap / policy / controlloop / actor / aai / AaiGetPnfOperation.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.net.URLEncoder;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Map;
26 import java.util.concurrent.CompletableFuture;
27 import javax.ws.rs.client.Invocation.Builder;
28 import javax.ws.rs.client.WebTarget;
29 import org.onap.policy.aai.AaiConstants;
30 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
31 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
32 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
35
36 /**
37  * A&AI get-pnf operator.
38  */
39 public class AaiGetPnfOperation extends AaiGetOperation {
40     private static final String URI_SEP = "/";
41
42     public static final String NAME = "Pnf";
43
44     // property prefixes
45     private static final String KEY_PREFIX = AaiConstants.CONTEXT_PREFIX + NAME + ".";
46
47     /**
48      * Constructs the object.
49      *
50      * @param params operation parameters
51      * @param config configuration for this operation
52      */
53     public AaiGetPnfOperation(ControlLoopOperationParams params, HttpConfig config) {
54         super(params, config);
55     }
56
57     /**
58      * Gets the "context key" for the PNF query response associated with the given
59      * target entity.
60      *
61      * @param targetEntity target entity
62      * @return the "context key" for the response associated with the given target
63      */
64     public static String getKey(String targetEntity) {
65         return (KEY_PREFIX + targetEntity);
66     }
67
68     @Override
69     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
70         Map<String, Object> headers = makeHeaders();
71
72         StringBuilder str = new StringBuilder(getClient().getBaseUrl());
73
74         String path = getPath() + URI_SEP + URLEncoder.encode(getTargetEntity(), StandardCharsets.UTF_8);
75         WebTarget web = getClient().getWebTarget().path(path);
76         str.append(path);
77
78         web = addQuery(web, str, "?", "depth", "0");
79
80         Builder webldr = web.request();
81         addHeaders(webldr, headers);
82
83         String url = str.toString();
84
85         logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
86
87         return handleResponse(outcome, url, callback -> webldr.async().get(callback));
88     }
89 }