X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-pdp%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fpdp%2Fconcepts%2FPdpGroup.java;h=53bf3c1c63848704aef59d3e96622225f90b29e6;hb=92d9b661cc32b8dcc90e813aa220e26ef6f83b17;hp=9665fd472ed6b3541a5f69de8f46fa4374e832bd;hpb=009e46781b895c60a012c4ac13baddb145f698e9;p=policy%2Fmodels.git 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 9665fd472..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; @@ -41,6 +45,12 @@ import org.onap.policy.models.pdp.enums.PdpState; @Data @NoArgsConstructor public class PdpGroup implements PfNameVersion, Comparable { + /** + * In the future, we'll eliminate the "version" field. Until then, the version of + * every group should always be this fixed value. + */ + public static final String VERSION = "1.0.0"; + private String name; private String version; private String description; @@ -72,4 +82,55 @@ public class PdpGroup implements PfNameVersion, Comparable { 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 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")); + } + } + } }