aboutsummaryrefslogtreecommitdiffstats
path: root/runtime/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java
blob: 6775eb0c60f62285bf8a747269fb2726989f95de (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
/*-
 * ============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.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.stream.StreamSupport;
import org.onap.policy.clamp.clds.util.JsonUtils;
import org.onap.policy.models.pdp.concepts.PdpGroups;

/**
 * This is an utility class that contains methods to work on the different results provided by the PEF.
 * Mainly used to aggregate the results.
 */
public class PoliciesPdpMerger {

    private PoliciesPdpMerger() {}

    /**
     * This method extract the content of a policy without knowing the key (policy Id).
     * This JsonElement normally contains only the policy ID then the content,
     * there is only one member in the Json element.
     * As this is not really practical to use this method remove that
     * nested Json.
     *
     * @param policyJsonElement The policy as JsonElement
     * @return It return the content as JsonObject
     */
    public static JsonObject getPolicyContentOutOfJsonElement(JsonElement policyJsonElement) {
        mergeJsonElement(policyJsonElement.getAsJsonObject(), policyJsonElement.getAsJsonObject()
                .remove(((String) policyJsonElement.getAsJsonObject().keySet().toArray()[0])).getAsJsonObject());
        return policyJsonElement.getAsJsonObject();
    }

    /**
     * This method merges 2 JsonElement together. If the jsonToMerge is null nothing is changed.
     *
     * @param json        The initial json that will received the data
     * @param jsonToMerge The json that will be added to the first json object
     */
    public static void mergeJsonElement(JsonObject json, JsonObject jsonToMerge) {
        if (jsonToMerge != null) {
            jsonToMerge.entrySet().stream().forEach(entry -> json.add(entry.getKey(), entry.getValue()));
        }
    }

    /**
     * This method merges the result of the policy listing and the associated Pdp Group info.
     * It can be seen as an enrichment of the policy listing.
     *
     * @param jsonPoliciesList The Json containing the policies from the PEF
     * @param pdpGroupsJson    The json containing the PDP groups info from the PEF
     * @return It returns a JsonObject containing the policies list enriched with PdpGroup info
     */
    public static JsonObject mergePoliciesAndPdpGroupStates(String jsonPoliciesList, String pdpGroupsJson) {
        PdpGroups pdpGroups = JsonUtils.GSON.fromJson(pdpGroupsJson, PdpGroups.class);
        JsonObject policiesListJson =
                JsonUtils.GSON.fromJson(jsonPoliciesList, JsonObject.class).get("topology_template")
                        .getAsJsonObject();
        StreamSupport.stream(policiesListJson.get("policies").getAsJsonArray().spliterator(), true)
                .forEach(policyJson -> enrichOnePolicy(pdpGroups, getPolicyContentOutOfJsonElement(policyJson)));
        return policiesListJson;
    }

    /**
     * Enrich one policy json node object with pdpGroup info.
     *
     * @param pdpGroups      The pdpGroups from PEF to search the policy
     * @param policyJsonNode The policy json node that must be enriched
     */
    private static void enrichOnePolicy(PdpGroups pdpGroups, JsonObject policyJsonNode) {
        PdpGroupsAnalyzer pdpGroupAnalyzer = new PdpGroupsAnalyzer(pdpGroups);
        JsonObject deploymentPdpJson = pdpGroupAnalyzer
                .getPdpGroupsForPolicy(policyJsonNode.get("name").getAsString(),
                        policyJsonNode.get("version").getAsString());
        mergeJsonElement(policyJsonNode, deploymentPdpJson);

        JsonObject supportedPdpGroupsJson = PdpGroupsAnalyzer
                .getSupportedPdpGroupsForModelType(pdpGroups, policyJsonNode.get("type").getAsString(),
                        policyJsonNode.get("type_version").getAsString());
        mergeJsonElement(policyJsonNode, supportedPdpGroupsJson);
    }
}