8e4b59c358a013e462db1d149f2c311266d20567
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.policy.client;
22
23 import jakarta.ws.rs.client.Entity;
24 import jakarta.ws.rs.core.MediaType;
25 import jakarta.ws.rs.core.Response;
26 import java.util.List;
27 import org.onap.policy.clamp.acm.participant.policy.main.parameters.ParticipantPolicyParameters;
28 import org.onap.policy.models.pdp.concepts.DeploymentGroup;
29 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
30 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
32 import org.springframework.stereotype.Component;
33
34 @Component
35 public class PolicyPapHttpClient extends AbstractHttpClient {
36
37     private static final String PAP_URI = "/policy/pap/v1/";
38     private final String pdpGroup;
39     private final String pdpType;
40
41     /**
42      * Constructor.
43      *
44      * @param parameters the policy participant parameters
45      */
46     public PolicyPapHttpClient(ParticipantPolicyParameters parameters) {
47         super(parameters.getPolicyPapParameters());
48         this.pdpGroup = parameters.getPdpGroup();
49         this.pdpType = parameters.getPdpType();
50     }
51
52     /**
53      * Deploy or undeploy Policies.
54      *
55      * @param policyName the name of the policy to be deployed/undeployed
56      * @param policyVersion the version of the policy to be deployed/undeployed
57      * @param action the action to deploy/undeploy policy
58      * @return Response
59      */
60     public Response handlePolicyDeployOrUndeploy(final String policyName, final String policyVersion,
61                                                  final DeploymentSubGroup.Action action) {
62
63         var policies = List.of(new ToscaConceptIdentifier(policyName, policyVersion));
64
65         var subGroup = new DeploymentSubGroup();
66         subGroup.setPolicies(policies);
67         subGroup.setPdpType(pdpType);
68         subGroup.setAction(action);
69
70         var group = new DeploymentGroup();
71         group.setDeploymentSubgroups(List.of(subGroup));
72         group.setName(pdpGroup);
73
74         var groups = new DeploymentGroups();
75         groups.setGroups(List.of(group));
76
77         return executePost(PAP_URI + "pdps/deployments/batch", Entity.entity(groups, MediaType.APPLICATION_JSON));
78     }
79 }