584170ae4be7a7e6b10ccaedbe5582d3c9859454
[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 java.util.LinkedList;
24 import java.util.List;
25 import javax.ws.rs.client.Entity;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import org.onap.policy.clamp.acm.participant.policy.main.parameters.ParticipantPolicyParameters;
29 import org.onap.policy.models.pdp.concepts.DeploymentGroup;
30 import org.onap.policy.models.pdp.concepts.DeploymentGroups;
31 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
33 import org.springframework.stereotype.Component;
34
35 @Component
36 public class PolicyPapHttpClient extends AbstractHttpClient {
37
38     private static final String PAP_URI = "/policy/pap/v1/";
39     private final String pdpGroup;
40     private final String pdpType;
41
42     /**
43      * Constructor.
44      *
45      * @param parameters the policy participant parameters
46      */
47     public PolicyPapHttpClient(ParticipantPolicyParameters parameters) {
48         super(parameters.getPolicyPapParameters());
49         this.pdpGroup = parameters.getPdpGroup();
50         this.pdpType = parameters.getPdpType();
51     }
52
53     /**
54      * Deploy or undeploy Policies.
55      *
56      * @param policyName the name of the policy to be deployed/undeployed
57      * @param policyVersion the version of the policy to be deployed/undeployed
58      * @param action the action to deploy/undeploy policy
59      * @return Response
60      */
61     public Response handlePolicyDeployOrUndeploy(final String policyName, final String policyVersion,
62                                                  final DeploymentSubGroup.Action action) {
63
64         List<ToscaConceptIdentifier> policies = new LinkedList<ToscaConceptIdentifier>();
65         policies.add(new ToscaConceptIdentifier(policyName, policyVersion));
66
67         DeploymentSubGroup subGroup = new DeploymentSubGroup();
68         subGroup.setPolicies(policies);
69         subGroup.setPdpType(pdpType);
70         subGroup.setAction(action);
71
72         DeploymentGroup group = new DeploymentGroup();
73         List<DeploymentSubGroup> subGroups = new LinkedList<DeploymentSubGroup>();
74         subGroups.add(subGroup);
75         group.setDeploymentSubgroups(subGroups);
76         group.setName(pdpGroup);
77
78         DeploymentGroups groups = new DeploymentGroups();
79         List<DeploymentGroup> groupsArr = new LinkedList<DeploymentGroup>();
80         groupsArr.add(group);
81         groups.setGroups(groupsArr);
82
83         return executePost(PAP_URI + "pdps/deployments/batch", Entity.entity(groups, MediaType.APPLICATION_JSON));
84     }
85 }