Add validation methods for PAP REST API
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / PdpGroup.java
index 4a26b16..53bf3c1 100644 (file)
 
 package org.onap.policy.models.pdp.concepts;
 
+import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-
-import lombok.Getter;
+import java.util.Set;
+import lombok.Data;
 import lombok.NoArgsConstructor;
-import lombok.Setter;
-import lombok.ToString;
+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;
-import org.onap.policy.models.tosca.simple.concepts.ToscaEntityType;
 
 /**
  * Class to represent a PDPGroup, which groups multiple PDPSubGroup entities together for
@@ -39,13 +42,18 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaEntityType;
  *
  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
  */
-@Getter
-@Setter
-@ToString
+@Data
 @NoArgsConstructor
-public class PdpGroup extends ToscaEntityType {
-    private static final long serialVersionUID = 1L;
+public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> {
+    /**
+     * 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;
     private PdpState pdpGroupState;
     private Map<String, String> properties;
     private List<PdpSubGroup> pdpSubgroups;
@@ -62,9 +70,67 @@ public class PdpGroup extends ToscaEntityType {
      * @param source source from which to copy fields
      */
     public PdpGroup(PdpGroup source) {
-        super(source);
+        this.name = source.name;
+        this.version = source.version;
+        this.description = source.description;
         this.pdpGroupState = source.pdpGroupState;
         this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties));
         this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new);
     }
+
+    @Override
+    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"));
+            }
+        }
+    }
 }