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