3cce3bd8a404a4da578aeaf36684ef5771061208
[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.controlloop.participant.policy.client;
22
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonObject;
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.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters;
29 import org.springframework.stereotype.Component;
30
31 @Component
32 public class PolicyPapHttpClient extends AbstractHttpClient {
33
34     private static final String PAP_URI = "/policy/pap/v1/";
35     private final String pdpGroup;
36     private final String pdpType;
37
38     /**
39      * Constructor.
40      *
41      * @param parameters the policy participant parameters
42      */
43     public PolicyPapHttpClient(ParticipantPolicyParameters parameters) {
44         super(parameters.getPolicyPapParameters());
45         this.pdpGroup = parameters.getPdpGroup();
46         this.pdpType = parameters.getPdpType();
47     }
48
49     /**
50      * Deploy or undeploy Policies.
51      *
52      * @param policyName the name of the policy to be deployed/undeployed
53      * @param policyVersion the version of the policy to be deployed/undeployed
54      * @param action the action to deploy/undeploy policy
55      * @return Response
56      */
57     public Response handlePolicyDeployOrUndeploy(final String policyName, final String policyVersion,
58                                                  final String action) {
59         // policies
60         JsonObject policyArrayBody = new JsonObject();
61         policyArrayBody.addProperty("name", policyName);
62         policyArrayBody.addProperty("version", policyVersion);
63         JsonArray policyArr = new JsonArray();
64         policyArr.add(policyArrayBody);
65
66         // deploymentSubgroups
67         JsonObject deploymentSubGrpBody = new JsonObject();
68         deploymentSubGrpBody.addProperty("pdpType", pdpType);
69         deploymentSubGrpBody.addProperty("action", action);
70         deploymentSubGrpBody.add("policies", policyArr);
71         JsonArray deployArr = new JsonArray();
72         deployArr.add(deploymentSubGrpBody);
73
74         // groups
75         JsonObject groupArrayBody = new JsonObject();
76         groupArrayBody.addProperty("name", pdpGroup);
77         groupArrayBody.add("deploymentSubgroups", deployArr);
78         JsonArray groupArr = new JsonArray();
79         groupArr.add(groupArrayBody);
80
81         // main json
82         JsonObject mainJson = new JsonObject();
83         mainJson.add("groups", groupArr);
84
85         return executePost(PAP_URI + "pdps/deployments/batch", Entity.entity(mainJson, MediaType.APPLICATION_JSON));
86     }
87 }