Add validation methods for PAP REST API
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / authorative / concepts / ToscaPolicyIdentifier.java
index e55c6bd..f98a238 100644 (file)
@@ -23,13 +23,16 @@ package org.onap.policy.models.tosca.authorative.concepts;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.NonNull;
+import org.apache.commons.lang3.ObjectUtils;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.ValidationResult;
 
 /**
  * Identifies a policy. Both the name and version must be non-null.
  */
 @Data
 @NoArgsConstructor
-public class ToscaPolicyIdentifier {
+public class ToscaPolicyIdentifier implements Comparable<ToscaPolicyIdentifier> {
 
     @NonNull
     private String name;
@@ -47,4 +50,37 @@ public class ToscaPolicyIdentifier {
         this.name = source.name;
         this.version = source.version;
     }
+
+    /**
+     * 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("name", name);
+        result.validateNotNull("version", version);
+
+        return result;
+    }
+
+    @Override
+    public int compareTo(ToscaPolicyIdentifier other) {
+        if (this == other) {
+            return 0;
+        }
+
+        if (other == null) {
+            return 1;
+        }
+
+        int result = ObjectUtils.compare(getName(), other.getName());
+        if (result != 0) {
+            return result;
+        }
+
+        return ObjectUtils.compare(getVersion(), other.getVersion());
+    }
 }