839fe139cd2030efefdfc004ee4899b5284b8eaa
[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-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.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.OperationProperties;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
35 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
36
37 /**
38  * A&AI get-pnf operator.
39  */
40 public class AaiGetPnfOperation extends AaiGetOperation {
41     private static final String URI_SEP = "/";
42
43     public static final String NAME = "Pnf";
44
45     // property prefixes
46     private static final String KEY_PREFIX = AaiConstants.CONTEXT_PREFIX + NAME + ".";
47
48     /**
49      * Constructs the object.
50      *
51      * @param params operation parameters
52      * @param config configuration for this operation
53      */
54     public AaiGetPnfOperation(ControlLoopOperationParams params, HttpConfig config) {
55         super(params, config);
56     }
57
58     /**
59      * Gets the "context key" for the PNF query response associated with the given
60      * target entity.
61      *
62      * @param targetEntity target entity
63      * @return the "context key" for the response associated with the given target
64      */
65     public static String getKey(String targetEntity) {
66         return (KEY_PREFIX + targetEntity);
67     }
68
69     @Override
70     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
71         Map<String, Object> headers = makeHeaders();
72
73         var str = new StringBuilder(getClient().getBaseUrl());
74
75         String target = getRequiredProperty(OperationProperties.AAI_TARGET_ENTITY, "target entity");
76         String path = getPath() + URI_SEP + URLEncoder.encode(target, StandardCharsets.UTF_8);
77         WebTarget web = getClient().getWebTarget().path(path);
78         str.append(path);
79
80         web = addQuery(web, str, "?", "depth", "0");
81
82         Builder webldr = web.request();
83         addHeaders(webldr, headers);
84
85         var url = str.toString();
86
87         logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
88
89         return handleResponse(outcome, url, callback -> webldr.async().get(callback));
90     }
91 }