aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java
diff options
context:
space:
mode:
authorS�bastien Determe <sebastien.determe@intl.att.com>2021-04-02 08:59:06 +0000
committerGerrit Code Review <gerrit@onap.org>2021-04-02 08:59:06 +0000
commitdf451dc64de36d571efb4f0457a83bc94e8f3969 (patch)
tree71320c72b3a43199ffa28750fb96c25bbd19171b /src/main/java
parentb814a52eef4a28b5440638bdd1fc0b9fb58db467 (diff)
parentee233569e74e4620cb35ce4e9c4320b30d108824 (diff)
Merge "Rework the backend to support PDP updates"
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/onap/policy/clamp/loop/components/external/PolicyComponent.java90
-rw-r--r--src/main/java/org/onap/policy/clamp/loop/deploy/DcaeDeployParameters.java5
-rw-r--r--src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java108
-rw-r--r--src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupsAnalyzer.java120
-rw-r--r--src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java24
5 files changed, 212 insertions, 135 deletions
diff --git a/src/main/java/org/onap/policy/clamp/loop/components/external/PolicyComponent.java b/src/main/java/org/onap/policy/clamp/loop/components/external/PolicyComponent.java
index 648463d78..ff4e2643c 100644
--- a/src/main/java/org/onap/policy/clamp/loop/components/external/PolicyComponent.java
+++ b/src/main/java/org/onap/policy/clamp/loop/components/external/PolicyComponent.java
@@ -1,8 +1,8 @@
/*-
* ============LICENSE_START=======================================================
- * ONAP CLAMP
+ * ONAP POLICY-CLAMP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights
* reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,20 +25,19 @@ package org.onap.policy.clamp.loop.components.external;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
-import com.google.gson.GsonBuilder;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonObject;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
import javax.persistence.Transient;
import org.apache.camel.Exchange;
+import org.onap.policy.clamp.clds.util.JsonUtils;
import org.onap.policy.clamp.loop.Loop;
import org.onap.policy.clamp.policy.microservice.MicroServicePolicy;
import org.onap.policy.clamp.policy.operational.OperationalPolicy;
+import org.onap.policy.clamp.policy.pdpgroup.PdpGroupPayload;
+/**
+ * This class represents the policy state according to all policies involved in the control loop.
+ * It can compute it with all policy queries result.
+ * It contains also the method to generate the PDP payload used for the policies deployment.
+ */
public class PolicyComponent extends ExternalComponent {
@Transient
@@ -81,81 +80,22 @@ public class PolicyComponent extends ExternalComponent {
* @return The json, payload to send
*/
public static String createPoliciesPayloadPdpGroup(Loop loop, String action) {
- Map<String, Map<String, List<JsonObject>>> pdpGroupMap = new HashMap<>();
+ PdpGroupPayload pdpGroupPayload = new PdpGroupPayload();
for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) {
- updatePdpGroupMap(opPolicy.getPdpGroup(), opPolicy.getPdpSubgroup(),
- opPolicy.getName(),
- "1.0.0", pdpGroupMap);
+ pdpGroupPayload
+ .updatePdpGroupMap(opPolicy.getPdpGroup(), opPolicy.getPdpSubgroup(), opPolicy.getName(), "1.0.0");
}
for (MicroServicePolicy msPolicy : loop.getMicroServicePolicies()) {
- updatePdpGroupMap(msPolicy.getPdpGroup(), msPolicy.getPdpSubgroup(),
- msPolicy.getName(),
- "1.0.0", pdpGroupMap);
+ pdpGroupPayload
+ .updatePdpGroupMap(msPolicy.getPdpGroup(), msPolicy.getPdpSubgroup(), msPolicy.getName(), "1.0.0");
}
- String payload = new GsonBuilder().setPrettyPrinting().create()
- .toJson(generateActivatePdpGroupPayload(pdpGroupMap, action));
+ String payload = JsonUtils.GSON.toJson(pdpGroupPayload.generateActivatePdpGroupPayload(action));
logger.info("PdpGroup policy payload: " + payload);
return payload;
}
- private static void updatePdpGroupMap(String pdpGroup,
- String pdpSubGroup,
- String policyName,
- String policyVersion,
- Map<String, Map<String,
- List<JsonObject>>> pdpGroupMap) {
- JsonObject policyJson = new JsonObject();
- policyJson.addProperty("name", policyName);
- policyJson.addProperty("version", policyVersion);
- Map<String, List<JsonObject>> pdpSubGroupMap;
- List<JsonObject> policyList;
- if (pdpGroupMap.get(pdpGroup) == null) {
- pdpSubGroupMap = new HashMap<>();
- policyList = new LinkedList<>();
- } else {
- pdpSubGroupMap = pdpGroupMap.get(pdpGroup);
- if (pdpSubGroupMap.get(pdpSubGroup) == null) {
- policyList = new LinkedList<>();
- } else {
- policyList = (List<JsonObject>) pdpSubGroupMap.get(pdpSubGroup);
- }
- }
- policyList.add(policyJson);
- pdpSubGroupMap.put(pdpSubGroup, policyList);
- pdpGroupMap.put(pdpGroup, pdpSubGroupMap);
- }
-
- private static JsonObject generateActivatePdpGroupPayload(
- Map<String, Map<String, List<JsonObject>>> pdpGroupMap, String action) {
- JsonArray payloadArray = new JsonArray();
- for (Entry<String, Map<String, List<JsonObject>>> pdpGroupInfo : pdpGroupMap.entrySet()) {
- JsonObject pdpGroupNode = new JsonObject();
- JsonArray subPdpArray = new JsonArray();
- pdpGroupNode.addProperty("name", pdpGroupInfo.getKey());
- pdpGroupNode.add("deploymentSubgroups", subPdpArray);
-
- for (Entry<String, List<JsonObject>> pdpSubGroupInfo : pdpGroupInfo.getValue().entrySet()) {
- JsonObject pdpSubGroupNode = new JsonObject();
- subPdpArray.add(pdpSubGroupNode);
- pdpSubGroupNode.addProperty("pdpType", pdpSubGroupInfo.getKey());
- pdpSubGroupNode.addProperty("action", action);
-
- JsonArray policyArray = new JsonArray();
- pdpSubGroupNode.add("policies", policyArray);
-
- for (JsonObject policy : pdpSubGroupInfo.getValue()) {
- policyArray.add(policy);
- }
- }
- payloadArray.add(pdpGroupNode);
- }
- JsonObject jsonObject = new JsonObject();
- jsonObject.add("groups", payloadArray);
- return jsonObject;
- }
-
private static ExternalComponentState findNewState(boolean found, boolean deployed) {
ExternalComponentState newState = NOT_SENT;
diff --git a/src/main/java/org/onap/policy/clamp/loop/deploy/DcaeDeployParameters.java b/src/main/java/org/onap/policy/clamp/loop/deploy/DcaeDeployParameters.java
index 9d56f23c4..1a1414611 100644
--- a/src/main/java/org/onap/policy/clamp/loop/deploy/DcaeDeployParameters.java
+++ b/src/main/java/org/onap/policy/clamp/loop/deploy/DcaeDeployParameters.java
@@ -1,8 +1,8 @@
/*-
* ============LICENSE_START=======================================================
- * ONAP CLAMP
+ * ONAP POLICY-CLAMP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights
* reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -109,5 +109,4 @@ public class DcaeDeployParameters {
globalProperties.add("dcaeDeployParameters", deployParamJson);
return globalProperties;
}
-
}
diff --git a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java
new file mode 100644
index 000000000..c89395044
--- /dev/null
+++ b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupPayload.java
@@ -0,0 +1,108 @@
+/*-
+ * ============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.JsonArray;
+import com.google.gson.JsonObject;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 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.
+ */
+public class PdpGroupPayload {
+
+ private Map<String, Map<String, List<JsonObject>>> pdpGroupMap = new HashMap<>();
+
+ /**
+ * 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
+ */
+ public void updatePdpGroupMap(String pdpGroup,
+ String pdpSubGroup,
+ String policyName,
+ String policyVersion) {
+ JsonObject policyJson = new JsonObject();
+ policyJson.addProperty("name", policyName);
+ policyJson.addProperty("version", policyVersion);
+ Map<String, List<JsonObject>> pdpSubGroupMap;
+ List<JsonObject> policyList;
+ if (pdpGroupMap.get(pdpGroup) == null) {
+ pdpSubGroupMap = new HashMap<>();
+ policyList = new LinkedList<>();
+ } else {
+ pdpSubGroupMap = pdpGroupMap.get(pdpGroup);
+ if (pdpSubGroupMap.get(pdpSubGroup) == null) {
+ policyList = new LinkedList<>();
+ } else {
+ policyList = pdpSubGroupMap.get(pdpSubGroup);
+ }
+ }
+ policyList.add(policyJson);
+ pdpSubGroupMap.put(pdpSubGroup, policyList);
+ pdpGroupMap.put(pdpGroup, pdpSubGroupMap);
+ }
+
+ /**
+ * This method generates the Payload in Json from the pdp Group structure containing the policies/versions
+ * that must be sent to the policy framework.
+ *
+ * @param action The action to do, either a POST or a DELETE
+ * @return The Json that can be sent to policy framework as JsonObject
+ */
+ public JsonObject generateActivatePdpGroupPayload(String action) {
+ JsonArray payloadArray = new JsonArray();
+ for (Map.Entry<String, Map<String, List<JsonObject>>> pdpGroupInfo : pdpGroupMap.entrySet()) {
+ JsonObject pdpGroupNode = new JsonObject();
+ JsonArray subPdpArray = new JsonArray();
+ pdpGroupNode.addProperty("name", pdpGroupInfo.getKey());
+ pdpGroupNode.add("deploymentSubgroups", subPdpArray);
+
+ for (Map.Entry<String, List<JsonObject>> pdpSubGroupInfo : pdpGroupInfo.getValue().entrySet()) {
+ JsonObject pdpSubGroupNode = new JsonObject();
+ subPdpArray.add(pdpSubGroupNode);
+ pdpSubGroupNode.addProperty("pdpType", pdpSubGroupInfo.getKey());
+ pdpSubGroupNode.addProperty("action", action);
+
+ JsonArray policyArray = new JsonArray();
+ pdpSubGroupNode.add("policies", policyArray);
+
+ for (JsonObject policy : pdpSubGroupInfo.getValue()) {
+ policyArray.add(policy);
+ }
+ }
+ payloadArray.add(pdpGroupNode);
+ }
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.add("groups", payloadArray);
+ return jsonObject;
+ }
+}
diff --git a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupsAnalyzer.java b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupsAnalyzer.java
index 768872750..6098d0f63 100644
--- a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupsAnalyzer.java
+++ b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupsAnalyzer.java
@@ -25,9 +25,14 @@ package org.onap.policy.clamp.policy.pdpgroup;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
+import java.util.Map;
import java.util.Objects;
-import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.commons.collections4.CollectionUtils;
+import org.onap.policy.clamp.clds.util.JsonUtils;
import org.onap.policy.clamp.loop.template.PolicyModel;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpGroups;
@@ -44,8 +49,77 @@ public class PdpGroupsAnalyzer {
public static final String SUPPORTED_PDP_GROUPS_INFO = "supportedPdpGroups";
/**
- * Get supported subGroups based on the defined policy type and version for s specific PDPgroup.
- * It returns null if the Group is not ACTIVE or if the policytype/version has not been found in the PDPSubgroups.
+ * This structure holds the map of PdpGroups per policies, policies are identifed by ToscaConceptIdentifier.
+ */
+ private final Map<ToscaConceptIdentifier, Map<String, PdpGroup>> pdpGroupsDeploymentPerPolicy =
+ new ConcurrentHashMap<>();
+
+ /**
+ * Constructor taking he PDPGroups info from the PEF.
+ * It then caches the groups per policies and per types.
+ *
+ * @param pdpGroups The pdpgroup info from the PEF
+ */
+ public PdpGroupsAnalyzer(PdpGroups pdpGroups) {
+ this.analyzePdpGroups(pdpGroups);
+ }
+
+ /**
+ * Getter of the GroupDeploymentPerPolicy structure.
+ *
+ * @return The map of policies.
+ */
+ public Map<ToscaConceptIdentifier, Map<String, PdpGroup>> getPdpGroupsDeploymentPerPolicy() {
+ return pdpGroupsDeploymentPerPolicy;
+ }
+
+ private static void addInfoToPdpGroupsStructure(ToscaConceptIdentifier toscaId,
+ Map<ToscaConceptIdentifier,
+ Map<String,
+ PdpGroup>> pdpGroupsDeploymentPerToscaIdentifier,
+ PdpGroup pdpGroupSource,
+ PdpSubGroup pdpSubGroupSource) {
+ // Copy the subgroup but empty the policies & types
+ pdpGroupsDeploymentPerToscaIdentifier.computeIfAbsent(toscaId, toscaKey -> new ConcurrentHashMap<>())
+ .computeIfAbsent(pdpGroupSource.getName(), pdpGroupName -> {
+ PdpGroup pdpGroupCopy = new PdpGroup(pdpGroupSource);
+ pdpGroupCopy.setPdpSubgroups(new ArrayList<>());
+ return pdpGroupCopy;
+ }).getPdpSubgroups().add(new PdpSubGroup(pdpSubGroupSource));
+ }
+
+ private void analyzePdpGroups(PdpGroups pdpGroups) {
+ CollectionUtils.emptyIfNull(pdpGroups.getGroups()).stream()
+ .forEach(group -> CollectionUtils.emptyIfNull(group.getPdpSubgroups()).stream().forEach(subGroup ->
+ CollectionUtils.emptyIfNull(subGroup.getPolicies()).parallelStream().forEach(policy ->
+ PdpGroupsAnalyzer.addInfoToPdpGroupsStructure(policy, this.pdpGroupsDeploymentPerPolicy,
+ group, subGroup))));
+ }
+
+ /**
+ * This method retrieves all pdpGroups and subgroups where a specific policy name/version is deployed.
+ *
+ * @param policyName The policy name that must be used for searching
+ * @param version THe policy version that must be used for searching
+ * @return It returns a JsonObject containing each pdpGroup and subgroups associated
+ */
+ public JsonObject getPdpGroupsForPolicy(String policyName, String version) {
+ Map<String, PdpGroup> mapOfGroups =
+ this.pdpGroupsDeploymentPerPolicy.get(new ToscaConceptIdentifier(policyName, version));
+ if (mapOfGroups != null) {
+ JsonObject policyPdpGroups = new JsonObject();
+ JsonArray pdpGroupsArray = new JsonArray();
+ policyPdpGroups.add(ASSIGNED_PDP_GROUPS_INFO, pdpGroupsArray);
+ pdpGroupsArray.add(JsonUtils.GSON
+ .toJsonTree(mapOfGroups));
+ return policyPdpGroups;
+ }
+ return null;
+ }
+
+ /**
+ * Get supported subGroups based on the defined policy type and version for specific PDPGroup.
+ * It returns null if the Group is TERMINATED or if the policytype/version has not been found in the PDPSubgroups.
*
* @param pdpGroup The pdpGroup that must be analyzed
* @param policyType The policy type
@@ -56,7 +130,7 @@ public class PdpGroupsAnalyzer {
*/
private static JsonObject getSupportedPdpSubgroupsForModelType(PdpGroup pdpGroup, String policyType,
String version) {
- if (!PdpState.ACTIVE.equals(pdpGroup.getPdpGroupState())) {
+ if (PdpState.TERMINATED.equals(pdpGroup.getPdpGroupState())) {
return null;
}
JsonObject supportedPdpGroup = new JsonObject();
@@ -87,7 +161,7 @@ public class PdpGroupsAnalyzer {
pdpGroups.getGroups().stream().map(pdpGroup -> PdpGroupsAnalyzer.getSupportedPdpSubgroupsForModelType(pdpGroup,
policyType, version)).filter(Objects::nonNull)
- .forEach(jsonPdpGroup -> pdpGroupsArray.add(jsonPdpGroup));
+ .forEach(pdpGroupsArray::add);
return pdpGroupsArray.size() != 0 ? supportedPdpGroups : null;
}
@@ -99,38 +173,8 @@ public class PdpGroupsAnalyzer {
* @param pdpGroups The PdpGroups containing all PDP group definition
*/
public static void updatePdpGroupOfPolicyModels(List<PolicyModel> policyModelsList, PdpGroups pdpGroups) {
- policyModelsList.parallelStream().forEach(policyModel -> {
- policyModel.setPolicyPdpGroup(getSupportedPdpGroupsForModelType(pdpGroups, policyModel.getPolicyModelType(),
- policyModel.getVersion()));
- });
- }
-
- /**
- * This method searches for the PdpGroup/subgroup where the policy given is currently deployed.
- *
- * @param pdpGroups The pdpGroups info from PEF
- * @param policyName The policy Id
- * @param version The policy version
- * @return It returns a JsonObject containing the pdpGroup/subgroup info
- */
- public static JsonObject getPdpGroupDeploymentOfOnePolicy(PdpGroups pdpGroups, String policyName, String version) {
- JsonObject pdpGroupInfo = new JsonObject();
- JsonObject assignedPdpGroups = new JsonObject();
- pdpGroupInfo.add(ASSIGNED_PDP_GROUPS_INFO, assignedPdpGroups);
-
- ToscaConceptIdentifier toscaConceptIdentifier = new ToscaConceptIdentifier(policyName, version);
- pdpGroups.getGroups().stream().anyMatch(pdpGroup ->
- pdpGroup.getPdpSubgroups().stream().anyMatch(
- pdpSubGroup -> {
- if (pdpSubGroup.getPolicies() != null && pdpSubGroup.getPolicies()
- .contains(toscaConceptIdentifier)) {
- assignedPdpGroups.addProperty("pdpGroup", pdpGroup.getName());
- assignedPdpGroups.addProperty("pdpSubGroup", pdpSubGroup.getPdpType());
- return true;
- }
- return false;
- })
- );
- return assignedPdpGroups.entrySet().isEmpty() ? null : pdpGroupInfo;
+ policyModelsList.parallelStream().forEach(policyModel -> policyModel
+ .setPolicyPdpGroup(getSupportedPdpGroupsForModelType(pdpGroups, policyModel.getPolicyModelType(),
+ policyModel.getVersion())));
}
} \ No newline at end of file
diff --git a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java
index 44b11119b..6775eb0c6 100644
--- a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java
+++ b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java
@@ -23,11 +23,8 @@
package org.onap.policy.clamp.policy.pdpgroup;
-import com.google.gson.Gson;
-import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
-import java.util.Map;
import java.util.stream.StreamSupport;
import org.onap.policy.clamp.clds.util.JsonUtils;
import org.onap.policy.models.pdp.concepts.PdpGroups;
@@ -38,6 +35,8 @@ import org.onap.policy.models.pdp.concepts.PdpGroups;
*/
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,
@@ -91,8 +90,9 @@ public class PoliciesPdpMerger {
* @param policyJsonNode The policy json node that must be enriched
*/
private static void enrichOnePolicy(PdpGroups pdpGroups, JsonObject policyJsonNode) {
- JsonObject deploymentPdpJson = PdpGroupsAnalyzer
- .getPdpGroupDeploymentOfOnePolicy(pdpGroups, policyJsonNode.get("name").getAsString(),
+ PdpGroupsAnalyzer pdpGroupAnalyzer = new PdpGroupsAnalyzer(pdpGroups);
+ JsonObject deploymentPdpJson = pdpGroupAnalyzer
+ .getPdpGroupsForPolicy(policyJsonNode.get("name").getAsString(),
policyJsonNode.get("version").getAsString());
mergeJsonElement(policyJsonNode, deploymentPdpJson);
@@ -101,18 +101,4 @@ public class PoliciesPdpMerger {
policyJsonNode.get("type_version").getAsString());
mergeJsonElement(policyJsonNode, supportedPdpGroupsJson);
}
-
- /**
- * This method removes the pdp States added for one policy.
- *
- * @param policyJsonNode The policy node Json as String
- * @return The Json with pdp group info removed
- */
- public static JsonObject removePdpStatesOnePolicy(JsonObject policyJsonNode) {
- //JsonObject policyJson = JsonUtils.GSON.fromJson(policyJsonNode, JsonObject.class);
- // Simply remove the nodes we have added.
- policyJsonNode.remove(PdpGroupsAnalyzer.ASSIGNED_PDP_GROUPS_INFO);
- policyJsonNode.remove(PdpGroupsAnalyzer.SUPPORTED_PDP_GROUPS_INFO);
- return policyJsonNode;
- }
} \ No newline at end of file