diff options
Diffstat (limited to 'models-pdp/src/main')
3 files changed, 109 insertions, 9 deletions
diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java index 8805ae9ba..53bf3c1c6 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java @@ -21,13 +21,17 @@ package org.onap.policy.models.pdp.concepts; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; - +import java.util.Set; import lombok.Data; import lombok.NoArgsConstructor; - +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ObjectValidationResult; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.models.base.PfNameVersion; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.pdp.enums.PdpState; @@ -78,4 +82,55 @@ public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> { public int compareTo(final PdpGroup other) { return compareNameVersion(this, other); } + + /** + * Validates that appropriate fields are populated for an incoming call to the PAP + * REST API. + * + * @return the validation result + */ + public ValidationResult validatePapRest() { + BeanValidationResult result = new BeanValidationResult("group", this); + + /* + * Don't care about version or state, because we override them. Ok if description + * is null. + */ + + result.validateNotNull("name", name); + result.validateNotNullList("pdpSubgroups", pdpSubgroups, PdpSubGroup::validatePapRest); + + checkDuplicateSubgroups(result); + + return result; + } + + /** + * Checks for duplicate subgroups. + * + * @param result where to place validation results + */ + private void checkDuplicateSubgroups(BeanValidationResult result) { + if (pdpSubgroups == null) { + return; + } + + Set<String> set = new HashSet<>(); + + for (PdpSubGroup subgrp : pdpSubgroups) { + if (subgrp == null) { + continue; + } + + String pdpType = subgrp.getPdpType(); + if (pdpType == null) { + continue; + } + + if (!set.add(pdpType)) { + result.addResult(new ObjectValidationResult("subgroups", pdpType, ValidationStatus.INVALID, + "duplicate subgroup")); + } + } + } } diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java index 32e0b1a29..a7e05dd6b 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroups.java @@ -24,14 +24,14 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; - import lombok.Getter; import lombok.Setter; import lombok.ToString; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ValidationResult; /** - * Request deploy or update a set of groups via the PDP Group deployment - * REST API. + * Request deploy or update a set of groups via the PDP Group deployment REST API. */ @Getter @Setter @@ -41,14 +41,29 @@ public class PdpGroups { /** * Get the contents of this class as a list of PDP group maps. + * * @return the PDP groups in a list of maps */ public List<Map<String, PdpGroup>> toMapList() { final Map<String, PdpGroup> pdpGroupMap = new LinkedHashMap<>(); for (PdpGroup pdpGroup : groups) { - pdpGroupMap.put(pdpGroup.getName() + ':' + pdpGroup.getVersion() , pdpGroup); + pdpGroupMap.put(pdpGroup.getName() + ':' + pdpGroup.getVersion(), pdpGroup); } return Collections.singletonList(pdpGroupMap); } + + /** + * Validates that appropriate fields are populated for an incoming call to the PAP + * REST API. + * + * @return the validation result + */ + public ValidationResult validatePapRest() { + BeanValidationResult result = new BeanValidationResult("groups", this); + + result.validateNotNullList("groups", groups, PdpGroup::validatePapRest); + + return result; + } } diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java index 405408946..2618a5017 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java @@ -24,16 +24,19 @@ package org.onap.policy.models.pdp.concepts; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; - import lombok.Data; import lombok.NonNull; - +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ObjectValidationResult; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; /** - * Class to represent a group of all PDP's of the same pdp type running for a particular domain. + * Class to represent a group of all PDP's of the same pdp type running for a particular + * domain. * * @author Ram Krishna Verma (ram.krishna.verma@est.tech) */ @@ -68,4 +71,31 @@ public class PdpSubGroup { this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties)); this.pdpInstances = PfUtils.mapList(source.pdpInstances, Pdp::new); } + + /** + * Validates that appropriate fields are populated for an incoming call to the PAP + * REST API. + * + * @return the validation result + */ + public ValidationResult validatePapRest() { + BeanValidationResult result = new BeanValidationResult("group", this); + + result.validateNotNull("pdpType", pdpType); + result.validateNotNullList("supportedPolicyTypes", supportedPolicyTypes, + ToscaPolicyTypeIdentifier::validatePapRest); + result.validateNotNullList("policies", policies, ToscaPolicyIdentifier::validatePapRest); + + if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) { + result.addResult(new ObjectValidationResult("supportedPolicyTypes", supportedPolicyTypes, + ValidationStatus.INVALID, "empty list")); + } + + if (desiredInstanceCount <= 0) { + result.addResult(new ObjectValidationResult("desiredInstanceCount", desiredInstanceCount, + ValidationStatus.INVALID, "non-positive")); + } + + return result; + } } |