aboutsummaryrefslogtreecommitdiffstats
path: root/runtime/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java
blob: c6b44076f110d2787d368f5dd9547b68318b3dee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*-
 * ============LICENSE_START=======================================================
 * ONAP POLICY-CLAMP
 * ================================================================================
 * Copyright (C) 2021 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */

package org.onap.policy.clamp.policy.pdpgroup;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.google.gson.JsonElement;
import java.util.ArrayList;
import java.util.Arrays;
import org.onap.policy.clamp.clds.util.JsonUtils;
import org.onap.policy.models.pdp.concepts.DeploymentGroup;
import org.onap.policy.models.pdp.concepts.DeploymentGroups;
import org.onap.policy.models.pdp.concepts.DeploymentSubGroup;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;

/**
 * This is an utility class that build the PDP group policy payload.
 * This is used when policies have to be deployed to PDP group/subgroups on the Policy Engine.
 * Currently it does not group the queries per pdpgroup/subgroups/action.
 * This is currently NOT thread safe, do not use parallel streams to update the structure.
 */
public class PdpGroupPayload {

    private static final EELFLogger logger = EELFManager.getInstance().getLogger(PdpGroupPayload.class);

    /**
     * The default node that will contain the actions array.
     */
    public static final String PDP_ACTIONS = "PdpActions";

    private final DeploymentGroups deploymentGroups = new DeploymentGroups();

    /**
     * Default constructor.
     */
    public PdpGroupPayload() {
        deploymentGroups.setGroups(new ArrayList<>());
    }

    /**
     * Constructor that takes a list of actions in input.
     *
     * @param listOfPdpActions The list of actions that needs to be done.
     *                         e.g: {"Pdpactions":["DELETE/PdpGroup1/PdpSubGroup1/PolicyName1/1.0.0",....]}
     * @throws PdpGroupPayloadException in case of issues to read the listOfActions
     */
    public PdpGroupPayload(final JsonElement listOfPdpActions) throws PdpGroupPayloadException {
        this();
        this.readListOfActions(listOfPdpActions);
    }

    /**
     * This method converts the list of actions directly to the pdp payload query as String.
     *
     * @param listOfPdpActions The list of actions that needs to be done.
     *                         e.g: {"Pdpactions":["DELETE/PdpGroup1/PdpSubGroup1/PolicyName1/1.0.0",....]}
     * @return The string containing the PDP payload that can be sent directly
     * @throws PdpGroupPayloadException in case of issues to read the listOfActions
     */
    public static String generatePdpGroupPayloadFromList(final JsonElement listOfPdpActions)
            throws PdpGroupPayloadException {
        return new PdpGroupPayload(listOfPdpActions).generatePdpGroupPayload();
    }


    private void readListOfActions(final JsonElement listOfPdpActions) throws PdpGroupPayloadException {
        for (JsonElement action : listOfPdpActions.getAsJsonObject().getAsJsonArray(PDP_ACTIONS)) {
            String[] opParams = action.getAsString().split("/");
            if (opParams.length == 5) {
                this.updatePdpGroupMap(opParams[1], opParams[2], opParams[3], opParams[4], opParams[0]);
            } else {
                logger.error("One PDP push command does not contain the right number of arguments: " + action);
                throw new PdpGroupPayloadException(
                        "One PDP push command does not contain the right number of arguments: " + action);
            }
        }
    }

    /**
     * This method updates the pdpGroupMap structure for a specific policy/version/pdpdGroup/PdpSubGroup.
     *
     * @param pdpGroup      The pdp Group in String
     * @param pdpSubGroup   The pdp Sub Group in String
     * @param policyName    The policy name
     * @param policyVersion The policy Version
     * @param action        DELETE or POST
     */
    public void updatePdpGroupMap(String pdpGroup,
                                  String pdpSubGroup,
                                  String policyName,
                                  String policyVersion, String action) {
        // create subgroup
        DeploymentSubGroup newSubGroup = new DeploymentSubGroup();
        newSubGroup.setPdpType(pdpSubGroup);
        newSubGroup.setAction(DeploymentSubGroup.Action.valueOf(action));
        newSubGroup.setPolicies(Arrays.asList(new ToscaConceptIdentifier(policyName, policyVersion)));
        // Add to deployment Groups structure
        this.deploymentGroups.getGroups().stream().filter(group ->
                group.getName().equals(pdpGroup)).findFirst()
                .ifPresentOrElse(group -> group.getDeploymentSubgroups().add(newSubGroup),
                        () -> {
                            DeploymentGroup newGroup = new DeploymentGroup();
                            newGroup.setName(pdpGroup);
                            newGroup.setDeploymentSubgroups(new ArrayList<>(Arrays.asList(newSubGroup)));
                            this.deploymentGroups.getGroups().add(newGroup);
                        });
    }

    /**
     * This method generates the Payload in Json from the pdp Group structure containing the policies/versions
     * that must be sent to the policy framework.
     *
     * @return The Json that can be sent to policy framework as String
     */
    public String generatePdpGroupPayload() {
        String payload = JsonUtils.GSON.toJson(this.deploymentGroups);
        logger.info("PdpGroup policy payload: " + payload);
        return payload;
    }
}